Whitespace normalization. - 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 1cff9deb7a8839bff68b86fa5c5c4a21049bc849
 (DIR) parent d77dc3305c595d23651ca6cfdbfdf9a7503eac5c
 (HTM) Author: Rob King <jking@deadpixi.com>
       Date:   Tue,  6 Sep 2016 17:24:43 -0500
       
       Whitespace normalization.
       
       Diffstat:
         include/libc.h                      |      58 +++++++++++++++---------------
         include/libg.h                      |     250 ++++++++++++++++----------------
         include/regexp.h                    |      84 ++++++++++++++++----------------
         libXg/Gwin.h                        |      14 +++++++-------
         libXg/GwinP.h                       |      28 ++++++++++++++--------------
         libXg/arc.c                         |      60 ++++++++++++++++----------------
         libXg/arith.c                       |     186 ++++++++++++++++----------------
         libXg/balloc.c                      |      76 ++++++++++++++++----------------
         libXg/bitblt.c                      |      96 ++++++++++++++++----------------
         libXg/bitbltclip.c                  |     114 ++++++++++++++++----------------
         libXg/border.c                      |      38 ++++++++++++++++----------------
         libXg/bscreenrect.c                 |       6 +++---
         libXg/circle.c                      |      24 ++++++++++++------------
         libXg/clipline.c                    |     336 ++++++++++++++++----------------
         libXg/clipr.c                       |      22 +++++++++++-----------
         libXg/copymasked.c                  |      64 ++++++++++++++++----------------
         libXg/cursorset.c                   |       6 +++---
         libXg/disc.c                        |      24 ++++++++++++------------
         libXg/ellipse.c                     |      20 ++++++++++----------
         libXg/font.c                        |       2 +-
         libXg/gcs.c                         |     560 ++++++++++++++++----------------
         libXg/getrect.c                     |      78 ++++++++++++++++----------------
         libXg/gwin.c                        |     474 ++++++++++++++++----------------
         libXg/latin1.c                      |      54 ++++++++++++++++----------------
         libXg/ldconvert.c                   |      86 +++++++++++++++---------------
         libXg/libgint.h                     |      46 ++++++++++++++++----------------
         libXg/menuhit.c                     |     352 ++++++++++++++++----------------
         libXg/point.c                       |      20 ++++++++++----------
         libXg/polysegment.c                 |      32 ++++++++++++++++----------------
         libXg/rdbitmap.c                    |      98 ++++++++++++++++----------------
         libXg/rdbitmapfile.c                |     102 ++++++++++++++++----------------
         libXg/rectclip.c                    |      36 ++++++++++++++++----------------
         libXg/rune.c                        |     340 ++++++++++++++++----------------
         libXg/segment.c                     |      28 ++++++++++++++--------------
         libXg/string.c                      |      38 ++++++++++++++++----------------
         libXg/strwidth.c                    |       2 +-
         libXg/texture.c                     |      62 ++++++++++++++++----------------
         libXg/wrbitmap.c                    |      70 ++++++++++++++++----------------
         libXg/wrbitmapfile.c                |      74 ++++++++++++++++----------------
         libXg/xtbinit.c                     |     514 ++++++++++++++++----------------
         libframe/frbox.c                    |     186 ++++++++++++++++----------------
         libframe/frdelete.c                 |     182 ++++++++++++++++----------------
         libframe/frdraw.c                   |      76 ++++++++++++++++----------------
         libframe/frinit.c                   |      56 ++++++++++++++++----------------
         libframe/frinsert.c                 |     442 ++++++++++++++++----------------
         libframe/frptofchar.c               |     166 ++++++++++++++++----------------
         libframe/frselect.c                 |     140 ++++++++++++++++----------------
         libframe/frstr.c                    |      38 ++++++++++++++++----------------
         libframe/frutil.c                   |     140 ++++++++++++++++----------------
         libframe/misc.c                     |      90 ++++++++++++++++----------------
         sam/address.c                       |     412 ++++++++++++++++----------------
         sam/buffer.c                        |     264 ++++++++++++++++----------------
         sam/cmd.c                           |     906 ++++++++++++++++----------------
         sam/disc.c                          |     514 ++++++++++++++++----------------
         sam/error.c                         |     162 +++++++++++++++---------------
         sam/errors.h                        |     116 +++++++++++++++---------------
         sam/file.c                          |     702 ++++++++++++++++----------------
         sam/io.c                            |     414 +++++++++++++++---------------
         sam/list.c                          |      34 ++++++++++++++++----------------
         sam/mesg.c                          |    1202 ++++++++++++++++----------------
         sam/mesg.h                          |     164 ++++++++++++++++----------------
         sam/moveto.c                        |     250 ++++++++++++++++----------------
         sam/multi.c                         |     112 ++++++++++++++++----------------
         sam/parse.h                         |      98 ++++++++++++++++----------------
         sam/rasp.c                          |     470 ++++++++++++++++----------------
         sam/regexp.c                        |    1286 ++++++++++++++++----------------
         sam/sam.c                           |     936 ++++++++++++++++----------------
         sam/sam.h                           |     606 ++++++++++++++++----------------
         sam/shell.c                         |     276 +++++++++++++++---------------
         sam/string.c                        |     172 +++++++++++++++---------------
         sam/sys.c                           |      54 ++++++++++++++++----------------
         sam/unix.c                          |     190 +++++++++++++++---------------
         sam/xec.c                           |     672 ++++++++++++++++----------------
         samterm/flayer.c                    |     638 ++++++++++++++++----------------
         samterm/flayer.h                    |      64 ++++++++++++++++----------------
         samterm/icons.c                     |      14 +++++++-------
         samterm/io.c                        |     218 ++++++++++++++++----------------
         samterm/main.c                      |     678 ++++++++++++++++----------------
         samterm/menu.c                      |     600 ++++++++++++++++----------------
         samterm/mesg.c                      |    1136 ++++++++++++++++----------------
         samterm/rasp.c                      |     358 ++++++++++++++++----------------
         samterm/samterm.h                   |     252 ++++++++++++++++----------------
         samterm/scroll.c                    |     224 ++++++++++++++++----------------
         samterm/unix.c                      |     200 ++++++++++++++++----------------
       
       84 files changed, 10242 insertions(+), 10242 deletions(-)
       ---
 (DIR) diff --git a/include/libc.h b/include/libc.h
       @@ -1,53 +1,53 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
        
       -        /* Plan 9 C library interface */
       +    /* Plan 9 C library interface */
        
        
       -#define        sprint                                sprintf
       -#define        dup(a,b)                        dup2(a,b)
       -#define        seek(a,b,c)                        lseek(a,b,c)
       -#define        create(name, mode, perm)        creat(name, perm)
       -#define        exec(a,b)                        execv(a,b)
       -#define        USED(a)
       +#define sprint              sprintf
       +#define dup(a,b)            dup2(a,b)
       +#define seek(a,b,c)         lseek(a,b,c)
       +#define create(name, mode, perm)    creat(name, perm)
       +#define exec(a,b)           execv(a,b)
       +#define USED(a)
        #define SET(a)
        
       -#define        _exits(v)                        if (v!=0) _exit(1); else _exit(0)
       +#define _exits(v)           if (v!=0) _exit(1); else _exit(0)
        
        enum
        {
       -        OREAD        =        0,                /* open for read */
       -        OWRITE        =        1,                /* open for write */
       -        ORDWR        =        2,                /* open for read/write */
       -        ERRLEN        =        64                /* length of error message */
       +    OREAD   =   0,      /* open for read */
       +    OWRITE  =   1,      /* open for write */
       +    ORDWR   =   2,      /* open for read/write */
       +    ERRLEN  =   64      /* length of error message */
        };
        
        enum
        {
       -        UTFmax                = 3,                /* maximum bytes per rune */
       -        Runesync        = 0x80,                /* cannot represent part of a utf sequence (<) */
       -        Runeself        = 0x80,                /* rune and utf sequences are the same (<) */
       -        Runeerror        = 0x80                /* decoding error in utf */
       +    UTFmax      = 3,        /* maximum bytes per rune */
       +    Runesync    = 0x80,     /* cannot represent part of a utf sequence (<) */
       +    Runeself    = 0x80,     /* rune and utf sequences are the same (<) */
       +    Runeerror   = 0x80      /* decoding error in utf */
        };
        
        /*
         * new rune routines
         */
       -extern        int        runetochar(char*, Rune*);
       -extern        int        chartorune(Rune*, char*);
       -extern        int        runelen(long);
       -extern        int        fullrune(char*, int);
       +extern  int runetochar(char*, Rune*);
       +extern  int chartorune(Rune*, char*);
       +extern  int runelen(long);
       +extern  int fullrune(char*, int);
        
        /*
         * rune routines from converted str routines
         */
       -extern        int        utflen(char*);                /* was countrune */
       -extern        char*        utfrune(char*, long);
       -extern        char*        utfrrune(char*, long);
       -extern        char*        utfutf(char*, char*);
       +extern  int utflen(char*);      /* was countrune */
       +extern  char*   utfrune(char*, long);
       +extern  char*   utfrrune(char*, long);
       +extern  char*   utfutf(char*, char*);
        /*
       - *        Miscellaneous functions
       + *  Miscellaneous functions
         */
       -extern        int        notify (void(*)(void *, char *));
       -extern        int        errstr(char *);
       -extern        char*        getuser(void);
       -extern        void        exits(char*);
       +extern  int notify (void(*)(void *, char *));
       +extern  int errstr(char *);
       +extern  char*   getuser(void);
       +extern  void    exits(char*);
 (DIR) diff --git a/include/libg.h b/include/libg.h
       @@ -10,7 +10,7 @@
        
        #include <commands.h>
        
       -enum{ EMAXMSG = 128+8192 };        /* max event size */
       +enum{ EMAXMSG = 128+8192 }; /* max event size */
        
        /*
         * Cursors
       @@ -28,43 +28,43 @@ enum{
         * Types
         */
        
       -typedef        struct        Bitmap                Bitmap;
       -typedef struct        Point                Point;
       -typedef struct        Rectangle         Rectangle;
       -typedef struct        Keystroke        Keystroke;
       -typedef struct        Mouse                Mouse;
       -typedef struct        Menu                Menu;
       -typedef struct        Event                Event;
       -typedef struct        RGB                RGB;
       +typedef struct  Bitmap      Bitmap;
       +typedef struct  Point       Point;
       +typedef struct  Rectangle   Rectangle;
       +typedef struct  Keystroke   Keystroke;
       +typedef struct  Mouse       Mouse;
       +typedef struct  Menu        Menu;
       +typedef struct  Event       Event;
       +typedef struct  RGB     RGB;
        
       -struct        Point
       +struct  Point
        {
       -        int        x;
       -        int        y;
       +    int x;
       +    int y;
        };
        
        struct Rectangle
        {
       -        Point min;
       -        Point max;
       +    Point min;
       +    Point max;
        };
        
       -struct        Bitmap
       +struct  Bitmap
        {
       -        Rectangle r;                /* rectangle in data area, local coords */
       -        Rectangle clipr;        /* clipping region */
       -        int        ldepth;
       -        int        id;                /* as known by the X server */
       -        Bitmap        *cache;                /* zero; distinguishes bitmap from layer */
       +    Rectangle r;        /* rectangle in data area, local coords */
       +    Rectangle clipr;    /* clipping region */
       +    int ldepth;
       +    int id;     /* as known by the X server */
       +    Bitmap  *cache;     /* zero; distinguishes bitmap from layer */
            XftDraw *fd; /* font drawable */
       -        int        flag;                /* flag used by X implementation of libg */
       +    int flag;       /* flag used by X implementation of libg */
        };
        
       -struct        Mouse
       +struct  Mouse
        {
       -        int                buttons; /* bit array: LMR=124 */
       -        Point                xy;
       -        unsigned long        msec;
       +    int     buttons; /* bit array: LMR=124 */
       +    Point       xy;
       +    unsigned long   msec;
        };
        
        struct  Keystroke
       @@ -77,71 +77,71 @@ struct  Keystroke
        
        struct Menu
        {
       -        char        **item;
       -        char        *(*gen)(int);
       -        int        lasthit;
       +    char    **item;
       +    char    *(*gen)(int);
       +    int lasthit;
        };
        
       -struct        Event
       +struct  Event
        {
       -        Keystroke        keystroke;
       -        Mouse                mouse;
       -        int                n;                /* number of characters in mesage */
       -        unsigned char        data[EMAXMSG];        /* message from an arbitrary file descriptor */
       +    Keystroke   keystroke;
       +    Mouse       mouse;
       +    int     n;      /* number of characters in mesage */
       +    unsigned char   data[EMAXMSG];  /* message from an arbitrary file descriptor */
        };
        
        struct RGB
        {
       -        unsigned long        red;
       -        unsigned long        green;
       -        unsigned long        blue;
       +    unsigned long   red;
       +    unsigned long   green;
       +    unsigned long   blue;
        };
        
        /*
         * Codes for bitblt etc.
         *
       - *               D
       - *             0   1
       + *         D
       + *       0   1
         *         ---------
       - *         0 | 1 | 2 |
       + *   0 | 1 | 2 |
         *     S   |---|---|
       - *          1 | 4 | 8 |
       + *   1 | 4 | 8 |
         *         ---------
         *
       - *        Usually used as D|S; DorS is so tracebacks are readable.
       + *  Usually used as D|S; DorS is so tracebacks are readable.
         */
        typedef
       -enum        Fcode
       +enum    Fcode
        {
       -        Zero                = 0x0,
       -        DnorS                = 0x1,
       -        DandnotS        = 0x2,
       -        notS                = 0x3,
       -        notDandS        = 0x4,
       -        notD                = 0x5,
       -        DxorS                = 0x6,
       -        DnandS                = 0x7,
       -        DandS                = 0x8,
       -        DxnorS                = 0x9,
       -        D                = 0xA,
       -        DornotS                = 0xB,
       -        S                = 0xC,
       -        notDorS                = 0xD,
       -        DorS                = 0xE,
       -        F                = 0xF
       +    Zero        = 0x0,
       +    DnorS       = 0x1,
       +    DandnotS    = 0x2,
       +    notS        = 0x3,
       +    notDandS    = 0x4,
       +    notD        = 0x5,
       +    DxorS       = 0x6,
       +    DnandS      = 0x7,
       +    DandS       = 0x8,
       +    DxnorS      = 0x9,
       +    D       = 0xA,
       +    DornotS     = 0xB,
       +    S       = 0xC,
       +    notDorS     = 0xD,
       +    DorS        = 0xE,
       +    F       = 0xF
        } Fcode;
        
        /*
         * Miscellany
         */
        
       -typedef void         (*Errfunc)(char *);
       +typedef void     (*Errfunc)(char *);
        
        extern void addlatin(char, char, short);
       -extern Point         add(Point, Point);
       -extern Point         sub(Point, Point);
       -extern Point         mul(Point, int);
       -extern Point         divpt(Point, int);
       +extern Point     add(Point, Point);
       +extern Point     sub(Point, Point);
       +extern Point     mul(Point, int);
       +extern Point     divpt(Point, int);
        extern Rectangle rsubp(Rectangle, Point);
        extern Rectangle raddp(Rectangle, Point);
        extern Rectangle inset(Rectangle, int);
       @@ -149,92 +149,92 @@ extern Rectangle rmul(Rectangle, int);
        extern Rectangle rdiv(Rectangle, int);
        extern Rectangle rshift(Rectangle, int);
        extern Rectangle rcanon(Rectangle);
       -extern Bitmap*         balloc(Rectangle, int);
       -extern void         bfree(Bitmap*);
       -extern int         rectclip(Rectangle*, Rectangle);
       -extern void         xtbinit(Errfunc, char*, int*, char**, char**);
       -extern void         bclose(void);
       -extern void         berror(char*);
       -extern void         bitblt2(Bitmap*, Point, Bitmap*, Rectangle, Fcode, unsigned long, unsigned long);
       -extern void         bitblt(Bitmap*, Point, Bitmap*, Rectangle, Fcode);
       -extern void         copymasked(Bitmap*, Point, Bitmap*, Bitmap*, Rectangle);
       -extern int         bitbltclip(void*);
       -extern Point         string(Bitmap*, Point, XftFont*, char*, Fcode);
       -extern void         segment(Bitmap*, Point, Point, int, Fcode);
       -extern void         point(Bitmap*, Point, int, Fcode);
       -extern void         arc(Bitmap*, Point, Point, Point, int, Fcode);
       -extern void         circle(Bitmap*, Point, int, int, Fcode);
       -extern void         disc(Bitmap*, Point, int, int, Fcode);
       -extern void         ellipse(Bitmap*, Point, int, int, int, Fcode);
       -extern void         polysegment(Bitmap *, int, Point *, int, Fcode);
       -extern long         strwidth(XftFont*, char*);
       -extern Point         strsize(XftFont*, char*);
       -extern long         charwidth(XftFont*, Rune);
       -extern void         texture(Bitmap*, Rectangle, Bitmap*, Fcode);
       -extern void         wrbitmap(Bitmap*, int, int, unsigned char*);
       -extern void         rdbitmap(Bitmap*, int, int, unsigned char*);
       -extern void         wrbitmapfile(int, Bitmap*);
       -extern Bitmap*         rdbitmapfile(int);
       -extern int         ptinrect(Point, Rectangle);
       -extern int         rectXrect(Rectangle, Rectangle);
       -extern int         eqpt(Point, Point);
       -extern int         eqrect(Rectangle, Rectangle);
       -extern void         border(Bitmap*, Rectangle, int, Fcode, unsigned long);
       -extern void         cursorswitch(unsigned int);
       -extern void         cursorset(Point);
       +extern Bitmap*   balloc(Rectangle, int);
       +extern void  bfree(Bitmap*);
       +extern int   rectclip(Rectangle*, Rectangle);
       +extern void  xtbinit(Errfunc, char*, int*, char**, char**);
       +extern void  bclose(void);
       +extern void  berror(char*);
       +extern void  bitblt2(Bitmap*, Point, Bitmap*, Rectangle, Fcode, unsigned long, unsigned long);
       +extern void  bitblt(Bitmap*, Point, Bitmap*, Rectangle, Fcode);
       +extern void  copymasked(Bitmap*, Point, Bitmap*, Bitmap*, Rectangle);
       +extern int   bitbltclip(void*);
       +extern Point     string(Bitmap*, Point, XftFont*, char*, Fcode);
       +extern void  segment(Bitmap*, Point, Point, int, Fcode);
       +extern void  point(Bitmap*, Point, int, Fcode);
       +extern void  arc(Bitmap*, Point, Point, Point, int, Fcode);
       +extern void  circle(Bitmap*, Point, int, int, Fcode);
       +extern void  disc(Bitmap*, Point, int, int, Fcode);
       +extern void  ellipse(Bitmap*, Point, int, int, int, Fcode);
       +extern void  polysegment(Bitmap *, int, Point *, int, Fcode);
       +extern long  strwidth(XftFont*, char*);
       +extern Point     strsize(XftFont*, char*);
       +extern long  charwidth(XftFont*, Rune);
       +extern void  texture(Bitmap*, Rectangle, Bitmap*, Fcode);
       +extern void  wrbitmap(Bitmap*, int, int, unsigned char*);
       +extern void  rdbitmap(Bitmap*, int, int, unsigned char*);
       +extern void  wrbitmapfile(int, Bitmap*);
       +extern Bitmap*   rdbitmapfile(int);
       +extern int   ptinrect(Point, Rectangle);
       +extern int   rectXrect(Rectangle, Rectangle);
       +extern int   eqpt(Point, Point);
       +extern int   eqrect(Rectangle, Rectangle);
       +extern void  border(Bitmap*, Rectangle, int, Fcode, unsigned long);
       +extern void  cursorswitch(unsigned int);
       +extern void  cursorset(Point);
        extern Rectangle bscreenrect(Rectangle*);
       -extern void         bflush(void);
       -extern int         clipline(Rectangle, Point*, Point*);
       -extern int         clipr(Bitmap*, Rectangle);
       -extern int         scrpix(int*,int*);
       +extern void  bflush(void);
       +extern int   clipline(Rectangle, Point*, Point*);
       +extern int   clipr(Bitmap*, Rectangle);
       +extern int   scrpix(int*,int*);
        extern unsigned long getbg(void);
        
       -extern void         einit(unsigned long);
       +extern void  einit(unsigned long);
        extern unsigned long estart(unsigned long, int, int);
        extern unsigned long etimer(unsigned long, long);
        extern unsigned long event(Event*);
        extern unsigned long eread(unsigned long, Event*);
       -extern Mouse         emouse(void);
       -extern Keystroke         ekbd(void);
       -extern void         pushkbd(int c);
       -extern int         ecanread(unsigned long);
       -extern int         ecanmouse(void);
       -extern int         ecankbd(void);
       -extern void         ereshaped(Rectangle);        /* supplied by user */
       -extern void         eflush(unsigned long);
       -extern int         menuhit(int, Mouse*, Menu*);
       +extern Mouse     emouse(void);
       +extern Keystroke     ekbd(void);
       +extern void  pushkbd(int c);
       +extern int   ecanread(unsigned long);
       +extern int   ecanmouse(void);
       +extern int   ecankbd(void);
       +extern void  ereshaped(Rectangle);  /* supplied by user */
       +extern void  eflush(unsigned long);
       +extern int   menuhit(int, Mouse*, Menu*);
        extern Rectangle getrect(int, Mouse*);
        extern unsigned long rgbpix(Bitmap*, RGB);
       -extern void         rdcolmap(Bitmap*, RGB*);
       -extern void         wrcolmap(Bitmap*, RGB*);
       +extern void  rdcolmap(Bitmap*, RGB*);
       +extern void  wrcolmap(Bitmap*, RGB*);
        extern void raisewindow(void);
        
        /* Extra functions supplied by libXg */
       -extern int        snarfswap(char*, int, char**);
       -extern int        scrollfwdbut(void);
       +extern int  snarfswap(char*, int, char**);
       +extern int  scrollfwdbut(void);
        
        enum{
       -        Emouse                = 1,
       -        Ekeyboard        = 2
       +    Emouse      = 1,
       +    Ekeyboard   = 2
        };
        
       -extern Point         Pt(int, int);
       +extern Point     Pt(int, int);
        extern Rectangle Rect(int, int, int, int);
        extern Rectangle Rpt(Point, Point);
        
        
       -#define        Dx(r)        ((r).max.x-(r).min.x)
       -#define        Dy(r)        ((r).max.y-(r).min.y)
       +#define Dx(r)   ((r).max.x-(r).min.x)
       +#define Dy(r)   ((r).max.y-(r).min.y)
        
        
       -extern        Bitmap        screen;
       -extern        XftFont        *font;
       +extern  Bitmap  screen;
       +extern  XftFont *font;
        extern  XftColor fontcolor;
        extern  XftColor bgcolor;
        
       -#define        BGSHORT(p)                (((p)[0]<<0) | ((p)[1]<<8))
       -#define        BGLONG(p)                ((BGSHORT(p)<<0) | (BGSHORT(p+2)<<16))
       -#define        BPSHORT(p, v)                ((p)[0]=(v), (p)[1]=((v)>>8))
       -#define        BPLONG(p, v)                (BPSHORT(p, (v)), BPSHORT(p+2, (v)>>16))
       +#define BGSHORT(p)      (((p)[0]<<0) | ((p)[1]<<8))
       +#define BGLONG(p)       ((BGSHORT(p)<<0) | (BGSHORT(p+2)<<16))
       +#define BPSHORT(p, v)       ((p)[0]=(v), (p)[1]=((v)>>8))
       +#define BPLONG(p, v)        (BPSHORT(p, (v)), BPSHORT(p+2, (v)>>16))
        
        #endif
 (DIR) diff --git a/include/regexp.h b/include/regexp.h
       @@ -1,65 +1,65 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
        
       -typedef struct Resub                Resub;
       -typedef struct Reclass                Reclass;
       -typedef struct Reinst                Reinst;
       -typedef struct Reprog                Reprog;
       +typedef struct Resub        Resub;
       +typedef struct Reclass      Reclass;
       +typedef struct Reinst       Reinst;
       +typedef struct Reprog       Reprog;
        
        /*
       - *        Sub expression matches
       + *  Sub expression matches
         */
        struct Resub{
       -        union
       -        {
       -                char *sp;
       -                Rune *rsp;
       -        }s;
       -        union
       -        {
       -                char *ep;
       -                Rune *rep;
       -        }e;
       +    union
       +    {
       +        char *sp;
       +        Rune *rsp;
       +    }s;
       +    union
       +    {
       +        char *ep;
       +        Rune *rep;
       +    }e;
        };
        
        /*
       - *        character class, each pair of rune's defines a range
       + *  character class, each pair of rune's defines a range
         */
        struct Reclass{
       -        Rune        *end;
       -        Rune        spans[64];
       +    Rune    *end;
       +    Rune    spans[64];
        };
        
        /*
       - *        Machine instructions
       + *  Machine instructions
         */
        struct Reinst{
       -        int        type;
       -        union        {
       -                Reclass        *cp;                /* class pointer */
       -                Rune        r;                /* character */
       -                int        subid;                /* sub-expression id for RBRA and LBRA */
       -                Reinst        *right;                /* right child of OR */
       -        }u1;
       -        union {        /* regexp relies on these two being in the same union */
       -                Reinst *left;                /* left child of OR */
       -                Reinst *next;                /* next instruction for CAT & LBRA */
       -        }u2;
       +    int type;
       +    union   {
       +        Reclass *cp;        /* class pointer */
       +        Rune    r;      /* character */
       +        int subid;      /* sub-expression id for RBRA and LBRA */
       +        Reinst  *right;     /* right child of OR */
       +    }u1;
       +    union { /* regexp relies on these two being in the same union */
       +        Reinst *left;       /* left child of OR */
       +        Reinst *next;       /* next instruction for CAT & LBRA */
       +    }u2;
        };
        
        /*
       - *        Reprogram definition
       + *  Reprogram definition
         */
        struct Reprog{
       -        Reinst        *startinst;        /* start pc */
       -        Reclass        class[16];        /* .data */
       -        Reinst        firstinst[5];        /* .text */
       +    Reinst  *startinst; /* start pc */
       +    Reclass class[16];  /* .data */
       +    Reinst  firstinst[5];   /* .text */
        };
        
       -extern Reprog        *regcomp(char*);
       -extern Reprog        *regcomplit(char*);
       -extern Reprog        *regcompnl(char*);
       -extern void        regerror(char*);
       -extern int        regexec(Reprog*, char*, Resub*, int);
       -extern void        regsub(char*, char*, Resub*, int);
       -extern int        rregexec(Reprog*, Rune*, Resub*, int);
       -extern void        rregsub(Rune*, Rune*, Resub*, int);
       +extern Reprog   *regcomp(char*);
       +extern Reprog   *regcomplit(char*);
       +extern Reprog   *regcompnl(char*);
       +extern void regerror(char*);
       +extern int  regexec(Reprog*, char*, Resub*, int);
       +extern void regsub(char*, char*, Resub*, int);
       +extern int  rregexec(Reprog*, Rune*, Resub*, int);
       +extern void rregsub(Rune*, Rune*, Resub*, int);
 (DIR) diff --git a/libXg/Gwin.h b/libXg/Gwin.h
       @@ -25,13 +25,13 @@ typedef struct _GwinRec *GwinWidget;
        
        /* Type definition for gwin resources */
        typedef struct {
       -                int buttons;
       -                struct {
       -                        int x;
       -                        int y;
       -                } xy;
       -                unsigned long msec;
       -        } Gwinmouse;
       +        int buttons;
       +        struct {
       +            int x;
       +            int y;
       +        } xy;
       +        unsigned long msec;
       +    } Gwinmouse;
        
        typedef void (*Reshapefunc)(int, int, int, int);
        typedef void (*Charfunc)(int, int, int, int, int);
 (DIR) diff --git a/libXg/GwinP.h b/libXg/GwinP.h
       @@ -8,20 +8,20 @@
        
        /* Gwin instance part */
        typedef struct {
       -        /* New resource fields */
       -        Pixel                foreground;
       -        Boolean                forwardr;        /* does right button scroll forward? */
       -        Reshapefunc        reshaped;        /* Notify app of reshape */
       -        Charfunc        gotchar;        /* Notify app of char arrival */
       -        Mousefunc        gotmouse;        /* Notify app of mouse change */
       -        String                selection;        /* Current selection */
       -        int                compose;
       +    /* New resource fields */
       +    Pixel       foreground;
       +    Boolean     forwardr;   /* does right button scroll forward? */
       +    Reshapefunc reshaped;   /* Notify app of reshape */
       +    Charfunc    gotchar;    /* Notify app of char arrival */
       +    Mousefunc   gotmouse;   /* Notify app of mouse change */
       +    String      selection;  /* Current selection */
       +    int     compose;
        } GwinPart;
        
        /* Full instance record */
        typedef struct _GwinRec {
       -        CorePart        core;
       -        GwinPart        gwin;
       +    CorePart    core;
       +    GwinPart    gwin;
        } GwinRec;
        
        /* New type for class methods */
       @@ -29,14 +29,14 @@ typedef String (*SelSwapProc)(Widget, String);
        
        /* Class part */
        typedef struct {
       -        SelSwapProc        select_swap;
       -        XtPointer        extension;
       +    SelSwapProc select_swap;
       +    XtPointer   extension;
        } GwinClassPart;
        
        /* Full class record */
        typedef struct _GwinClassRec {
       -        CoreClassPart        core_class;
       -        GwinClassPart        gwin_class;
       +    CoreClassPart   core_class;
       +    GwinClassPart   gwin_class;
        } GwinClassRec, *GwinWidgetClass;
        
        /* External definition for class record */
 (DIR) diff --git a/libXg/arc.c b/libXg/arc.c
       @@ -10,36 +10,36 @@
        void
        arc(Bitmap *b, Point p0, Point p1, Point p2, int v, Fcode f)
        {
       -        unsigned int d;
       -        int x, y, r, start, end, delta;
       -        GC g;
       +    unsigned int d;
       +    int x, y, r, start, end, delta;
       +    GC g;
        
       -        p1.x -= p0.x;
       -        p1.y -= p0.y;
       -        p2.x -= p0.x;
       -        p2.y -= p0.y;
       -        r = (int)sqrt((double)(p1.x*p1.x + p1.y*p1.y));
       -        start = (int)(64*rad2deg(atan2(-p2.y, p2.x)));
       -        end = (int)(64*rad2deg(atan2(-p1.y, p1.x)));
       -        if(start < 0)
       -                start += 64*360;
       -        if(end < 0)
       -                end += 64*360;
       -        delta = end - start;
       -        if(delta < 0)
       -                delta += 64*360;
       -        x = p0.x - r;
       -        y = p0.y - r;
       -        if(b->flag&SHIFT){
       -                x -= b->r.min.x;
       -                y -= b->r.min.y;
       -        }
       -        d = 2*r;
       -        g = _getfillgc(f, b, v);
       -        /*
       -         * delta is positive, so this draws counterclockwise arc
       -         * from start to start+delta
       -         */
       -        XDrawArc(_dpy, (Drawable)b->id, g, x, y, d, d, start, delta);
       +    p1.x -= p0.x;
       +    p1.y -= p0.y;
       +    p2.x -= p0.x;
       +    p2.y -= p0.y;
       +    r = (int)sqrt((double)(p1.x*p1.x + p1.y*p1.y));
       +    start = (int)(64*rad2deg(atan2(-p2.y, p2.x)));
       +    end = (int)(64*rad2deg(atan2(-p1.y, p1.x)));
       +    if(start < 0)
       +        start += 64*360;
       +    if(end < 0)
       +        end += 64*360;
       +    delta = end - start;
       +    if(delta < 0)
       +        delta += 64*360;
       +    x = p0.x - r;
       +    y = p0.y - r;
       +    if(b->flag&SHIFT){
       +        x -= b->r.min.x;
       +        y -= b->r.min.y;
       +    }
       +    d = 2*r;
       +    g = _getfillgc(f, b, v);
       +    /*
       +     * delta is positive, so this draws counterclockwise arc
       +     * from start to start+delta
       +     */
       +    XDrawArc(_dpy, (Drawable)b->id, g, x, y, d, d, start, delta);
        }
        
 (DIR) diff --git a/libXg/arith.c b/libXg/arith.c
       @@ -6,190 +6,190 @@
        Point
        add(Point a, Point b)
        {
       -        a.x += b.x;
       -        a.y += b.y;
       -        return a;
       +    a.x += b.x;
       +    a.y += b.y;
       +    return a;
        }
        
        Point
        sub(Point a, Point b)
        {
       -        a.x -= b.x;
       -        a.y -= b.y;
       -        return a;
       +    a.x -= b.x;
       +    a.y -= b.y;
       +    return a;
        }
        
        Rectangle
        inset(Rectangle r, int n)
        {
       -        r.min.x += n;
       -        r.min.y += n;
       -        r.max.x -= n;
       -        r.max.y -= n;
       -        return r;
       +    r.min.x += n;
       +    r.min.y += n;
       +    r.max.x -= n;
       +    r.max.y -= n;
       +    return r;
        }
        
        Point
        divpt(Point a, int b)
        {
       -        a.x /= b;
       -        a.y /= b;
       -        return a;
       +    a.x /= b;
       +    a.y /= b;
       +    return a;
        }
        
        Point
        mul(Point a, int b)
        {
       -        a.x *= b;
       -        a.y *= b;
       -        return a;
       +    a.x *= b;
       +    a.y *= b;
       +    return a;
        }
        
        Rectangle
        rsubp(Rectangle r, Point p)
        {
       -        r.min.x -= p.x;
       -        r.min.y -= p.y;
       -        r.max.x -= p.x;
       -        r.max.y -= p.y;
       -        return r;
       +    r.min.x -= p.x;
       +    r.min.y -= p.y;
       +    r.max.x -= p.x;
       +    r.max.y -= p.y;
       +    return r;
        }
        
        Rectangle
        raddp(Rectangle r, Point p)
        {
       -        r.min.x += p.x;
       -        r.min.y += p.y;
       -        r.max.x += p.x;
       -        r.max.y += p.y;
       -        return r;
       +    r.min.x += p.x;
       +    r.min.y += p.y;
       +    r.max.x += p.x;
       +    r.max.y += p.y;
       +    return r;
        }
        
        Rectangle
        rmul(Rectangle r, int a)
        {
       -        if (a != 1) {
       -                r.min.x *= a;
       -                r.min.y *= a;
       -                r.max.x *= a;
       -                r.max.y *= a;
       -        }
       -        return r;
       +    if (a != 1) {
       +        r.min.x *= a;
       +        r.min.y *= a;
       +        r.max.x *= a;
       +        r.max.y *= a;
       +    }
       +    return r;
        }
        
        Rectangle
        rdiv(Rectangle r, int a)
        {
       -        if (a != 1) {
       -                r.min.x /= a;
       -                r.min.y /= a;
       -                r.max.x /= a;
       -                r.max.y /= a;
       -        }
       -        return r;
       +    if (a != 1) {
       +        r.min.x /= a;
       +        r.min.y /= a;
       +        r.max.x /= a;
       +        r.max.y /= a;
       +    }
       +    return r;
        }
        
        Rectangle
        rshift(Rectangle r, int a)
        {
       -        if (a > 0) {
       -                r.min.x <<= a;
       -                r.min.y <<= a;
       -                r.max.x <<= a;
       -                r.max.y <<= a;
       -        }
       -        else if (a < 0) {
       -                a = -a;
       -                r.min.x >>= a;
       -                r.min.y >>= a;
       -                r.max.x >>= a;
       -                r.max.y >>= a;
       -        }
       -        return r;
       +    if (a > 0) {
       +        r.min.x <<= a;
       +        r.min.y <<= a;
       +        r.max.x <<= a;
       +        r.max.y <<= a;
       +    }
       +    else if (a < 0) {
       +        a = -a;
       +        r.min.x >>= a;
       +        r.min.y >>= a;
       +        r.max.x >>= a;
       +        r.max.y >>= a;
       +    }
       +    return r;
        }
        
        int
        eqpt(Point p, Point q)
        {
       -        return p.x==q.x && p.y==q.y;
       +    return p.x==q.x && p.y==q.y;
        }
        
        int
        eqrect(Rectangle r, Rectangle s)
        {
       -        return r.min.x==s.min.x && r.max.x==s.max.x &&
       -               r.min.y==s.min.y && r.max.y==s.max.y;
       +    return r.min.x==s.min.x && r.max.x==s.max.x &&
       +           r.min.y==s.min.y && r.max.y==s.max.y;
        }
        
        int
        rectXrect(Rectangle r, Rectangle s)
        {
       -        return r.min.x<s.max.x && s.min.x<r.max.x &&
       -               r.min.y<s.max.y && s.min.y<r.max.y;
       +    return r.min.x<s.max.x && s.min.x<r.max.x &&
       +           r.min.y<s.max.y && s.min.y<r.max.y;
        }
        
        int
        rectinrect(Rectangle r, Rectangle s)
        {
       -        /* !ptinrect(r.min, s) in line for speed */
       -        if(!(r.min.x>=s.min.x && r.min.x<s.max.x &&
       -                r.min.y>=s.min.y && r.min.y<s.max.y))
       -                        return 0;
       -        return r.max.x<=s.max.x && r.max.y<=s.max.y;
       +    /* !ptinrect(r.min, s) in line for speed */
       +    if(!(r.min.x>=s.min.x && r.min.x<s.max.x &&
       +        r.min.y>=s.min.y && r.min.y<s.max.y))
       +            return 0;
       +    return r.max.x<=s.max.x && r.max.y<=s.max.y;
        }
        
        int
        ptinrect(Point p, Rectangle r)
        {
       -        return p.x>=r.min.x && p.x<r.max.x &&
       -               p.y>=r.min.y && p.y<r.max.y;
       +    return p.x>=r.min.x && p.x<r.max.x &&
       +           p.y>=r.min.y && p.y<r.max.y;
        }
        
        Rectangle
        rcanon(Rectangle r)
        {
       -        int t;
       -        if (r.max.x < r.min.x) {
       -                t = r.min.x;
       -                r.min.x = r.max.x;
       -                r.max.x = t;
       -        }
       -        if (r.max.y < r.min.y) {
       -                t = r.min.y;
       -                r.min.y = r.max.y;
       -                r.max.y = t;
       -        }
       -        return r;
       +    int t;
       +    if (r.max.x < r.min.x) {
       +        t = r.min.x;
       +        r.min.x = r.max.x;
       +        r.max.x = t;
       +    }
       +    if (r.max.y < r.min.y) {
       +        t = r.min.y;
       +        r.min.y = r.max.y;
       +        r.max.y = t;
       +    }
       +    return r;
        }
        
        Rectangle
        Rect(int x1, int y1, int x2, int y2)
        {
       -        Rectangle r;
       +    Rectangle r;
        
       -        r.min.x = x1;
       -        r.min.y = y1;
       -        r.max.x = x2;
       -        r.max.y = y2;
       -        return r;
       +    r.min.x = x1;
       +    r.min.y = y1;
       +    r.max.x = x2;
       +    r.max.y = y2;
       +    return r;
        }
        
        Rectangle
        Rpt(Point p1, Point p2)
        {
       -        Rectangle r;
       +    Rectangle r;
        
       -        r.min = p1;
       -        r.max = p2;
       -        return r;
       +    r.min = p1;
       +    r.max = p2;
       +    return r;
        }
        
        Point
        Pt(int x, int y)
        {
       -        Point p;
       +    Point p;
        
       -        p.x = x;
       -        p.y = y;
       -        return p;
       +    p.x = x;
       +    p.y = y;
       +    return p;
        }
 (DIR) diff --git a/libXg/balloc.c b/libXg/balloc.c
       @@ -7,49 +7,49 @@
        Bitmap*
        balloc(Rectangle r, int ldepth)
        {
       -        Bitmap *b;
       +    Bitmap *b;
        
       -        b = _balloc(r, ldepth);
       -        bitblt(b, r.min, b, r, Zero);
       -        return b;
       +    b = _balloc(r, ldepth);
       +    bitblt(b, r.min, b, r, Zero);
       +    return b;
        }
        
        Bitmap*
        _balloc(Rectangle r, int ldepth)
        {
       -        int id;
       -        Bitmap *b;
       -        int ld;
       -        Rectangle rx;
       +    int id;
       +    Bitmap *b;
       +    int ld;
       +    Rectangle rx;
        
       -        b = (Bitmap *)calloc(1, sizeof(Bitmap));
       -        if(b == 0)
       -                berror("balloc malloc");
       -        if (ldepth == 0)
       -                ld = 0;
       -        else
       -                ld = screen.ldepth;
       -        rx = r;
       -        if (Dx(rx) == 0)
       -                rx.max.x++;
       -        if (Dy(rx) == 0)
       -                rx.max.y++;
       -        id = (int) XCreatePixmap(_dpy, (Drawable)screen.id,
       -                        Dx(rx), Dy(rx), _ld2d[ld]);
       -        b->ldepth = ldepth;
       -        b->r = r;
       -        b->clipr = r;
       -        b->id = id;
       -        b->cache = 0;
       -        if(ldepth == 0)
       -                b->flag = DP1|BL1;
       -        else
       -                b->flag = screen.flag&BL1;
       -        if(r.min.x==0 && r.min.y ==0)
       -                b->flag |= ZORG;
       -        else
       -                b->flag |= SHIFT;
       -        return b;
       +    b = (Bitmap *)calloc(1, sizeof(Bitmap));
       +    if(b == 0)
       +        berror("balloc malloc");
       +    if (ldepth == 0)
       +        ld = 0;
       +    else
       +        ld = screen.ldepth;
       +    rx = r;
       +    if (Dx(rx) == 0)
       +        rx.max.x++;
       +    if (Dy(rx) == 0)
       +        rx.max.y++;
       +    id = (int) XCreatePixmap(_dpy, (Drawable)screen.id,
       +            Dx(rx), Dy(rx), _ld2d[ld]);
       +    b->ldepth = ldepth;
       +    b->r = r;
       +    b->clipr = r;
       +    b->id = id;
       +    b->cache = 0;
       +    if(ldepth == 0)
       +        b->flag = DP1|BL1;
       +    else
       +        b->flag = screen.flag&BL1;
       +    if(r.min.x==0 && r.min.y ==0)
       +        b->flag |= ZORG;
       +    else
       +        b->flag |= SHIFT;
       +    return b;
        }
        
        void
       @@ -57,6 +57,6 @@ bfree(Bitmap *b)
        {
            if (b->fd)
                XftDrawDestroy(b->fd);
       -        XFreePixmap(_dpy, (Pixmap)b->id);
       -        free(b);
       +    XFreePixmap(_dpy, (Pixmap)b->id);
       +    free(b);
        }
 (DIR) diff --git a/libXg/bitblt.c b/libXg/bitblt.c
       @@ -13,10 +13,10 @@ bitblt(Bitmap *d, Point p, Bitmap *s, Rectangle r, Fcode f)
        void
        bitblt2(Bitmap *d, Point p, Bitmap *s, Rectangle r, Fcode f, unsigned long fg, unsigned long bg)
        {
       -        int sx, sy, dx, dy, bfunc;
       -        GC g;
       -        unsigned long plane;
       -        Bitmap *btmp;
       +    int sx, sy, dx, dy, bfunc;
       +    GC g;
       +    unsigned long plane;
       +    Bitmap *btmp;
        
            if (fg == 0)
                fg = _fgpixel;
       @@ -24,48 +24,48 @@ bitblt2(Bitmap *d, Point p, Bitmap *s, Rectangle r, Fcode f, unsigned long fg, u
            if (bg == 0)
                bg = _bgpixel;
        
       -        if(Dx(r)<=0 || Dy(r)<=0)
       -                return;
       -        sx = r.min.x;
       -        sy = r.min.y;
       -        if(s->flag&SHIFT){
       -                sx -= s->r.min.x;
       -                sy -= s->r.min.y;
       -        }
       -        dx = p.x;
       -        dy = p.y;
       -        if(d->flag&SHIFT){
       -                dx -= d->r.min.x;
       -                dy -= d->r.min.y;
       -        }
       -        g = _getcopygc2(f, d, s, &bfunc, fg, bg);
       -        if(bfunc == UseCopyArea)
       -                XCopyArea(_dpy, (Drawable)s->id, (Drawable)d->id, g,
       -                        sx, sy, Dx(r), Dy(r), dx, dy);
       -        else if(bfunc == UseFillRectangle){
       -                XFillRectangle(_dpy, (Drawable)d->id, g,
       -                        dx, dy, Dx(r), Dy(r));
       -        }else{
       -                /* bfunc == UseCopyPlane */
       -                plane = _ld2dmask[s->ldepth];
       -                plane &= ~(plane>>1);
       -                if(0/*f == S*/)
       -                        XCopyPlane(_dpy, (Drawable)s->id, (Drawable)d->id, g,
       -                                sx, sy, Dx(r), Dy(r), dx, dy, plane);
       -                else {
       -                        /*
       -                         * CopyPlane can only do func code S,
       -                         * so copy src rect into a bitmap with the same depth
       -                         * as the dest, then do the bitblt from the tmp.
       -                         * This won't recurse again because we only get
       -                         * UseCopyPlane with differing bitmap depths
       -                         */
       -                        btmp = _balloc(Rect(0,0,Dx(r),Dy(r)), d->ldepth);
       -                        XCopyPlane(_dpy, (Drawable)s->id, (Drawable)btmp->id, g,
       -                                sx, sy, Dx(r), Dy(r), 0, 0, plane);
       -                        bitblt(d, p, btmp, btmp->r, f);
       -                        bfree(btmp);
       -                }
       -        }
       -        XFlush(_dpy);
       +    if(Dx(r)<=0 || Dy(r)<=0)
       +        return;
       +    sx = r.min.x;
       +    sy = r.min.y;
       +    if(s->flag&SHIFT){
       +        sx -= s->r.min.x;
       +        sy -= s->r.min.y;
       +    }
       +    dx = p.x;
       +    dy = p.y;
       +    if(d->flag&SHIFT){
       +        dx -= d->r.min.x;
       +        dy -= d->r.min.y;
       +    }
       +    g = _getcopygc2(f, d, s, &bfunc, fg, bg);
       +    if(bfunc == UseCopyArea)
       +        XCopyArea(_dpy, (Drawable)s->id, (Drawable)d->id, g,
       +            sx, sy, Dx(r), Dy(r), dx, dy);
       +    else if(bfunc == UseFillRectangle){
       +        XFillRectangle(_dpy, (Drawable)d->id, g,
       +            dx, dy, Dx(r), Dy(r));
       +    }else{
       +        /* bfunc == UseCopyPlane */
       +        plane = _ld2dmask[s->ldepth];
       +        plane &= ~(plane>>1);
       +        if(0/*f == S*/)
       +            XCopyPlane(_dpy, (Drawable)s->id, (Drawable)d->id, g,
       +                sx, sy, Dx(r), Dy(r), dx, dy, plane);
       +        else {
       +            /*
       +             * CopyPlane can only do func code S,
       +             * so copy src rect into a bitmap with the same depth
       +             * as the dest, then do the bitblt from the tmp.
       +             * This won't recurse again because we only get
       +             * UseCopyPlane with differing bitmap depths
       +             */
       +            btmp = _balloc(Rect(0,0,Dx(r),Dy(r)), d->ldepth);
       +            XCopyPlane(_dpy, (Drawable)s->id, (Drawable)btmp->id, g,
       +                sx, sy, Dx(r), Dy(r), 0, 0, plane);
       +            bitblt(d, p, btmp, btmp->r, f);
       +            bfree(btmp);
       +        }
       +    }
       +    XFlush(_dpy);
        }
 (DIR) diff --git a/libXg/bitbltclip.c b/libXg/bitbltclip.c
       @@ -7,62 +7,62 @@
        int
        bitbltclip(void *vp)
        {
       -        int dx, dy;
       -        int i;
       -        struct bbcarg{
       -                Bitmap *dm;
       -                Point p;
       -                Bitmap *sm;
       -                Rectangle r;
       -                Fcode f;
       -        }*bp;
       +    int dx, dy;
       +    int i;
       +    struct bbcarg{
       +        Bitmap *dm;
       +        Point p;
       +        Bitmap *sm;
       +        Rectangle r;
       +        Fcode f;
       +    }*bp;
        
       -        bp = (struct bbcarg *)vp;
       -        dx = Dx(bp->r);
       -        dy = Dy(bp->r);
       -        if(bp->p.x < bp->dm->clipr.min.x){
       -                i = bp->dm->clipr.min.x-bp->p.x;
       -                bp->r.min.x += i;
       -                bp->p.x += i;
       -                dx -= i;
       -        }
       -        if(bp->p.y < bp->dm->clipr.min.y){
       -                i = bp->dm->clipr.min.y-bp->p.y;
       -                bp->r.min.y += i;
       -                bp->p.y += i;
       -                dy -= i;
       -        }
       -        if(bp->p.x+dx > bp->dm->clipr.max.x){
       -                i = bp->p.x+dx-bp->dm->clipr.max.x;
       -                bp->r.max.x -= i;
       -                dx -= i;
       -        }
       -        if(bp->p.y+dy > bp->dm->clipr.max.y){
       -                i = bp->p.y+dy-bp->dm->clipr.max.y;
       -                bp->r.max.y -= i;
       -                dy -= i;
       -        }
       -        if(bp->r.min.x < bp->sm->clipr.min.x){
       -                i = bp->sm->clipr.min.x-bp->r.min.x;
       -                bp->p.x += i;
       -                bp->r.min.x += i;
       -                dx -= i;
       -        }
       -        if(bp->r.min.y < bp->sm->clipr.min.y){
       -                i = bp->sm->clipr.min.y-bp->r.min.y;
       -                bp->p.y += i;
       -                bp->r.min.y += i;
       -                dy -= i;
       -        }
       -        if(bp->r.max.x > bp->sm->clipr.max.x){
       -                i = bp->r.max.x-bp->sm->clipr.max.x;
       -                bp->r.max.x -= i;
       -                dx -= i;
       -        }
       -        if(bp->r.max.y > bp->sm->clipr.max.y){
       -                i = bp->r.max.y-bp->sm->clipr.max.y;
       -                bp->r.max.y -= i;
       -                dy -= i;
       -        }
       -        return dx>0 && dy>0;
       +    bp = (struct bbcarg *)vp;
       +    dx = Dx(bp->r);
       +    dy = Dy(bp->r);
       +    if(bp->p.x < bp->dm->clipr.min.x){
       +        i = bp->dm->clipr.min.x-bp->p.x;
       +        bp->r.min.x += i;
       +        bp->p.x += i;
       +        dx -= i;
       +    }
       +    if(bp->p.y < bp->dm->clipr.min.y){
       +        i = bp->dm->clipr.min.y-bp->p.y;
       +        bp->r.min.y += i;
       +        bp->p.y += i;
       +        dy -= i;
       +    }
       +    if(bp->p.x+dx > bp->dm->clipr.max.x){
       +        i = bp->p.x+dx-bp->dm->clipr.max.x;
       +        bp->r.max.x -= i;
       +        dx -= i;
       +    }
       +    if(bp->p.y+dy > bp->dm->clipr.max.y){
       +        i = bp->p.y+dy-bp->dm->clipr.max.y;
       +        bp->r.max.y -= i;
       +        dy -= i;
       +    }
       +    if(bp->r.min.x < bp->sm->clipr.min.x){
       +        i = bp->sm->clipr.min.x-bp->r.min.x;
       +        bp->p.x += i;
       +        bp->r.min.x += i;
       +        dx -= i;
       +    }
       +    if(bp->r.min.y < bp->sm->clipr.min.y){
       +        i = bp->sm->clipr.min.y-bp->r.min.y;
       +        bp->p.y += i;
       +        bp->r.min.y += i;
       +        dy -= i;
       +    }
       +    if(bp->r.max.x > bp->sm->clipr.max.x){
       +        i = bp->r.max.x-bp->sm->clipr.max.x;
       +        bp->r.max.x -= i;
       +        dx -= i;
       +    }
       +    if(bp->r.max.y > bp->sm->clipr.max.y){
       +        i = bp->r.max.y-bp->sm->clipr.max.y;
       +        bp->r.max.y -= i;
       +        dy -= i;
       +    }
       +    return dx>0 && dy>0;
        }
 (DIR) diff --git a/libXg/border.c b/libXg/border.c
       @@ -8,23 +8,23 @@ extern unsigned long _borderpixel;
        void
        border(Bitmap *l, Rectangle r, int i, Fcode c, unsigned long bg)
        {
       -        if(i > 0){
       -                bitblt2(l, r.min,
       -                        l, Rect(r.min.x, r.min.y, r.max.x, r.min.y+i), c, _borderpixel, bg);
       -                bitblt2(l, Pt(r.min.x, r.max.y-i),
       -                        l, Rect(r.min.x, r.max.y-i, r.max.x, r.max.y), c, _borderpixel, bg);
       -                bitblt2(l, Pt(r.min.x, r.min.y+i),
       -                        l, Rect(r.min.x, r.min.y+i, r.min.x+i, r.max.y-i), c, _borderpixel, bg);
       -                bitblt2(l, Pt(r.max.x-i, r.min.y+i),
       -                        l, Rect(r.max.x-i, r.min.y+i, r.max.x, r.max.y-i), c, _borderpixel, bg);
       -        }else if(i < 0){
       -                bitblt2(l, Pt(r.min.x, r.min.y+i),
       -                        l, Rect(r.min.x, r.min.y+i, r.max.x, r.min.y), c, _borderpixel, bg);
       -                bitblt2(l, Pt(r.min.x, r.max.y),
       -                        l, Rect(r.min.x, r.max.y, r.max.x, r.max.y-i), c, _borderpixel, bg);
       -                bitblt2(l, Pt(r.min.x+i, r.min.y+i),
       -                        l, Rect(r.min.x+i, r.min.y+i, r.min.x, r.max.y-i), c, _borderpixel, bg);
       -                bitblt2(l, Pt(r.max.x, r.min.y+i),
       -                        l, Rect(r.max.x, r.min.y+i, r.max.x-i, r.max.y-i), c, _borderpixel, bg);
       -        }
       +    if(i > 0){
       +        bitblt2(l, r.min,
       +            l, Rect(r.min.x, r.min.y, r.max.x, r.min.y+i), c, _borderpixel, bg);
       +        bitblt2(l, Pt(r.min.x, r.max.y-i),
       +            l, Rect(r.min.x, r.max.y-i, r.max.x, r.max.y), c, _borderpixel, bg);
       +        bitblt2(l, Pt(r.min.x, r.min.y+i),
       +            l, Rect(r.min.x, r.min.y+i, r.min.x+i, r.max.y-i), c, _borderpixel, bg);
       +        bitblt2(l, Pt(r.max.x-i, r.min.y+i),
       +            l, Rect(r.max.x-i, r.min.y+i, r.max.x, r.max.y-i), c, _borderpixel, bg);
       +    }else if(i < 0){
       +        bitblt2(l, Pt(r.min.x, r.min.y+i),
       +            l, Rect(r.min.x, r.min.y+i, r.max.x, r.min.y), c, _borderpixel, bg);
       +        bitblt2(l, Pt(r.min.x, r.max.y),
       +            l, Rect(r.min.x, r.max.y, r.max.x, r.max.y-i), c, _borderpixel, bg);
       +        bitblt2(l, Pt(r.min.x+i, r.min.y+i),
       +            l, Rect(r.min.x+i, r.min.y+i, r.min.x, r.max.y-i), c, _borderpixel, bg);
       +        bitblt2(l, Pt(r.max.x, r.min.y+i),
       +            l, Rect(r.max.x, r.min.y+i, r.max.x-i, r.max.y-i), c, _borderpixel, bg);
       +    }
        }
 (DIR) diff --git a/libXg/bscreenrect.c b/libXg/bscreenrect.c
       @@ -12,7 +12,7 @@
        Rectangle
        bscreenrect(Rectangle *clipr)
        {
       -        if(clipr)
       -                *clipr = screen.clipr;
       -        return screen.r;
       +    if(clipr)
       +        *clipr = screen.clipr;
       +    return screen.r;
        }
 (DIR) diff --git a/libXg/circle.c b/libXg/circle.c
       @@ -7,17 +7,17 @@
        void
        circle(Bitmap *b, Point p, int r, int v, Fcode f)
        {
       -        unsigned int d;
       -        int x, y;
       -        GC g;
       +    unsigned int d;
       +    int x, y;
       +    GC g;
        
       -        x = p.x - r;
       -        y = p.y - r;
       -        if (b->flag&SHIFT){
       -                x -= b->r.min.x;
       -                y -= b->r.min.y;
       -        }
       -        d = 2*r;
       -        g = _getfillgc(f, b, v);
       -        XDrawArc(_dpy, (Drawable)b->id, g, x, y, d, d, 0, 23040/* 360 deg */);
       +    x = p.x - r;
       +    y = p.y - r;
       +    if (b->flag&SHIFT){
       +        x -= b->r.min.x;
       +        y -= b->r.min.y;
       +    }
       +    d = 2*r;
       +    g = _getfillgc(f, b, v);
       +    XDrawArc(_dpy, (Drawable)b->id, g, x, y, d, d, 0, 23040/* 360 deg */);
        }
 (DIR) diff --git a/libXg/clipline.c b/libXg/clipline.c
       @@ -6,220 +6,220 @@
        
        typedef struct Linedesc
        {
       -        int        x0;
       -        int        y0;
       -        char        xmajor;
       -        char        slopeneg;
       -        long        dminor;
       -        long        dmajor;
       +    int x0;
       +    int y0;
       +    char    xmajor;
       +    char    slopeneg;
       +    long    dminor;
       +    long    dmajor;
        } Linedesc;
        
       -int         _clipline(Rectangle, Point*, Point*, Linedesc*);
       +int  _clipline(Rectangle, Point*, Point*, Linedesc*);
        
       -#define        XYswap(p)        t=(p)->x, (p)->x=(p)->y, (p)->y=t
       -#define        Swap(x, y)        t=x, x=y, y=t
       +#define XYswap(p)   t=(p)->x, (p)->x=(p)->y, (p)->y=t
       +#define Swap(x, y)  t=x, x=y, y=t
        
        static long
       -lfloor(long x, long y)        /* first integer <= x/y */
       +lfloor(long x, long y)  /* first integer <= x/y */
        {
       -        if(y <= 0){
       -                if(y == 0)
       -                        return x;
       -                y = -y;
       -                x = -x;
       -        }
       -        if(x < 0){        /* be careful; C div. is undefined */
       -                x = -x;
       -                x += y-1;
       -                return -(x/y);
       -        }
       -        return x/y;
       +    if(y <= 0){
       +        if(y == 0)
       +            return x;
       +        y = -y;
       +        x = -x;
       +    }
       +    if(x < 0){  /* be careful; C div. is undefined */
       +        x = -x;
       +        x += y-1;
       +        return -(x/y);
       +    }
       +    return x/y;
        }
        
        static long
       -lceil(long x, long y)        /* first integer >= x/y */
       +lceil(long x, long y)   /* first integer >= x/y */
        {
       -        if(y <= 0){
       -                if(y == 0)
       -                        return x;
       -                y = -y;
       -                x = -x;
       -        }
       -        if(x < 0){
       -                x = -x;
       -                return -(x/y);
       -        }
       -        x += y-1;
       -        return x/y;
       +    if(y <= 0){
       +        if(y == 0)
       +            return x;
       +        y = -y;
       +        x = -x;
       +    }
       +    if(x < 0){
       +        x = -x;
       +        return -(x/y);
       +    }
       +    x += y-1;
       +    return x/y;
        }
        
        int
        _gminor(long x, Linedesc *l)
        {
       -        long y;
       +    long y;
        
       -        y = 2*(x-l->x0)*l->dminor + l->dmajor;
       -        y = lfloor(y, 2*l->dmajor) + l->y0;
       -        return l->slopeneg? -y : y;
       +    y = 2*(x-l->x0)*l->dminor + l->dmajor;
       +    y = lfloor(y, 2*l->dmajor) + l->y0;
       +    return l->slopeneg? -y : y;
        }
        
        int
        _gmajor(long y, Linedesc *l)
        {
       -        long x;
       -
       -        x = 2*((l->slopeneg? -y : y)-l->y0)*l->dmajor - l->dminor;
       -        x = lceil(x, 2*l->dminor) + l->x0;
       -        if(l->dminor)
       -                while(_gminor(x-1, l) == y)
       -                        x--;
       -        return x;
       +    long x;
       +
       +    x = 2*((l->slopeneg? -y : y)-l->y0)*l->dmajor - l->dminor;
       +    x = lceil(x, 2*l->dminor) + l->x0;
       +    if(l->dminor)
       +        while(_gminor(x-1, l) == y)
       +            x--;
       +    return x;
        }
        
        void
        gsetline(Point *pp0, Point *pp1, Linedesc *l)
        {
       -        long dx, dy, t;
       -        Point endpt;
       -        int swapped;
       -        Point p0, p1;
       -
       -        swapped = 0;
       -        p0 = *pp0;
       -        p1 = *pp1;
       -        l->xmajor = 1;
       -        l->slopeneg = 0;
       -        dx = p1.x - p0.x;
       -        dy = p1.y - p0.y;
       -        if(abs(dy) > abs(dx)){        /* Steep */
       -                l->xmajor = 0;
       -                XYswap(&p0);
       -                XYswap(&p1);
       -                Swap(dx, dy);
       -        }
       -        if(dx < 0){
       -                swapped++;
       -                Swap(p0.x, p1.x);
       -                Swap(p0.y, p1.y);
       -                dx = -dx;
       -                dy = -dy;
       -        }
       -        if(dy < 0){
       -                l->slopeneg = 1;
       -                dy = -dy;
       -                p0.y = -p0.y;
       -        }
       -        l->dminor = dy;
       -        l->dmajor = dx;
       -        l->x0 = p0.x;
       -        l->y0 = p0.y;
       -        p1.x = swapped? p0.x+1 : p1.x-1;
       -        p1.y = _gminor(p1.x, l);
       -        if(l->xmajor == 0){
       -                XYswap(&p0);
       -                XYswap(&p1);
       -        }
       -        if(pp0->x > pp1->x){
       -                *pp1 = *pp0;
       -                *pp0 = p1;
       -        }else
       -                *pp1 = p1;
       +    long dx, dy, t;
       +    Point endpt;
       +    int swapped;
       +    Point p0, p1;
       +
       +    swapped = 0;
       +    p0 = *pp0;
       +    p1 = *pp1;
       +    l->xmajor = 1;
       +    l->slopeneg = 0;
       +    dx = p1.x - p0.x;
       +    dy = p1.y - p0.y;
       +    if(abs(dy) > abs(dx)){  /* Steep */
       +        l->xmajor = 0;
       +        XYswap(&p0);
       +        XYswap(&p1);
       +        Swap(dx, dy);
       +    }
       +    if(dx < 0){
       +        swapped++;
       +        Swap(p0.x, p1.x);
       +        Swap(p0.y, p1.y);
       +        dx = -dx;
       +        dy = -dy;
       +    }
       +    if(dy < 0){
       +        l->slopeneg = 1;
       +        dy = -dy;
       +        p0.y = -p0.y;
       +    }
       +    l->dminor = dy;
       +    l->dmajor = dx;
       +    l->x0 = p0.x;
       +    l->y0 = p0.y;
       +    p1.x = swapped? p0.x+1 : p1.x-1;
       +    p1.y = _gminor(p1.x, l);
       +    if(l->xmajor == 0){
       +        XYswap(&p0);
       +        XYswap(&p1);
       +    }
       +    if(pp0->x > pp1->x){
       +        *pp1 = *pp0;
       +        *pp0 = p1;
       +    }else
       +        *pp1 = p1;
        }
        /*
         * Modified clip-to-rectangle algorithm
       - *        works in bitmaps
       - *        Everything in SCREEN coordinates.
       + *  works in bitmaps
       + *  Everything in SCREEN coordinates.
         *
       - *        Newman & Sproull 124 (1st edition)
       + *  Newman & Sproull 124 (1st edition)
         */
        
        static int
        code(Point *p, Rectangle *r)
        {
       -        return( (p->x<r->min.x? 1 : p->x>=r->max.x? 2 : 0) |
       -                (p->y<r->min.y? 4 : p->y>=r->max.y? 8 : 0));
       +    return( (p->x<r->min.x? 1 : p->x>=r->max.x? 2 : 0) |
       +        (p->y<r->min.y? 4 : p->y>=r->max.y? 8 : 0));
        }
        
        int
        clipline(Rectangle r, Point *p0, Point *p1)
        {
       -        Linedesc l;
       +    Linedesc l;
        
       -        return _clipline(r, p0, p1, &l);
       +    return _clipline(r, p0, p1, &l);
        }
        
        int
        _clipline(Rectangle r, Point *p0, Point *p1, Linedesc *l)
        {
       -        int c0, c1, n;
       -        long t, ret;
       -        Point temp;
       -        int swapped;
       -
       -        if(p0->x==p1->x && p0->y==p1->y)
       -                return 0;
       -        gsetline(p0, p1, l);
       -        /* line is now closed */
       -        if(l->xmajor == 0){
       -                XYswap(p0);
       -                XYswap(p1);
       -                XYswap(&r.min);
       -                XYswap(&r.max);
       -        }
       -        c0 = code(p0, &r);
       -        c1 = code(p1, &r);
       -        ret = 1;
       -        swapped = 0;
       -        n = 0;
       -        while(c0 | c1){
       -                if(c0 & c1){        /* no point of line in r */
       -                        ret = 0;
       -                        goto Return;
       -                }
       -                if(++n > 10){        /* horrible points; overflow etc. etc. */
       -                        ret = 0;
       -                        goto Return;
       -                }
       -                if(c0 == 0){        /* swap points */
       -                        temp = *p0;
       -                        *p0 = *p1;
       -                        *p1 = temp;
       -                        Swap(c0, c1);
       -                        swapped ^= 1;
       -                }
       -                if(c0 == 0)
       -                        break;
       -                if(c0 & 1){                /* push towards left edge */
       -                        p0->x = r.min.x;
       -                        p0->y = _gminor(p0->x, l);
       -                }else if(c0 & 2){        /* push towards right edge */
       -                        p0->x = r.max.x-1;
       -                        p0->y = _gminor(p0->x, l);
       -                }else if(c0 & 4){        /* push towards top edge */
       -                        p0->y = r.min.y;
       -                        if(l->slopeneg)
       -                                p0->x = _gmajor(p0->y-1, l)-1;
       -                        else
       -                                p0->x = _gmajor(p0->y, l);
       -                }else if(c0 & 8){        /* push towards bottom edge */
       -                        p0->y = r.max.y-1;
       -                        if(l->slopeneg)
       -                                p0->x = _gmajor(p0->y, l);
       -                        else
       -                                p0->x = _gmajor(p0->y+1, l)-1;
       -                }
       -                c0 = code(p0, &r);
       -        }
       +    int c0, c1, n;
       +    long t, ret;
       +    Point temp;
       +    int swapped;
       +
       +    if(p0->x==p1->x && p0->y==p1->y)
       +        return 0;
       +    gsetline(p0, p1, l);
       +    /* line is now closed */
       +    if(l->xmajor == 0){
       +        XYswap(p0);
       +        XYswap(p1);
       +        XYswap(&r.min);
       +        XYswap(&r.max);
       +    }
       +    c0 = code(p0, &r);
       +    c1 = code(p1, &r);
       +    ret = 1;
       +    swapped = 0;
       +    n = 0;
       +    while(c0 | c1){
       +        if(c0 & c1){    /* no point of line in r */
       +            ret = 0;
       +            goto Return;
       +        }
       +        if(++n > 10){   /* horrible points; overflow etc. etc. */
       +            ret = 0;
       +            goto Return;
       +        }
       +        if(c0 == 0){    /* swap points */
       +            temp = *p0;
       +            *p0 = *p1;
       +            *p1 = temp;
       +            Swap(c0, c1);
       +            swapped ^= 1;
       +        }
       +        if(c0 == 0)
       +            break;
       +        if(c0 & 1){     /* push towards left edge */
       +            p0->x = r.min.x;
       +            p0->y = _gminor(p0->x, l);
       +        }else if(c0 & 2){   /* push towards right edge */
       +            p0->x = r.max.x-1;
       +            p0->y = _gminor(p0->x, l);
       +        }else if(c0 & 4){   /* push towards top edge */
       +            p0->y = r.min.y;
       +            if(l->slopeneg)
       +                p0->x = _gmajor(p0->y-1, l)-1;
       +            else
       +                p0->x = _gmajor(p0->y, l);
       +        }else if(c0 & 8){   /* push towards bottom edge */
       +            p0->y = r.max.y-1;
       +            if(l->slopeneg)
       +                p0->x = _gmajor(p0->y, l);
       +            else
       +                p0->x = _gmajor(p0->y+1, l)-1;
       +        }
       +        c0 = code(p0, &r);
       +    }
        
            Return:
       -        if(l->xmajor == 0){
       -                XYswap(p0);
       -                XYswap(p1);
       -        }
       -        if(swapped){
       -                temp = *p0;
       -                *p0 = *p1;
       -                *p1 = temp;
       -        }
       -        return ret;
       +    if(l->xmajor == 0){
       +        XYswap(p0);
       +        XYswap(p1);
       +    }
       +    if(swapped){
       +        temp = *p0;
       +        *p0 = *p1;
       +        *p1 = temp;
       +    }
       +    return ret;
        }
 (DIR) diff --git a/libXg/clipr.c b/libXg/clipr.c
       @@ -7,15 +7,15 @@
        int
        clipr(Bitmap *d, Rectangle r)
        {
       -        if(rectclip(&r, d->r) == 0)
       -                return 0;
       -        d->clipr = r;
       -        if(r.min.x != d->r.min.x ||
       -           r.min.y != d->r.min.y ||
       -           r.max.x != d->r.max.x ||
       -           r.max.y != d->r.max.y)
       -                d->flag |= CLIP;
       -        else
       -                d->flag &= ~CLIP;
       -        return 1;
       +    if(rectclip(&r, d->r) == 0)
       +        return 0;
       +    d->clipr = r;
       +    if(r.min.x != d->r.min.x ||
       +       r.min.y != d->r.min.y ||
       +       r.max.x != d->r.max.x ||
       +       r.max.y != d->r.max.y)
       +        d->flag |= CLIP;
       +    else
       +        d->flag &= ~CLIP;
       +    return 1;
        }
 (DIR) diff --git a/libXg/copymasked.c b/libXg/copymasked.c
       @@ -13,37 +13,37 @@
        void
        copymasked(Bitmap *d, Point p, Bitmap *s, Bitmap *m, Rectangle r)
        {
       -        int sx, sy, dx, dy;
       -        XGCValues gcv;
       -        GC g;
       +    int sx, sy, dx, dy;
       +    XGCValues gcv;
       +    GC g;
        
       -        if(Dx(r)<=0 || Dy(r)<=0)
       -                return;
       -        sx = r.min.x;
       -        sy = r.min.y;
       -        if(s->flag&SHIFT){
       -                sx -= s->r.min.x;
       -                sy -= s->r.min.y;
       -        }
       -        dx = p.x;
       -        dy = p.y;
       -        if(d->flag&SHIFT){
       -                dx -= d->r.min.x;
       -                dy -= d->r.min.y;
       -        }
       -        gcv.fill_style = FillStippled;
       -        gcv.stipple = (Pixmap)m->id;
       -        gcv.function = GXclear;
       -        gcv.ts_x_origin = dx;
       -        gcv.ts_y_origin = dy;
       -        gcv.fill_style = FillStippled;
       -        g = _getgc(d, GCFunction|GCStipple|GCTileStipXOrigin
       -                |GCTileStipYOrigin|GCFillStyle, &gcv);
       -        XFillRectangle(_dpy, (Drawable)d->id, g,
       -                        dx, dy, Dx(r), Dy(r));
       -        gcv.function = GXor;
       -        gcv.fill_style = FillSolid;
       -        g = _getgc(d, GCFunction|GCFillStyle, &gcv);
       -        XCopyArea(_dpy, (Drawable)s->id, (Drawable)d->id, g,
       -                        sx, sy, Dx(r), Dy(r), dx, dy);
       +    if(Dx(r)<=0 || Dy(r)<=0)
       +        return;
       +    sx = r.min.x;
       +    sy = r.min.y;
       +    if(s->flag&SHIFT){
       +        sx -= s->r.min.x;
       +        sy -= s->r.min.y;
       +    }
       +    dx = p.x;
       +    dy = p.y;
       +    if(d->flag&SHIFT){
       +        dx -= d->r.min.x;
       +        dy -= d->r.min.y;
       +    }
       +    gcv.fill_style = FillStippled;
       +    gcv.stipple = (Pixmap)m->id;
       +    gcv.function = GXclear;
       +    gcv.ts_x_origin = dx;
       +    gcv.ts_y_origin = dy;
       +    gcv.fill_style = FillStippled;
       +    g = _getgc(d, GCFunction|GCStipple|GCTileStipXOrigin
       +        |GCTileStipYOrigin|GCFillStyle, &gcv);
       +    XFillRectangle(_dpy, (Drawable)d->id, g,
       +            dx, dy, Dx(r), Dy(r));
       +    gcv.function = GXor;
       +    gcv.fill_style = FillSolid;
       +    g = _getgc(d, GCFunction|GCFillStyle, &gcv);
       +    XCopyArea(_dpy, (Drawable)s->id, (Drawable)d->id, g,
       +            sx, sy, Dx(r), Dy(r), dx, dy);
        }
 (DIR) diff --git a/libXg/cursorset.c b/libXg/cursorset.c
       @@ -10,7 +10,7 @@
        void
        cursorset(Point p)
        {
       -        /* motion will be relative to window origin */
       -        p = sub(p, screen.r.min);
       -        XWarpPointer(_dpy, None, (Window)screen.id, 0, 0, 0, 0, p.x, p.y);
       +    /* motion will be relative to window origin */
       +    p = sub(p, screen.r.min);
       +    XWarpPointer(_dpy, None, (Window)screen.id, 0, 0, 0, 0, p.x, p.y);
        }
 (DIR) diff --git a/libXg/disc.c b/libXg/disc.c
       @@ -7,17 +7,17 @@
        void
        disc(Bitmap *b, Point p, int r, int v, Fcode f)
        {
       -        unsigned int d;
       -        int x, y;
       -        GC g;
       +    unsigned int d;
       +    int x, y;
       +    GC g;
        
       -        x = p.x - r;
       -        y = p.y - r;
       -        if (b->flag&SHIFT){
       -                x -= b->r.min.x;
       -                y -= b->r.min.y;
       -        }
       -        d = 2*r;
       -        g = _getfillgc(f, b, v);
       -        XFillArc(_dpy, (Drawable)b->id, g, x, y, d, d, 0, 23040/* 360 deg */);
       +    x = p.x - r;
       +    y = p.y - r;
       +    if (b->flag&SHIFT){
       +        x -= b->r.min.x;
       +        y -= b->r.min.y;
       +    }
       +    d = 2*r;
       +    g = _getfillgc(f, b, v);
       +    XFillArc(_dpy, (Drawable)b->id, g, x, y, d, d, 0, 23040/* 360 deg */);
        }
 (DIR) diff --git a/libXg/ellipse.c b/libXg/ellipse.c
       @@ -9,15 +9,15 @@
        void
        ellipse(Bitmap *bp, Point p, int a, int b, int v, Fcode f)
        {
       -        int x, y;
       -        GC g;
       +    int x, y;
       +    GC g;
        
       -        x = p.x - a;
       -        y = p.y - b;
       -        if (bp->flag&SHIFT){
       -                x -= bp->r.min.x;
       -                y -= bp->r.min.y;
       -        }
       -        g = _getfillgc(f, bp, v);
       -        XDrawArc(_dpy, (Drawable)bp->id, g, x, y, 2*a, 2*b, 0, 23040/* 360 deg */);
       +    x = p.x - a;
       +    y = p.y - b;
       +    if (bp->flag&SHIFT){
       +        x -= bp->r.min.x;
       +        y -= bp->r.min.y;
       +    }
       +    g = _getfillgc(f, bp, v);
       +    XDrawArc(_dpy, (Drawable)bp->id, g, x, y, 2*a, 2*b, 0, 23040/* 360 deg */);
        }
 (DIR) diff --git a/libXg/font.c b/libXg/font.c
       @@ -4,7 +4,7 @@
        #include <libg.h>
        #include "libgint.h"
        
       -#define        PJW        0        /* use NUL==pjw for invisible characters */
       +#define PJW 0   /* use NUL==pjw for invisible characters */
        
        long
        charwidth(XftFont *f, Rune r)
 (DIR) diff --git a/libXg/gcs.c b/libXg/gcs.c
       @@ -36,22 +36,22 @@
         * source and dest use 1 for black.  This is a straight translation.
         */
        static int gx[16] = {
       -        GXclear,                /* Zero */
       -        GXnor,                        /* DnorS */
       -        GXandInverted,                /* DandnotS */
       -        GXcopyInverted,                /* notS */
       -        GXandReverse,                /* notDandS */
       -        GXinvert,                /* notD */
       -        GXxor,                        /* DxorS */
       -        GXnand,                        /* DnandS */
       -        GXand,                        /* DandS */
       -        GXequiv,                /* DxnorS */
       -        GXnoop,                        /* D */
       -        GXorInverted,                /* DornotS */
       -        GXcopy,                        /* S */
       -        GXorReverse,                /* notDorS */
       -        GXor,                        /* DorS */
       -        GXset,                        /* F */
       +    GXclear,        /* Zero */
       +    GXnor,          /* DnorS */
       +    GXandInverted,      /* DandnotS */
       +    GXcopyInverted,     /* notS */
       +    GXandReverse,       /* notDandS */
       +    GXinvert,       /* notD */
       +    GXxor,          /* DxorS */
       +    GXnand,         /* DnandS */
       +    GXand,          /* DandS */
       +    GXequiv,        /* DxnorS */
       +    GXnoop,         /* D */
       +    GXorInverted,       /* DornotS */
       +    GXcopy,         /* S */
       +    GXorReverse,        /* notDorS */
       +    GXor,           /* DorS */
       +    GXset,          /* F */
        };
        
        /*
       @@ -61,22 +61,22 @@ static int gx[16] = {
         * The comment on each line is op, in Fcode terms.
         */
        static int d0s1gx[16] = {
       -        GXset,                        /* Zero */
       -        GXorReverse,                /* DnorS */
       -        GXor,                        /* DandnotS */
       -        GXcopy,                        /* notS */
       -        GXnand,                        /* notDandS */
       -        GXinvert,                /* notD */
       -        GXxor,                        /* DxorS */
       -        GXandReverse,                /* DnandS */
       -        GXorInverted,                /* DandS */
       -        GXequiv,                /* DxnorS */
       -        GXnoop,                        /* D */
       -        GXand,                        /* DornotS */
       -        GXcopyInverted,                /* S */
       -        GXnor,                        /* notDorS */
       -        GXandInverted,                /* DorS */
       -        GXclear,                /* F */
       +    GXset,          /* Zero */
       +    GXorReverse,        /* DnorS */
       +    GXor,           /* DandnotS */
       +    GXcopy,         /* notS */
       +    GXnand,         /* notDandS */
       +    GXinvert,       /* notD */
       +    GXxor,          /* DxorS */
       +    GXandReverse,       /* DnandS */
       +    GXorInverted,       /* DandS */
       +    GXequiv,        /* DxnorS */
       +    GXnoop,         /* D */
       +    GXand,          /* DornotS */
       +    GXcopyInverted,     /* S */
       +    GXnor,          /* notDorS */
       +    GXandInverted,      /* DorS */
       +    GXclear,        /* F */
        };
        /*
         * gx func code corresponding to libg func code when 1 means black
       @@ -85,22 +85,22 @@ static int d0s1gx[16] = {
         * The comment on each line is op, in Fcode terms.
         */
        static int d1s0gx[16] = {
       -        GXclear,                /* Zero */
       -        GXandReverse,                /* DnorS */
       -        GXand,                        /* DandnotS */
       -        GXcopy,                        /* notS */
       -        GXnor,                        /* notDandS */
       -        GXinvert,                /* notD */
       -        GXequiv,                /* DxorS */
       -        GXorReverse,                /* DnandS */
       -        GXandInverted,                /* DandS */
       -        GXxor,                        /* DxnorS */
       -        GXnoop,                        /* D */
       -        GXor,                        /* DornotS */
       -        GXcopyInverted,                /* S */
       -        GXnand,                        /* notDorS */
       -        GXorInverted,                /* DorS */
       -        GXset,                        /* F */
       +    GXclear,        /* Zero */
       +    GXandReverse,       /* DnorS */
       +    GXand,          /* DandnotS */
       +    GXcopy,         /* notS */
       +    GXnor,          /* notDandS */
       +    GXinvert,       /* notD */
       +    GXequiv,        /* DxorS */
       +    GXorReverse,        /* DnandS */
       +    GXandInverted,      /* DandS */
       +    GXxor,          /* DxnorS */
       +    GXnoop,         /* D */
       +    GXor,           /* DornotS */
       +    GXcopyInverted,     /* S */
       +    GXnand,         /* notDorS */
       +    GXorInverted,       /* DorS */
       +    GXset,          /* F */
        };
        
        /*
       @@ -110,44 +110,44 @@ static int d1s0gx[16] = {
         * The comment on each line is op, in Fcode terms.
         */
        static int d0s0gx[16] = {
       -        GXset,                        /* Zero */
       -        GXnand,                        /* DnorS */
       -        GXorInverted,                /* DandnotS */
       -        GXcopyInverted,                /* notS */
       -        GXorReverse,                /* notDandS */
       -        GXinvert,                /* notD */
       -        GXequiv,                /* DxorS */
       -        GXnor,                        /* DnandS */
       -        GXor,                        /* DandS */
       -        GXxor,                        /* DxnorS */
       -        GXnoop,                        /* D */
       -        GXandInverted,                /* DornotS */
       -        GXcopy,                        /* S */
       -        GXandReverse,                /* notDorS */
       -        GXand,                        /* DorS */
       -        GXclear,                /* F */
       +    GXset,          /* Zero */
       +    GXnand,         /* DnorS */
       +    GXorInverted,       /* DandnotS */
       +    GXcopyInverted,     /* notS */
       +    GXorReverse,        /* notDandS */
       +    GXinvert,       /* notD */
       +    GXequiv,        /* DxorS */
       +    GXnor,          /* DnandS */
       +    GXor,           /* DandS */
       +    GXxor,          /* DxnorS */
       +    GXnoop,         /* D */
       +    GXandInverted,      /* DornotS */
       +    GXcopy,         /* S */
       +    GXandReverse,       /* notDorS */
       +    GXand,          /* DorS */
       +    GXclear,        /* F */
        };
        
        /*
         * 1 for those Fcodes that are degenerate (don't involve src)
         */
        static int degengc[16] = {
       -        1,                        /* Zero */
       -        0,                        /* DnorS */
       -        0,                        /* DandnotS */
       -        0,                        /* notS */
       -        0,                        /* notDandS */
       -        1,                        /* notD */
       -        0,                        /* DxorS */
       -        0,                        /* DnandS */
       -        0,                        /* DandS */
       -        0,                        /* DxnorS */
       -        1,                        /* D */
       -        0,                        /* DornotS */
       -        0,                        /* S */
       -        0,                        /* notDorS */
       -        0,                        /* DorS */
       -        1,                        /* F */
       +    1,          /* Zero */
       +    0,          /* DnorS */
       +    0,          /* DandnotS */
       +    0,          /* notS */
       +    0,          /* notDandS */
       +    1,          /* notD */
       +    0,          /* DxorS */
       +    0,          /* DnandS */
       +    0,          /* DandS */
       +    0,          /* DxnorS */
       +    1,          /* D */
       +    0,          /* DornotS */
       +    0,          /* S */
       +    0,          /* notDorS */
       +    0,          /* DorS */
       +    1,          /* F */
        };
        
        /*
       @@ -160,37 +160,37 @@ static int degengc[16] = {
        GC
        _getgc(Bitmap *b, unsigned long gcvm, XGCValues *pgcv)
        {
       -        static GC gc0, gcn;
       -        static int clipset = 0;
       -        GC g;
       -        XRectangle xr;
       +    static GC gc0, gcn;
       +    static int clipset = 0;
       +    GC g;
       +    XRectangle xr;
        
       -        g = (b->ldepth==0)? gc0 : gcn;
       -        if(!g){
       -                g = XCreateGC(_dpy, (Drawable)b->id, gcvm, pgcv);
       -                if(b->ldepth==0)
       -                        gc0 = g;
       -                else
       -                        gcn = g;
       -        } else
       -                XChangeGC(_dpy, g, gcvm, pgcv);
       -        if(b->flag&CLIP){
       -                xr.x = b->clipr.min.x;
       -                xr.y = b->clipr.min.y;
       -                xr.width = Dx(b->clipr);
       -                xr.height = Dy(b->clipr);
       -                if(b->flag&SHIFT){
       -                        xr.x -= b->r.min.x;
       -                        xr.y -= b->r.min.y;
       -                }
       -                XSetClipRectangles(_dpy, g, 0, 0, &xr, 1, YXBanded);
       -                clipset = 1;
       -        }else if(clipset){
       -                pgcv->clip_mask = None;
       -                XChangeGC(_dpy, g, GCClipMask, pgcv);
       -                clipset = 0;
       -        }
       -        return g;
       +    g = (b->ldepth==0)? gc0 : gcn;
       +    if(!g){
       +        g = XCreateGC(_dpy, (Drawable)b->id, gcvm, pgcv);
       +        if(b->ldepth==0)
       +            gc0 = g;
       +        else
       +            gcn = g;
       +    } else
       +        XChangeGC(_dpy, g, gcvm, pgcv);
       +    if(b->flag&CLIP){
       +        xr.x = b->clipr.min.x;
       +        xr.y = b->clipr.min.y;
       +        xr.width = Dx(b->clipr);
       +        xr.height = Dy(b->clipr);
       +        if(b->flag&SHIFT){
       +            xr.x -= b->r.min.x;
       +            xr.y -= b->r.min.y;
       +        }
       +        XSetClipRectangles(_dpy, g, 0, 0, &xr, 1, YXBanded);
       +        clipset = 1;
       +    }else if(clipset){
       +        pgcv->clip_mask = None;
       +        XChangeGC(_dpy, g, GCClipMask, pgcv);
       +        clipset = 0;
       +    }
       +    return g;
        }
        
        /*
       @@ -207,88 +207,88 @@ _getfillgc(Fcode f, Bitmap *b, unsigned long val)
        GC
        _getfillgc2(Fcode f, Bitmap *b, unsigned long val, unsigned long fg, unsigned long bg)
        {
       -        int xf, m;
       -        unsigned long v, spix, vmax;
       -        XGCValues gcv;
       +    int xf, m;
       +    unsigned long v, spix, vmax;
       +    XGCValues gcv;
        
       -        f &= F;
       -        vmax = _ld2dmask[b->ldepth];
       -        v = val & vmax;
       -        spix = v;
       -        xf = GXcopy;
       -        m = b->flag;
       -        if(m & DP1){
       -                xf = (m&BL1)? gx[f] : d0s1gx[f];
       -        }else{
       -                switch(f){
       -                case Zero:
       -            labZero:
       -                        spix = bg;
       -                        break;
       -                case F:
       -            labF:
       -                        spix = fg;
       -                        break;
       -                case D:
       -            labD:
       -                        xf = GXnoop;
       -                        break;
       -                case notD:
       -            labnotD:
       -                        xf = GXxor;
       -                        spix = fg^bg;
       -                        break;
       -                case S:
       -                        if(val == ~0)
       -                                spix = fg;
       -                        else
       -                                spix = v;
       -                        break;
       -                case notS:
       -                        if(val == ~0)
       -                                spix = bg;
       -                        else
       -                                spix = v;
       -                        break;
       -                case DxorS:
       -                        xf = GXxor;
       -                        if(val == ~0)
       -                                spix = fg^bg;
       -                        else
       -                                spix = v;
       -                        break;
       -                case DxnorS:
       -                        xf = GXxor;
       -                        if(val == 0)
       -                                spix = fg^bg;
       -                        else
       -                                spix = v;
       -                        break;
       -                default:
       -                        /* hard to do anything other than v==0 or v==~0 case */
       -                        if(v < vmax-v){
       -                                /* v is closer to 0 than vmax */
       -                                switch(f&~S){
       -                                case D&~S:        goto labD;
       -                                case notD&~S:        goto labnotD;
       -                                case Zero&~S:        goto labZero;
       -                                case F&~S:        goto labF;
       -                                }
       -                        }else{
       -                                /* v is closer to vmax than 0 */
       -                                switch(f&S){
       -                                case D&S:        goto labD;
       -                                case notD&S:        goto labnotD;
       -                                case Zero&S:        goto labZero;
       -                                case F&S:        goto labF;
       -                                }
       -                        }
       -                        
       -                }
       -        }
       -        gcv.foreground = spix;
       -        gcv.function = xf;
       -        return _getgc(b, GCForeground|GCFunction, &gcv);
       +    f &= F;
       +    vmax = _ld2dmask[b->ldepth];
       +    v = val & vmax;
       +    spix = v;
       +    xf = GXcopy;
       +    m = b->flag;
       +    if(m & DP1){
       +        xf = (m&BL1)? gx[f] : d0s1gx[f];
       +    }else{
       +        switch(f){
       +        case Zero:
       +        labZero:
       +            spix = bg;
       +            break;
       +        case F:
       +        labF:
       +            spix = fg;
       +            break;
       +        case D:
       +        labD:
       +            xf = GXnoop;
       +            break;
       +        case notD:
       +        labnotD:
       +            xf = GXxor;
       +            spix = fg^bg;
       +            break;
       +        case S:
       +            if(val == ~0)
       +                spix = fg;
       +            else
       +                spix = v;
       +            break;
       +        case notS:
       +            if(val == ~0)
       +                spix = bg;
       +            else
       +                spix = v;
       +            break;
       +        case DxorS:
       +            xf = GXxor;
       +            if(val == ~0)
       +                spix = fg^bg;
       +            else
       +                spix = v;
       +            break;
       +        case DxnorS:
       +            xf = GXxor;
       +            if(val == 0)
       +                spix = fg^bg;
       +            else
       +                spix = v;
       +            break;
       +        default:
       +            /* hard to do anything other than v==0 or v==~0 case */
       +            if(v < vmax-v){
       +                /* v is closer to 0 than vmax */
       +                switch(f&~S){
       +                case D&~S:  goto labD;
       +                case notD&~S:   goto labnotD;
       +                case Zero&~S:   goto labZero;
       +                case F&~S:  goto labF;
       +                }
       +            }else{
       +                /* v is closer to vmax than 0 */
       +                switch(f&S){
       +                case D&S:   goto labD;
       +                case notD&S:    goto labnotD;
       +                case Zero&S:    goto labZero;
       +                case F&S:   goto labF;
       +                }
       +            }
       +            
       +        }
       +    }
       +    gcv.foreground = spix;
       +    gcv.function = xf;
       +    return _getgc(b, GCForeground|GCFunction, &gcv);
        }
        
        /*
       @@ -307,103 +307,103 @@ _getcopygc(Fcode f, Bitmap *db, Bitmap *sb, int *bltfunc)
        GC
        _getcopygc2(Fcode f, Bitmap *db, Bitmap *sb, int *bltfunc, unsigned long fg, unsigned long bg)
        {
       -        unsigned long spix, df, sf;
       -        int xf, c;
       -        XGCValues gcv;
       -        unsigned long gcvm;
       +    unsigned long spix, df, sf;
       +    int xf, c;
       +    XGCValues gcv;
       +    unsigned long gcvm;
        
       -        f &= F;
       -        gcvm = 0;
       -        df = db->flag;
       -        if(degengc[f]){
       -                *bltfunc = UseFillRectangle;
       -                if(df&SCR || !(df&DP1)){
       +    f &= F;
       +    gcvm = 0;
       +    df = db->flag;
       +    if(degengc[f]){
       +        *bltfunc = UseFillRectangle;
       +        if(df&SCR || !(df&DP1)){
                    // nothing XXX
       -                }else{
       -                        /* must be DP1 and BL1 */
       -                        fg = 1;
       -                        bg = 0;
       -                }
       -                switch(f){
       -                case Zero:
       -                        xf = GXcopy;
       -                        spix = bg;
       -                        break;
       -                case F:
       -                        xf = GXcopy;
       -                        spix = fg;
       -                        break;
       -                case D:
       -                        xf = GXnoop;
       -                        spix = fg;
       -                        break;
       -                case notD:
       -                        xf = GXxor;
       -                        spix = fg^bg;
       -                        break;
       -                }
       -                gcv.function = xf;
       -                gcv.foreground = spix;
       -                gcvm = GCFunction|GCForeground;
       -        }else{
       -                /* src is involved in f */
       +        }else{
       +            /* must be DP1 and BL1 */
       +            fg = 1;
       +            bg = 0;
       +        }
       +        switch(f){
       +        case Zero:
       +            xf = GXcopy;
       +            spix = bg;
       +            break;
       +        case F:
       +            xf = GXcopy;
       +            spix = fg;
       +            break;
       +        case D:
       +            xf = GXnoop;
       +            spix = fg;
       +            break;
       +        case notD:
       +            xf = GXxor;
       +            spix = fg^bg;
       +            break;
       +        }
       +        gcv.function = xf;
       +        gcv.foreground = spix;
       +        gcvm = GCFunction|GCForeground;
       +    }else{
       +        /* src is involved in f */
        
        #define code(f1,f2) ((((f1)&(DP1|BL1))<<2)|((f2)&(DP1|BL1)))
        
       -                sf = sb->flag;
       -                c = code(df,sf);
       -                *bltfunc = UseCopyArea;
       -                switch(code(df,sf)){
       -                case code(DP1|BL1,DP1|BL1):
       -                case code(BL1,BL1):
       -                        xf = gx[f];
       -                        break;
       -                case code(DP1|BL1,DP1):
       -                        xf = d1s0gx[f];
       -                        break;
       -                case code(DP1,DP1|BL1):
       -                        xf = d0s1gx[f];
       -                        break;
       -                case code(DP1,DP1):
       -                case code(0,0):
       -                        xf = d0s0gx[f];
       -                        break;
       -                default:
       -                        /*
       -                         * One bitmap has depth 1, the other has screen depth.
       -                         * We know the bitmap must have BL1.
       -                         * CopyPlane must be used; it won't really work
       -                         * for more than fcode==S.
       -                         */
       +        sf = sb->flag;
       +        c = code(df,sf);
       +        *bltfunc = UseCopyArea;
       +        switch(code(df,sf)){
       +        case code(DP1|BL1,DP1|BL1):
       +        case code(BL1,BL1):
       +            xf = gx[f];
       +            break;
       +        case code(DP1|BL1,DP1):
       +            xf = d1s0gx[f];
       +            break;
       +        case code(DP1,DP1|BL1):
       +            xf = d0s1gx[f];
       +            break;
       +        case code(DP1,DP1):
       +        case code(0,0):
       +            xf = d0s0gx[f];
       +            break;
       +        default:
       +            /*
       +             * One bitmap has depth 1, the other has screen depth.
       +             * We know the bitmap must have BL1.
       +             * CopyPlane must be used; it won't really work
       +             * for more than fcode==S.
       +             */
        
       -                        *bltfunc = UseCopyPlane;
       -                        xf = GXcopy;
       -                        switch(c){
       +            *bltfunc = UseCopyPlane;
       +            xf = GXcopy;
       +            switch(c){
        
       -                        case code(0,DP1|BL1):
       -                        case code(BL1,DP1|BL1):
       +            case code(0,DP1|BL1):
       +            case code(BL1,DP1|BL1):
                        // nothing XXX
       -                                break;
       -                        case code(DP1|BL1,0):
       -                                fg = 0;
       -                                bg = 1;
       -                                break;
       -                        case code(DP1|BL1,BL1):
       -                                fg = 1;
       -                                bg = 0;
       -                                break;
       -                        default:
       -                                berror("bad combination of copy bitmaps");
       -                        }
       -                        gcv.foreground = fg;
       -                        gcv.background = bg;
       -                        gcvm |= GCForeground|GCBackground;
       -                }
       -                gcv.function = xf;
       -                gcvm |= GCFunction;
       -        
       +                break;
       +            case code(DP1|BL1,0):
       +                fg = 0;
       +                bg = 1;
       +                break;
       +            case code(DP1|BL1,BL1):
       +                fg = 1;
       +                bg = 0;
       +                break;
       +            default:
       +                berror("bad combination of copy bitmaps");
       +            }
       +            gcv.foreground = fg;
       +            gcv.background = bg;
       +            gcvm |= GCForeground|GCBackground;
       +        }
       +        gcv.function = xf;
       +        gcvm |= GCFunction;
       +    
        #undef code
       -        }
       +    }
        
       -        return _getgc(db, gcvm, &gcv);
       +    return _getgc(db, gcvm, &gcv);
        }
 (DIR) diff --git a/libXg/getrect.c b/libXg/getrect.c
       @@ -9,56 +9,56 @@ grabcursor(void)
        {
            raisewindow();
        
       -        /* Grab X server with an limp wrist. */
       -        while (XGrabPointer(_dpy, screen.id, False,
       -                        ButtonPressMask|ButtonReleaseMask|
       -                        ButtonMotionMask|StructureNotifyMask,
       -                GrabModeAsync, GrabModeAsync, None, None, CurrentTime)
       -                        != GrabSuccess)
       -                sleep(2);
       +    /* Grab X server with an limp wrist. */
       +    while (XGrabPointer(_dpy, screen.id, False,
       +            ButtonPressMask|ButtonReleaseMask|
       +            ButtonMotionMask|StructureNotifyMask,
       +        GrabModeAsync, GrabModeAsync, None, None, CurrentTime)
       +            != GrabSuccess)
       +        sleep(2);
        
       -        /* Grab the keyboard too */
       -        XSetInputFocus(_dpy, screen.id, RevertToParent, CurrentTime);
       +    /* Grab the keyboard too */
       +    XSetInputFocus(_dpy, screen.id, RevertToParent, CurrentTime);
        }
        
        static void
        ungrabcursor(void)
        {
       -        XUngrabPointer(_dpy, CurrentTime);
       +    XUngrabPointer(_dpy, CurrentTime);
        }
        
        Rectangle
        getrect(int but, Mouse *m){
       -        Rectangle r, rc;
       +    Rectangle r, rc;
        
       -        but = 1<<(but-1);
       -        cursorswitch(SweepCursor);
       -        while(m->buttons)
       -                *m = emouse();
       -        grabcursor();
       -        while(!(m->buttons & but)){
       -                *m = emouse();
       -                if(m->buttons & (7^but))
       -                        goto Return;
       -        }
       -        r.min = m->xy;
       -        r.max = m->xy;
       -        do{
       -                rc = rcanon(r);
       -                border(&screen, rc, 2, F&~D, _bgpixel);
       -                *m = emouse();
       -                border(&screen, rc, 2, F&~D, _bgpixel);
       -                r.max = m->xy;
       -        }while(m->buttons & but);
       +    but = 1<<(but-1);
       +    cursorswitch(SweepCursor);
       +    while(m->buttons)
       +        *m = emouse();
       +    grabcursor();
       +    while(!(m->buttons & but)){
       +        *m = emouse();
       +        if(m->buttons & (7^but))
       +            goto Return;
       +    }
       +    r.min = m->xy;
       +    r.max = m->xy;
       +    do{
       +        rc = rcanon(r);
       +        border(&screen, rc, 2, F&~D, _bgpixel);
       +        *m = emouse();
       +        border(&screen, rc, 2, F&~D, _bgpixel);
       +        r.max = m->xy;
       +    }while(m->buttons & but);
        
            Return:
       -        cursorswitch(DefaultCursor);
       -        if(m->buttons & (7^but)){
       -                rc.min.x = rc.max.x = 0;
       -                rc.min.y = rc.max.y = 0;
       -                while(m->buttons)
       -                        *m = emouse();
       -        }
       -        ungrabcursor();
       -        return rc;
       +    cursorswitch(DefaultCursor);
       +    if(m->buttons & (7^but)){
       +        rc.min.x = rc.max.x = 0;
       +        rc.min.y = rc.max.y = 0;
       +        while(m->buttons)
       +            *m = emouse();
       +    }
       +    ungrabcursor();
       +    return rc;
        }
 (DIR) diff --git a/libXg/gwin.c b/libXg/gwin.c
       @@ -32,35 +32,35 @@ static String SelectSwap(Widget, String);
        #define Offset(field) XtOffsetOf(GwinRec, gwin.field)
        
        static XtResource resources[] = {
       -        {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
       -                Offset(foreground), XtRString, (XtPointer)XtDefaultForeground},
       -        {XtNscrollForwardR, XtCScrollForwardR, XtRBoolean, sizeof(Boolean),
       -                Offset(forwardr), XtRImmediate, (XtPointer)TRUE},
       -        {XtNreshaped, XtCReshaped, XtRFunction, sizeof(Reshapefunc),
       -                Offset(reshaped), XtRFunction, (XtPointer) NULL},
       -        {XtNgotchar, XtCGotchar, XtRFunction, sizeof(Charfunc),
       -                Offset(gotchar), XtRFunction, (XtPointer) NULL},
       -        {XtNgotmouse, XtCGotmouse, XtRFunction, sizeof(Mousefunc),
       -                Offset(gotmouse), XtRFunction, (XtPointer) NULL},
       -        {XtNselection, XtCSelection, XtRString, sizeof(String),
       -                Offset(selection), XtRString, (XtPointer) NULL},
       -        {XtNcomposeMod, XtCComposeMod, XtRInt, sizeof(int),
       -                Offset(compose), XtRImmediate, (XtPointer) 0}
       +    {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
       +        Offset(foreground), XtRString, (XtPointer)XtDefaultForeground},
       +    {XtNscrollForwardR, XtCScrollForwardR, XtRBoolean, sizeof(Boolean),
       +        Offset(forwardr), XtRImmediate, (XtPointer)TRUE},
       +    {XtNreshaped, XtCReshaped, XtRFunction, sizeof(Reshapefunc),
       +        Offset(reshaped), XtRFunction, (XtPointer) NULL},
       +    {XtNgotchar, XtCGotchar, XtRFunction, sizeof(Charfunc),
       +        Offset(gotchar), XtRFunction, (XtPointer) NULL},
       +    {XtNgotmouse, XtCGotmouse, XtRFunction, sizeof(Mousefunc),
       +        Offset(gotmouse), XtRFunction, (XtPointer) NULL},
       +    {XtNselection, XtCSelection, XtRString, sizeof(String),
       +        Offset(selection), XtRString, (XtPointer) NULL},
       +    {XtNcomposeMod, XtCComposeMod, XtRInt, sizeof(int),
       +        Offset(compose), XtRImmediate, (XtPointer) 0}
        };
        #undef Offset
        
        static XtActionsRec actions[] = {
       -        {"key", Keyaction},
       -        {"mouse", Mouseaction},
       -        {"mapping", Mappingaction}
       +    {"key", Keyaction},
       +    {"mouse", Mouseaction},
       +    {"mapping", Mappingaction}
        };
        
        static char tms[] =
       -        "<Key> : key() \n\
       -        <Motion> : mouse() \n\
       -        <BtnDown> : mouse() \n\
       -        <BtnUp> : mouse() \n\
       -        <Mapping> : mapping() \n";
       +    "<Key> : key() \n\
       +    <Motion> : mouse() \n\
       +    <BtnDown> : mouse() \n\
       +    <BtnUp> : mouse() \n\
       +    <Mapping> : mapping() \n";
        
        /* Class record declaration */
        
       @@ -102,7 +102,7 @@ GwinClassRec gwinClassRec = {
           },
          /* Gwin class part */
           {
       -    /* select_swap          */    SelectSwap,
       +    /* select_swap    */    SelectSwap,
           }
        };
        
       @@ -115,52 +115,52 @@ static int keypermod;
        static void
        Realize(Widget w, XtValueMask *valueMask, XSetWindowAttributes *attrs)
        {
       -        XtValueMask                mask;
       +    XtValueMask     mask;
        
       -        *valueMask |= CWBackingStore;
       -        attrs->backing_store = Always;
       +    *valueMask |= CWBackingStore;
       +    attrs->backing_store = Always;
        
       -        XtCreateWindow(w, InputOutput, (Visual *)0, *valueMask, attrs);
       -        XtSetKeyboardFocus(w->core.parent, w);
       -        if (modmap = XGetModifierMapping(XtDisplay(w)))
       -                keypermod = modmap->max_keypermod;
       +    XtCreateWindow(w, InputOutput, (Visual *)0, *valueMask, attrs);
       +    XtSetKeyboardFocus(w->core.parent, w);
       +    if (modmap = XGetModifierMapping(XtDisplay(w)))
       +        keypermod = modmap->max_keypermod;
        
       -        Resize(w);
       +    Resize(w);
        }
        
        static void
        Resize(Widget w)
        {
       -        if(XtIsRealized(w))
       -                (*(XtClass(w)->core_class.expose))(w, (XEvent *)NULL, (Region)NULL);
       +    if(XtIsRealized(w))
       +        (*(XtClass(w)->core_class.expose))(w, (XEvent *)NULL, (Region)NULL);
        }
        
        static void
        Redraw(Widget w, XEvent *e, Region r)
        {
       -        Reshapefunc f;
       +    Reshapefunc f;
        
       -        f = ((GwinWidget)w)->gwin.reshaped;
       -        if(f)
       -                (*f)(w->core.x, w->core.y,
       -                        w->core.x+w->core.width, w->core.y+w->core.height);
       +    f = ((GwinWidget)w)->gwin.reshaped;
       +    if(f)
       +        (*f)(w->core.x, w->core.y,
       +            w->core.x+w->core.width, w->core.y+w->core.height);
        }
        
        static void
        Mappingaction(Widget w, XEvent *e, String *p, Cardinal *np)
        {
       -        if (modmap)
       -                XFreeModifiermap(modmap);
       -        modmap = XGetModifierMapping(e->xany.display);
       -        if (modmap)
       -                keypermod = modmap->max_keypermod;
       +    if (modmap)
       +        XFreeModifiermap(modmap);
       +    modmap = XGetModifierMapping(e->xany.display);
       +    if (modmap)
       +        keypermod = modmap->max_keypermod;
        }
        
        #define STUFFCOMPOSE() \
       -                                f = ((GwinWidget)w)->gwin.gotchar; \
       -                                if (f) \
       -                                        for (c = 0; c < composing; c++) \
       -                                                (*f)(compose[c], 0, Tcurrent, 0, 0)
       +                f = ((GwinWidget)w)->gwin.gotchar; \
       +                if (f) \
       +                    for (c = 0; c < composing; c++) \
       +                        (*f)(compose[c], 0, Tcurrent, 0, 0)
        
        typedef struct Keymapping Keymapping;
        struct Keymapping{
       @@ -200,25 +200,25 @@ keysymtoshort(KeySym k)
        static void
        Keyaction(Widget w, XEvent *e, String *p, Cardinal *np)
        {
       -        static unsigned char compose[5];
       -        static int composing = -2;
       +    static unsigned char compose[5];
       +    static int composing = -2;
            int composed = 0;
            int kind = Kraw;
        
       -        int c, minmod;
       -        KeySym k, mk;
       -        Charfunc f;
       -        Modifiers md;
       +    int c, minmod;
       +    KeySym k, mk;
       +    Charfunc f;
       +    Modifiers md;
        
       -        /*
       -         * I tried using XtGetActionKeysym, but it didn't seem to
       -         * do case conversion properly
       -         * (at least, with Xterminal servers and R4 intrinsics)
       -         */
       -        if(e->xany.type != KeyPress)
       -                return;
       +    /*
       +     * I tried using XtGetActionKeysym, but it didn't seem to
       +     * do case conversion properly
       +     * (at least, with Xterminal servers and R4 intrinsics)
       +     */
       +    if(e->xany.type != KeyPress)
       +        return;
        
       -        XtTranslateKeycode(e->xany.display, (KeyCode)e->xkey.keycode, e->xkey.state, &md, &k);
       +    XtTranslateKeycode(e->xany.display, (KeyCode)e->xkey.keycode, e->xkey.state, &md, &k);
        
            /* Check to see if it's a specially-handled key first. */
            for (Keymapping *m = keymappings; m && m->kind != Kend; m++){
       @@ -232,81 +232,81 @@ Keyaction(Widget w, XEvent *e, String *p, Cardinal *np)
                }
            }
        
       -        /*
       -         * The following song and dance is so we can have our chosen
       -         * modifier key behave like a compose key, i.e, press and release
       -         * and then type the compose sequence, like Plan 9.  We have
       -         * to find out which key is the compose key first 'though.
       -         */
       -        if (IsModifierKey(k) && ((GwinWidget)w)->gwin.compose
       -                        && composing == -2 && modmap) {
       -                minmod = (((GwinWidget)w)->gwin.compose+2)*keypermod;
       -                for (c = minmod; c < minmod+keypermod; c++) {
       -                        XtTranslateKeycode(e->xany.display,
       -                                        modmap->modifiermap[c],
       -                                        e->xkey.state, &md, &mk);
       -                        if (k == mk) {
       -                                composing = -1;
       -                                break;
       -                        }
       -                }
       -                return;
       -        }
       -
       -        /* Handle Multi_key separately, since it isn't a modifier */
       -        if(k == XK_Multi_key) {
       -                composing = -1;
       -                return;
       -        }
       -
       -        if(k == NoSymbol || k > 0xff00)
       -                return;
       -
       -        /* Check to see if we are in a composition sequence */
       -        if (!((GwinWidget)w)->gwin.compose && (e->xkey.state & Mod1Mask)
       -                        && composing == -2)
       -                composing = -1;
       -        if (composing > -2) {
       -                compose[++composing] = k;
       -                if ((*compose == 'X') && (composing > 0)) {
       -                        if ((k < '0') || (k > 'f') ||
       -                                        ((k > '9') && (k < 'a'))) {
       -                                STUFFCOMPOSE();
       -                                c = (unsigned short)k;
       -                                composing = -2;
       -                        } else if (composing == 4) {
       -                                c = unicode(compose);
       -                                if (c == -1) {
       -                                        STUFFCOMPOSE();
       -                                        c = (unsigned short)compose[4];
       -                                } else
       +    /*
       +     * The following song and dance is so we can have our chosen
       +     * modifier key behave like a compose key, i.e, press and release
       +     * and then type the compose sequence, like Plan 9.  We have
       +     * to find out which key is the compose key first 'though.
       +     */
       +    if (IsModifierKey(k) && ((GwinWidget)w)->gwin.compose
       +            && composing == -2 && modmap) {
       +        minmod = (((GwinWidget)w)->gwin.compose+2)*keypermod;
       +        for (c = minmod; c < minmod+keypermod; c++) {
       +            XtTranslateKeycode(e->xany.display,
       +                    modmap->modifiermap[c],
       +                        e->xkey.state, &md, &mk);
       +            if (k == mk) {
       +                composing = -1;
       +                break;
       +            }
       +        }
       +        return;
       +    }
       +
       +    /* Handle Multi_key separately, since it isn't a modifier */
       +    if(k == XK_Multi_key) {
       +        composing = -1;
       +        return;
       +    }
       +
       +    if(k == NoSymbol || k > 0xff00)
       +        return;
       +
       +    /* Check to see if we are in a composition sequence */
       +    if (!((GwinWidget)w)->gwin.compose && (e->xkey.state & Mod1Mask)
       +            && composing == -2)
       +        composing = -1;
       +    if (composing > -2) {
       +        compose[++composing] = k;
       +        if ((*compose == 'X') && (composing > 0)) {
       +            if ((k < '0') || (k > 'f') ||
       +                    ((k > '9') && (k < 'a'))) {
       +                STUFFCOMPOSE();
       +                c = (unsigned short)k;
       +                composing = -2;
       +            } else if (composing == 4) {
       +                c = unicode(compose);
       +                if (c == -1) {
       +                    STUFFCOMPOSE();
       +                    c = (unsigned short)compose[4];
       +                } else
                            composed = 1;
       -                                composing = -2;
       -                        }
       -                } else if (composing == 1) {
       -                        c = (int)latin1(compose);
       -                        if (c == -1) {
       -                                STUFFCOMPOSE();
       -                                c = (unsigned short)compose[1];
       -                        } else
       +                composing = -2;
       +            }
       +        } else if (composing == 1) {
       +            c = (int)latin1(compose);
       +            if (c == -1) {
       +                STUFFCOMPOSE();
       +                c = (unsigned short)compose[1];
       +            } else
                        composed = 1;
       -                        composing = -2;
       -                }
       -        } else {
       -                if (composing >= 0) {
       -                        composing++;
       -                        STUFFCOMPOSE();
       -                }
       -                c = keysymtoshort(k);
       -                composing = -2;
       -        }
       -
       -        if (composing >= -1)
       -                return;
       -
       -        f = ((GwinWidget)w)->gwin.gotchar;
       -        if(f)
       -                (*f)(c, kind, Tcurrent, 0, 0);
       +            composing = -2;
       +        }
       +    } else {
       +        if (composing >= 0) {
       +            composing++;
       +            STUFFCOMPOSE();
       +        }
       +        c = keysymtoshort(k);
       +        composing = -2;
       +    }
       +
       +    if (composing >= -1)
       +        return;
       +
       +    f = ((GwinWidget)w)->gwin.gotchar;
       +    if(f)
       +        (*f)(c, kind, Tcurrent, 0, 0);
        }
        
        typedef struct Chordmapping Chordmapping;
       @@ -333,50 +333,50 @@ Chordmapping chordmappings[] ={
        static void
        Mouseaction(Widget w, XEvent *e, String *p, Cardinal *np)
        {
       -        int s = 0;
       +    int s = 0;
            int ps = 0; /* the previous state */
            int ob = 0;
       -        XButtonEvent *be = (XButtonEvent *)e;
       -        XMotionEvent *me = (XMotionEvent *)e;
       -        Gwinmouse m;
       -        Mousefunc f;
       -
       -        switch(e->type){
       -        case ButtonPress:
       -                m.xy.x = be->x;
       -                m.xy.y = be->y;
       -                m.msec = be->time;
       -                ps = s = be->state;
       -                switch(be->button){
       -                case 1:        s |= Button1Mask; break;
       -                case 2:        s |= Button2Mask; break;
       -                case 3:        s |= Button3Mask; break;
       -                case 4:        s |= Button4Mask; break;
       -                case 5: s |= Button5Mask; break;
       -                }
       -                break;
       -        case ButtonRelease:
       -                m.xy.x = be->x;
       -                m.xy.y = be->y;
       -                m.msec = be->time;
       -                ps = s = be->state;
       -                switch(be->button){
       -                case 1:        s &= ~Button1Mask; break;
       -                case 2:        s &= ~Button2Mask; break;
       -                case 3:        s &= ~Button3Mask; break;
       -                case 4:        s &= ~Button4Mask; break;
       -                case 5: s &= ~Button5Mask; break;
       -                }
       -                break;
       -        case MotionNotify:
       -                ps = s = me->state;
       -                m.xy.x = me->x;
       -                m.xy.y = me->y;
       -                m.msec = me->time;
       -                break;
       -        default:
       -                return;
       -        }
       +    XButtonEvent *be = (XButtonEvent *)e;
       +    XMotionEvent *me = (XMotionEvent *)e;
       +    Gwinmouse m;
       +    Mousefunc f;
       +
       +    switch(e->type){
       +    case ButtonPress:
       +        m.xy.x = be->x;
       +        m.xy.y = be->y;
       +        m.msec = be->time;
       +        ps = s = be->state;
       +        switch(be->button){
       +        case 1: s |= Button1Mask; break;
       +        case 2: s |= Button2Mask; break;
       +        case 3: s |= Button3Mask; break;
       +        case 4: s |= Button4Mask; break;
       +        case 5: s |= Button5Mask; break;
       +        }
       +        break;
       +    case ButtonRelease:
       +        m.xy.x = be->x;
       +        m.xy.y = be->y;
       +        m.msec = be->time;
       +        ps = s = be->state;
       +        switch(be->button){
       +        case 1: s &= ~Button1Mask; break;
       +        case 2: s &= ~Button2Mask; break;
       +        case 3: s &= ~Button3Mask; break;
       +        case 4: s &= ~Button4Mask; break;
       +        case 5: s &= ~Button5Mask; break;
       +        }
       +        break;
       +    case MotionNotify:
       +        ps = s = me->state;
       +        m.xy.x = me->x;
       +        m.xy.y = me->y;
       +        m.msec = me->time;
       +        break;
       +    default:
       +        return;
       +    }
        
            m.buttons = 0;
        
       @@ -403,86 +403,86 @@ Mouseaction(Widget w, XEvent *e, String *p, Cardinal *np)
                }
            }
        
       -        f = ((GwinWidget)w)->gwin.gotmouse;
       -        if(f)
       -                (*f)(&m);
       +    f = ((GwinWidget)w)->gwin.gotmouse;
       +    if(f)
       +        (*f)(&m);
        }
        
        static void
        SelCallback(Widget w, XtPointer cldata, Atom *sel, Atom *seltype,
       -        XtPointer val, unsigned long *len, int *fmt)
       +    XtPointer val, unsigned long *len, int *fmt)
        {
       -        String s;
       -        int n;
       -        GwinWidget gw = (GwinWidget)w;
       -
       -        if(gw->gwin.selection)
       -                XtFree(gw->gwin.selection);
       -        if(*seltype != XA_STRING)
       -                n = 0;
       -        else
       -                n = (*len) * (*fmt/8);
       -        s = (String)XtMalloc(n+1);
       -        if(n > 0)
       -                memcpy(s, (char *)val, n);
       -        s[n] = 0;
       -        gw->gwin.selection = s;
       -        XtFree(val);
       +    String s;
       +    int n;
       +    GwinWidget gw = (GwinWidget)w;
       +
       +    if(gw->gwin.selection)
       +        XtFree(gw->gwin.selection);
       +    if(*seltype != XA_STRING)
       +        n = 0;
       +    else
       +        n = (*len) * (*fmt/8);
       +    s = (String)XtMalloc(n+1);
       +    if(n > 0)
       +        memcpy(s, (char *)val, n);
       +    s[n] = 0;
       +    gw->gwin.selection = s;
       +    XtFree(val);
        }
        
        static Boolean
        SendSel(Widget w, Atom *sel, Atom *target, Atom *rtype, XtPointer *ans,
       -                unsigned long *anslen, int *ansfmt)
       +        unsigned long *anslen, int *ansfmt)
        {
       -        GwinWidget gw = (GwinWidget)w;
       -        static Atom targets = 0;
       -        XrmValue src, dst;
       -        char *s;
       -
       -        if(*target == XA_STRING){
       -                s = gw->gwin.selection;
       -                if(!s)
       -                        s = "";
       -                *rtype = XA_STRING;
       -                *ans = (XtPointer) XtNewString(s);
       -                *anslen = strlen(*ans);
       -                *ansfmt = 8;
       -                return TRUE;
       -        }
       -
       -        return FALSE;
       +    GwinWidget gw = (GwinWidget)w;
       +    static Atom targets = 0;
       +    XrmValue src, dst;
       +    char *s;
       +
       +    if(*target == XA_STRING){
       +        s = gw->gwin.selection;
       +        if(!s)
       +            s = "";
       +        *rtype = XA_STRING;
       +        *ans = (XtPointer) XtNewString(s);
       +        *anslen = strlen(*ans);
       +        *ansfmt = 8;
       +        return TRUE;
       +    }
       +
       +    return FALSE;
        }
        
        static String
        SelectSwap(Widget w, String s)
        {
       -        GwinWidget gw;
       -        String ans;
       -
       -        gw = (GwinWidget)w;
       -        if(gw->gwin.selection){
       -                XtFree(gw->gwin.selection);
       -                gw->gwin.selection = 0;
       -        }
       -        XtGetSelectionValue(w, XA_PRIMARY, XA_STRING, SelCallback, 0,
       -                        XtLastTimestampProcessed(XtDisplay(w)));
       -
       -        while(gw->gwin.selection == 0)
       -                XtAppProcessEvent(XtWidgetToApplicationContext(w) , XtIMAll);
       -        ans = gw->gwin.selection;
       -        gw->gwin.selection = XtMalloc(strlen(s)+1);
       -        strcpy(gw->gwin.selection, s);
       -
       -        XtOwnSelection(w, XA_PRIMARY, XtLastTimestampProcessed(XtDisplay(w)),
       -                        SendSel, NULL, NULL);
       -
       -        return ans;
       +    GwinWidget gw;
       +    String ans;
       +
       +    gw = (GwinWidget)w;
       +    if(gw->gwin.selection){
       +        XtFree(gw->gwin.selection);
       +        gw->gwin.selection = 0;
       +    }
       +    XtGetSelectionValue(w, XA_PRIMARY, XA_STRING, SelCallback, 0,
       +            XtLastTimestampProcessed(XtDisplay(w)));
       +
       +    while(gw->gwin.selection == 0)
       +        XtAppProcessEvent(XtWidgetToApplicationContext(w) , XtIMAll);
       +    ans = gw->gwin.selection;
       +    gw->gwin.selection = XtMalloc(strlen(s)+1);
       +    strcpy(gw->gwin.selection, s);
       +
       +    XtOwnSelection(w, XA_PRIMARY, XtLastTimestampProcessed(XtDisplay(w)),
       +            SendSel, NULL, NULL);
       +
       +    return ans;
        }
        
        /* The returned answer should be free()ed when no longer needed */
        String
        GwinSelectionSwap(Widget w, String s)
        {
       -        XtCheckSubclass(w, gwinWidgetClass, NULL);
       -        return (*((GwinWidgetClass) XtClass(w))->gwin_class.select_swap)(w, s);
       +    XtCheckSubclass(w, gwinWidgetClass, NULL);
       +    return (*((GwinWidgetClass) XtClass(w))->gwin_class.select_swap)(w, s);
        }
 (DIR) diff --git a/libXg/latin1.c b/libXg/latin1.c
       @@ -11,8 +11,8 @@
        
        struct latin
        {
       -        unsigned short        l;
       -        unsigned char        c[2];
       +    unsigned short  l;
       +    unsigned char   c[2];
        };
        
        struct latin latintab[] = {
       @@ -232,7 +232,7 @@ struct latin *mappings = NULL;
        void
        freelatin(void)
        {
       -        free(mappings);
       +    free(mappings);
        }
        
        void
       @@ -255,12 +255,12 @@ initlatin(void)
            if (mappings)
                atexit(freelatin);
        
       -        FILE *keyboard = NULL;
       -        if (getenv("HOME")){
       -                char path[1024] = {0};
       -                snprintf(path, sizeof(path) - 1, "%s/.keyboard", getenv("HOME"));
       -                keyboard = fopen(path, "r");
       -        }
       +    FILE *keyboard = NULL;
       +    if (getenv("HOME")){
       +        char path[1024] = {0};
       +        snprintf(path, sizeof(path) - 1, "%s/.keyboard", getenv("HOME"));
       +        keyboard = fopen(path, "r");
       +    }
        
            if (!keyboard)
                return;
       @@ -270,13 +270,13 @@ initlatin(void)
            while (fscanf(keyboard, " %c%c %hx%*[^\n]\n", &c0, &c1, &l) == 3)
                addlatin(c0, c1, l);
        
       -        fclose(keyboard);
       +    fclose(keyboard);
        }
        
        long
        latin1(unsigned char *k)
        {
       -        struct latin *l;
       +    struct latin *l;
        
            for (l = mappings; l->l; l++)
                if (k[0] == l->c[0] && k[1] == l->c[1])
       @@ -286,26 +286,26 @@ latin1(unsigned char *k)
                if (k[0] == l->c[0] && k[1] == l->c[1])
                    return l->l;
        
       -        return -1;
       +    return -1;
        }
        
        int
        unicode(unsigned char *k)
        {
       -        int i, c;
       +    int i, c;
        
       -        k++;        /* skip 'X' */
       -        c = 0;
       -        for(i=0; i<4; i++,k++){
       -                c <<= 4;
       -                if('0'<=*k && *k<='9')
       -                        c += *k-'0';
       -                else if('a'<=*k && *k<='f')
       -                        c += 10 + *k-'a';
       -                else if('A'<=*k && *k<='F')
       -                        c += 10 + *k-'A';
       -                else
       -                        return -1;
       -        }
       -        return c;
       +    k++;    /* skip 'X' */
       +    c = 0;
       +    for(i=0; i<4; i++,k++){
       +        c <<= 4;
       +        if('0'<=*k && *k<='9')
       +            c += *k-'0';
       +        else if('a'<=*k && *k<='f')
       +            c += 10 + *k-'a';
       +        else if('A'<=*k && *k<='F')
       +            c += 10 + *k-'A';
       +        else
       +            return -1;
       +    }
       +    return c;
        }
 (DIR) diff --git a/libXg/ldconvert.c b/libXg/ldconvert.c
       @@ -7,49 +7,49 @@
        void
        _ldconvert(char *in, int inld, char *out, int outld, int w, int h)
        {
       -        int        a, b, i, j, i1, j1, j2, mask;
       -        int        ind, inl, outd, outl;
       -        int        hh, ww;
       -        char        *p, *q;
       +    int a, b, i, j, i1, j1, j2, mask;
       +    int ind, inl, outd, outl;
       +    int hh, ww;
       +    char    *p, *q;
        
       -        i1 = 8 >> inld;
       -        j1 = 8 >> outld;
       -        ind = 1 << inld;
       -        outd = 1 << outld;
       -        inl = ((w << inld) + 7)/8;
       -        outl = ((w << outld) + 7)/8;
       -        b = 0;
       +    i1 = 8 >> inld;
       +    j1 = 8 >> outld;
       +    ind = 1 << inld;
       +    outd = 1 << outld;
       +    inl = ((w << inld) + 7)/8;
       +    outl = ((w << outld) + 7)/8;
       +    b = 0;
        
       -        if (ind > outd) {
       -                mask = 256 - (256 >> outd);
       -                for (hh = 0; hh < h; hh++, in += inl, out += outl)
       -                        for (p = in, q = out, ww = 0; ww < w; ww++) {
       -                                for (j = j1; j > 0; ) {
       -                                        a = *p++;
       -                                        for (i = i1; i > 0; i--, j--) {
       -                                                b |= a & mask;
       -                                                a <<= ind;
       -                                                b <<= outd;
       -                                        }
       -                                }
       -                                *q++ = (b >> 8);
       -                        }
       -        } else {
       -                j2 = 1 << (outld - inld);
       -                mask = 256 - (256 >> ind);
       -                for (hh = 0; hh < h; hh++, in += inl, out += outl)
       -                        for (p = in, q = out, ww = 0; ww < w; ww++) {
       -                                a = *p++;
       -                                for (i = i1; i > 0; ) {
       -                                        for (j = j1; j > 0; j--, i--) {
       -                                                b |= a & mask;
       -                                                a <<= ind;
       -                                                b <<= outd;
       -                                        }
       -                                        for (j = j2; j > 0; j--)
       -                                                b |= (b << ind);
       -                                        *q++ = (b >> 8);
       -                                }
       -                        }
       -        }
       +    if (ind > outd) {
       +        mask = 256 - (256 >> outd);
       +        for (hh = 0; hh < h; hh++, in += inl, out += outl)
       +            for (p = in, q = out, ww = 0; ww < w; ww++) {
       +                for (j = j1; j > 0; ) {
       +                    a = *p++;
       +                    for (i = i1; i > 0; i--, j--) {
       +                        b |= a & mask;
       +                        a <<= ind;
       +                        b <<= outd;
       +                    }
       +                }
       +                *q++ = (b >> 8);
       +            }
       +    } else {
       +        j2 = 1 << (outld - inld);
       +        mask = 256 - (256 >> ind);
       +        for (hh = 0; hh < h; hh++, in += inl, out += outl)
       +            for (p = in, q = out, ww = 0; ww < w; ww++) {
       +                a = *p++;
       +                for (i = i1; i > 0; ) {
       +                    for (j = j1; j > 0; j--, i--) {
       +                        b |= a & mask;
       +                        a <<= ind;
       +                        b <<= outd;
       +                    }
       +                    for (j = j2; j > 0; j--)
       +                        b |= (b << ind);
       +                    *q++ = (b >> 8);
       +                }
       +            }
       +    }
        }
 (DIR) diff --git a/libXg/libgint.h b/libXg/libgint.h
       @@ -17,8 +17,8 @@
        #define Font xFont
        #define Event xEvent
        
       -#if        defined(v10) || defined(HPUX)
       -typedef        char*        caddr_t;
       +#if defined(v10) || defined(HPUX)
       +typedef char*   caddr_t;
        #endif
        
        #include <X11/Xlib.h>
       @@ -33,28 +33,28 @@ typedef        char*        caddr_t;
        void initcursors(void);
        
        /* Return a GCs for solid filling/strings/etc., segments/points, and tiling */
       -extern GC        _getfillgc(Fcode, Bitmap*, unsigned long);
       -extern GC        _getcopygc(Fcode, Bitmap*, Bitmap*, int*);
       -extern GC        _getfillgc2(Fcode, Bitmap*, unsigned long, unsigned long, unsigned long);
       -extern GC        _getcopygc2(Fcode, Bitmap*, Bitmap*, int*, unsigned long, unsigned long);
       -extern GC        _getgc(Bitmap*, unsigned long, XGCValues *);
       +extern GC   _getfillgc(Fcode, Bitmap*, unsigned long);
       +extern GC   _getcopygc(Fcode, Bitmap*, Bitmap*, int*);
       +extern GC   _getfillgc2(Fcode, Bitmap*, unsigned long, unsigned long, unsigned long);
       +extern GC   _getcopygc2(Fcode, Bitmap*, Bitmap*, int*, unsigned long, unsigned long);
       +extern GC   _getgc(Bitmap*, unsigned long, XGCValues *);
        
        /* convert between different bitmap depths */
       -extern void        _ldconvert(char *, int, char *, int, int, int);
       +extern void _ldconvert(char *, int, char *, int, int, int);
        
        /* balloc without zero init (which uses a gc!) */
       -extern Bitmap        *_balloc(Rectangle, int);
       +extern Bitmap   *_balloc(Rectangle, int);
        
        /* X Display for this application's connection */
       -extern Display        *_dpy;
       +extern Display  *_dpy;
        
        /* screen depth foreground and background for this application */
       -extern unsigned long        _fgpixel, _bgpixel;
       -extern XColor                _fgcolor, _bgcolor;
       +extern unsigned long    _fgpixel, _bgpixel;
       +extern XColor       _fgcolor, _bgcolor;
        
        /* indexed by log depth (0 <= ld <= 5), to give depth and planemask */
       -extern int                _ld2d[];
       -extern unsigned long        _ld2dmask[];
       +extern int      _ld2d[];
       +extern unsigned long    _ld2dmask[];
        
        /* libg.h defines:
         *   extern Bitmap screen;   -- Bitmap for application Window after xbinit()
       @@ -78,17 +78,17 @@ extern unsigned long        _ld2dmask[];
        
        /* values for bitmap flag field (see _getcopygc if change first two vals) */
        enum {
       -        DP1=        0x1,        /* depth == 1 (ldepth == 0) */
       -        BL1=        0x2,        /* black == 1 model */
       -        SCR=        0x4,        /* on screen */
       -        ZORG=        0x8,        /* r.min == Pt(0,0) */
       -        SHIFT= 0x20,        /* !SCR & !ZORG */
       -        CLIP=  0x40        /* r != clipr */
       +    DP1=    0x1,    /* depth == 1 (ldepth == 0) */
       +    BL1=    0x2,    /* black == 1 model */
       +    SCR=    0x4,    /* on screen */
       +    ZORG=   0x8,    /* r.min == Pt(0,0) */
       +    SHIFT= 0x20,    /* !SCR & !ZORG */
       +    CLIP=  0x40 /* r != clipr */
        };
        
        /* values for return bltfunc arg of _getcopygc */
        enum {
       -        UseCopyArea,
       -        UseCopyPlane,
       -        UseFillRectangle
       +    UseCopyArea,
       +    UseCopyPlane,
       +    UseFillRectangle
        };
 (DIR) diff --git a/libXg/menuhit.c b/libXg/menuhit.c
       @@ -6,28 +6,28 @@
        
        enum
        {
       -        Margin = 3,                /* outside to text */
       -        Border = 2,                /* outside to selection boxes */
       -        Blackborder = 1,        /* width of outlining border */
       -        Vspacing = 1,                /* extra spacing between lines of text */
       -        Maxunscroll = 25,        /* maximum #entries before scrolling turns on */
       -        Nscroll = 20,                /* number entries in scrolling part */
       -        Scrollwid = 14,                /* width of scroll bar */
       -        Gap = 4                        /* between text and scroll bar */
       +    Margin = 3,     /* outside to text */
       +    Border = 2,     /* outside to selection boxes */
       +    Blackborder = 1,    /* width of outlining border */
       +    Vspacing = 1,       /* extra spacing between lines of text */
       +    Maxunscroll = 25,   /* maximum #entries before scrolling turns on */
       +    Nscroll = 20,       /* number entries in scrolling part */
       +    Scrollwid = 14,     /* width of scroll bar */
       +    Gap = 4         /* between text and scroll bar */
        };
        
       -static        Bitmap        *menutxt;
       +static  Bitmap  *menutxt;
        
       -static        uchar menutxtbits[] = {
       -        0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
       -        0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
       -        0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
       -        0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
       +static  uchar menutxtbits[] = {
       +    0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
       +    0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
       +    0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
       +    0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
        };
        
        static int
        fontheight() {
       -        return font->ascent + font->descent;
       +    return font->ascent + font->descent;
        }
        
        /*
       @@ -37,11 +37,11 @@ fontheight() {
        static Rectangle
        menurect(Rectangle r, int i)
        {
       -        if(i < 0)
       -                return Rect(0, 0, 0, 0);
       -        r.min.y += (fontheight()+Vspacing)*i;
       -        r.max.y = r.min.y+fontheight()+Vspacing;
       -        return inset(r, Border-Margin);
       +    if(i < 0)
       +        return Rect(0, 0, 0, 0);
       +    r.min.y += (fontheight()+Vspacing)*i;
       +    r.max.y = r.min.y+fontheight()+Vspacing;
       +    return inset(r, Border-Margin);
        }
        
        /*
       @@ -51,9 +51,9 @@ menurect(Rectangle r, int i)
        static int
        menusel(Rectangle r, Point p)
        {
       -        if(!ptinrect(p, r))
       -                return -1;
       -        return (p.y-r.min.y)/(fontheight()+Vspacing);
       +    if(!ptinrect(p, r))
       +        return -1;
       +    return (p.y-r.min.y)/(fontheight()+Vspacing);
        }
        
        /*
       @@ -65,177 +65,177 @@ menusel(Rectangle r, Point p)
        static int
        menuscan(int but, Mouse *m, Rectangle menur, int lasti)
        {
       -        int i;
       -        Rectangle r;
       +    int i;
       +    Rectangle r;
        
       -        r = menurect(menur, lasti);
       -        bitblt(&screen, r.min, &screen, r, F&~D);
       -        *m = emouse();
       -        while(m->buttons & (1<<(but-1))){
       -                *m = emouse();
       -                i = menusel(menur, m->xy);
       -                if(i == lasti)
       -                        continue;
       -                bitblt(&screen, r.min, &screen, r, F&~D);
       -                if(i == -1)
       -                        return i;
       -                r = menurect(menur, i);
       -                bitblt(&screen, r.min, &screen, r, F&~D);
       -                lasti = i;
       -        }
       -        return lasti;
       +    r = menurect(menur, lasti);
       +    bitblt(&screen, r.min, &screen, r, F&~D);
       +    *m = emouse();
       +    while(m->buttons & (1<<(but-1))){
       +        *m = emouse();
       +        i = menusel(menur, m->xy);
       +        if(i == lasti)
       +            continue;
       +        bitblt(&screen, r.min, &screen, r, F&~D);
       +        if(i == -1)
       +            return i;
       +        r = menurect(menur, i);
       +        bitblt(&screen, r.min, &screen, r, F&~D);
       +        lasti = i;
       +    }
       +    return lasti;
        }
        
        void
        menupaint(Menu *menu, Rectangle textr, int off, int nitemdrawn)
        {
       -        int i;
       -        Point pt;
       -        Rectangle r;
       -        char *item;
       +    int i;
       +    Point pt;
       +    Rectangle r;
       +    char *item;
        
       -        r = inset(textr, Border-Margin);
       -        bitblt(&screen, r.min, &screen, r, 0);
       -        pt = Pt(textr.min.x+textr.max.x, textr.min.y);
       -        for(i = 0; i<nitemdrawn; i++, pt.y += fontheight()+Vspacing){
       -                item = menu->item? menu->item[i+off] : (*menu->gen)(i+off);
       -                string(&screen,
       -                        Pt((pt.x-strwidth(font, item))/2, pt.y),
       -                        font, item, S);
       -        }
       +    r = inset(textr, Border-Margin);
       +    bitblt(&screen, r.min, &screen, r, 0);
       +    pt = Pt(textr.min.x+textr.max.x, textr.min.y);
       +    for(i = 0; i<nitemdrawn; i++, pt.y += fontheight()+Vspacing){
       +        item = menu->item? menu->item[i+off] : (*menu->gen)(i+off);
       +        string(&screen,
       +            Pt((pt.x-strwidth(font, item))/2, pt.y),
       +            font, item, S);
       +    }
        }
        
        static void
        menuscrollpaint(Rectangle scrollr, int off, int nitem, int nitemdrawn)
        {
       -        Rectangle r;
       +    Rectangle r;
        
       -        bitblt(&screen, scrollr.min, &screen, scrollr, 0);
       -        r.min.x = scrollr.min.x;
       -        r.max.x = scrollr.max.x;
       -        r.min.y = scrollr.min.y + (Dy(scrollr)*off)/nitem;
       -        r.max.y = scrollr.min.y + (Dy(scrollr)*(off+nitemdrawn))/nitem;
       -        if(r.max.y < r.min.y+2)
       -                r.max.y = r.min.y+2;
       -        border(&screen, r, 1, F, _bgpixel);
       -        if(menutxt == 0){
       -                menutxt = balloc(Rect(0, 0, 16, 16), 0);
       -                if(menutxt)
       -                        wrbitmap(menutxt, 0, 16, menutxtbits);
       -        }
       -        if(menutxt)
       -                texture(&screen, inset(r, 1), menutxt, S);
       +    bitblt(&screen, scrollr.min, &screen, scrollr, 0);
       +    r.min.x = scrollr.min.x;
       +    r.max.x = scrollr.max.x;
       +    r.min.y = scrollr.min.y + (Dy(scrollr)*off)/nitem;
       +    r.max.y = scrollr.min.y + (Dy(scrollr)*(off+nitemdrawn))/nitem;
       +    if(r.max.y < r.min.y+2)
       +        r.max.y = r.min.y+2;
       +    border(&screen, r, 1, F, _bgpixel);
       +    if(menutxt == 0){
       +        menutxt = balloc(Rect(0, 0, 16, 16), 0);
       +        if(menutxt)
       +            wrbitmap(menutxt, 0, 16, menutxtbits);
       +    }
       +    if(menutxt)
       +        texture(&screen, inset(r, 1), menutxt, S);
        }
        
        int
        menuhit(int but, Mouse *m, Menu *menu)
        {
       -        int i, nitem, nitemdrawn, maxwid, lasti, off, noff, wid, screenitem;
       -        int scrolling;
       -        Rectangle r, menur, sc, textr, scrollr;
       -        Bitmap *b;
       -        Point pt;
       -        char *item;
       +    int i, nitem, nitemdrawn, maxwid, lasti, off, noff, wid, screenitem;
       +    int scrolling;
       +    Rectangle r, menur, sc, textr, scrollr;
       +    Bitmap *b;
       +    Point pt;
       +    char *item;
        
       -        sc = screen.clipr;
       -        clipr(&screen, screen.r);
       -        maxwid = 0;
       -        for(nitem = 0;
       -            item = menu->item? menu->item[nitem] : (*menu->gen)(nitem);
       -            nitem++){
       -                i = strwidth(font, item);
       -                if(i > maxwid)
       -                        maxwid = i;
       -        }
       -        if(menu->lasthit<0 || menu->lasthit>=nitem)
       -                menu->lasthit = 0;
       -        screenitem = (Dy(screen.r)-10)/(fontheight()+Vspacing);
       -        if(nitem>Maxunscroll || nitem>screenitem){
       -                scrolling = 1;
       -                nitemdrawn = Nscroll;
       -                if(nitemdrawn > screenitem)
       -                        nitemdrawn = screenitem;
       -                wid = maxwid + Gap + Scrollwid;
       -                off = menu->lasthit - nitemdrawn/2;
       -                if(off < 0)
       -                        off = 0;
       -                if(off > nitem-nitemdrawn)
       -                        off = nitem-nitemdrawn;
       -                lasti = menu->lasthit-off;
       -        }else{
       -                scrolling = 0;
       -                nitemdrawn = nitem;
       -                wid = maxwid;
       -                off = 0;
       -                lasti = menu->lasthit;
       -        }
       -        r = inset(Rect(0, 0, wid, nitemdrawn*(fontheight()+Vspacing)), -Margin);
       -        r = rsubp(r, Pt(wid/2, lasti*(fontheight()+Vspacing)+fontheight()/2));
       -        r = raddp(r, m->xy);
       -        pt = Pt(0, 0);
       -        if(r.max.x>screen.r.max.x)
       -                pt.x = screen.r.max.x-r.max.x;
       -        if(r.max.y>screen.r.max.y)
       -                pt.y = screen.r.max.y-r.max.y;
       -        if(r.min.x<screen.r.min.x)
       -                pt.x = screen.r.min.x-r.min.x;
       -        if(r.min.y<screen.r.min.y)
       -                pt.y = screen.r.min.y-r.min.y;
       -        menur = raddp(r, pt);
       -        textr.max.x = menur.max.x-Margin;
       -        textr.min.x = textr.max.x-maxwid;
       -        textr.min.y = menur.min.y+Margin;
       -        textr.max.y = textr.min.y + nitemdrawn*(fontheight()+Vspacing);
       -        if(scrolling){
       -                scrollr = inset(menur, Border);
       -                scrollr.max.x = scrollr.min.x+Scrollwid;
       -        }else
       -                scrollr = Rect(0, 0, 0, 0);
       +    sc = screen.clipr;
       +    clipr(&screen, screen.r);
       +    maxwid = 0;
       +    for(nitem = 0;
       +        item = menu->item? menu->item[nitem] : (*menu->gen)(nitem);
       +        nitem++){
       +        i = strwidth(font, item);
       +        if(i > maxwid)
       +            maxwid = i;
       +    }
       +    if(menu->lasthit<0 || menu->lasthit>=nitem)
       +        menu->lasthit = 0;
       +    screenitem = (Dy(screen.r)-10)/(fontheight()+Vspacing);
       +    if(nitem>Maxunscroll || nitem>screenitem){
       +        scrolling = 1;
       +        nitemdrawn = Nscroll;
       +        if(nitemdrawn > screenitem)
       +            nitemdrawn = screenitem;
       +        wid = maxwid + Gap + Scrollwid;
       +        off = menu->lasthit - nitemdrawn/2;
       +        if(off < 0)
       +            off = 0;
       +        if(off > nitem-nitemdrawn)
       +            off = nitem-nitemdrawn;
       +        lasti = menu->lasthit-off;
       +    }else{
       +        scrolling = 0;
       +        nitemdrawn = nitem;
       +        wid = maxwid;
       +        off = 0;
       +        lasti = menu->lasthit;
       +    }
       +    r = inset(Rect(0, 0, wid, nitemdrawn*(fontheight()+Vspacing)), -Margin);
       +    r = rsubp(r, Pt(wid/2, lasti*(fontheight()+Vspacing)+fontheight()/2));
       +    r = raddp(r, m->xy);
       +    pt = Pt(0, 0);
       +    if(r.max.x>screen.r.max.x)
       +        pt.x = screen.r.max.x-r.max.x;
       +    if(r.max.y>screen.r.max.y)
       +        pt.y = screen.r.max.y-r.max.y;
       +    if(r.min.x<screen.r.min.x)
       +        pt.x = screen.r.min.x-r.min.x;
       +    if(r.min.y<screen.r.min.y)
       +        pt.y = screen.r.min.y-r.min.y;
       +    menur = raddp(r, pt);
       +    textr.max.x = menur.max.x-Margin;
       +    textr.min.x = textr.max.x-maxwid;
       +    textr.min.y = menur.min.y+Margin;
       +    textr.max.y = textr.min.y + nitemdrawn*(fontheight()+Vspacing);
       +    if(scrolling){
       +        scrollr = inset(menur, Border);
       +        scrollr.max.x = scrollr.min.x+Scrollwid;
       +    }else
       +        scrollr = Rect(0, 0, 0, 0);
        
       -        b = balloc(menur, screen.ldepth);
       -        if(b == 0)
       -                b = &screen;
       -        bitblt(b, menur.min, &screen, menur, S);
       -        bitblt(&screen, menur.min, &screen, menur, 0);
       -        border(&screen, menur, Blackborder, F, _bgpixel);
       -        r = menurect(textr, lasti);
       -        cursorset(divpt(add(r.min, r.max), 2));
       -        menupaint(menu, textr, off, nitemdrawn);
       -        if(scrolling)
       -                menuscrollpaint(scrollr, off, nitem, nitemdrawn);
       -        r = menurect(textr, lasti);
       -        cursorset(divpt(add(r.min, r.max), 2));
       -        menupaint(menu, textr, off, nitemdrawn);
       -        if(scrolling)
       -                menuscrollpaint(scrollr, off, nitem, nitemdrawn);
       -        while(m->buttons & (1<<(but-1))){
       -                lasti = menuscan(but, m, textr, lasti);
       -                if(lasti >= 0)
       -                        break;
       -                while(!ptinrect(m->xy, textr) && (m->buttons & (1<<(but-1)))){
       -                        if(scrolling && ptinrect(m->xy, scrollr)){
       -                                noff = ((m->xy.y-scrollr.min.y)*nitem)/Dy(scrollr);
       -                                noff -= nitemdrawn/2;
       -                                if(noff < 0)
       -                                        noff = 0;
       -                                if(noff > nitem-nitemdrawn)
       -                                        noff = nitem-nitemdrawn;
       -                                if(noff != off){
       -                                        off = noff;
       -                                        menupaint(menu, textr, off, nitemdrawn);
       -                                        menuscrollpaint(scrollr, off, nitem, nitemdrawn);
       -                                }
       -                        }
       -                        *m = emouse();
       -                }
       -        }
       -        bitblt(&screen, menur.min, b, menur, S);
       -        if(b != &screen)
       -                bfree(b);
       -        clipr(&screen, sc);
       -        if(lasti >= 0){
       -                menu->lasthit = lasti+off;
       -                return menu->lasthit;
       -        }
       -        return -1;
       +    b = balloc(menur, screen.ldepth);
       +    if(b == 0)
       +        b = &screen;
       +    bitblt(b, menur.min, &screen, menur, S);
       +    bitblt(&screen, menur.min, &screen, menur, 0);
       +    border(&screen, menur, Blackborder, F, _bgpixel);
       +    r = menurect(textr, lasti);
       +    cursorset(divpt(add(r.min, r.max), 2));
       +    menupaint(menu, textr, off, nitemdrawn);
       +    if(scrolling)
       +        menuscrollpaint(scrollr, off, nitem, nitemdrawn);
       +    r = menurect(textr, lasti);
       +    cursorset(divpt(add(r.min, r.max), 2));
       +    menupaint(menu, textr, off, nitemdrawn);
       +    if(scrolling)
       +        menuscrollpaint(scrollr, off, nitem, nitemdrawn);
       +    while(m->buttons & (1<<(but-1))){
       +        lasti = menuscan(but, m, textr, lasti);
       +        if(lasti >= 0)
       +            break;
       +        while(!ptinrect(m->xy, textr) && (m->buttons & (1<<(but-1)))){
       +            if(scrolling && ptinrect(m->xy, scrollr)){
       +                noff = ((m->xy.y-scrollr.min.y)*nitem)/Dy(scrollr);
       +                noff -= nitemdrawn/2;
       +                if(noff < 0)
       +                    noff = 0;
       +                if(noff > nitem-nitemdrawn)
       +                    noff = nitem-nitemdrawn;
       +                if(noff != off){
       +                    off = noff;
       +                    menupaint(menu, textr, off, nitemdrawn);
       +                    menuscrollpaint(scrollr, off, nitem, nitemdrawn);
       +                }
       +            }
       +            *m = emouse();
       +        }
       +    }
       +    bitblt(&screen, menur.min, b, menur, S);
       +    if(b != &screen)
       +        bfree(b);
       +    clipr(&screen, sc);
       +    if(lasti >= 0){
       +        menu->lasthit = lasti+off;
       +        return menu->lasthit;
       +    }
       +    return -1;
        }
 (DIR) diff --git a/libXg/point.c b/libXg/point.c
       @@ -7,15 +7,15 @@
        void
        point(Bitmap *b, Point p, int v, Fcode f)
        {
       -        int x, y;
       -        GC g;
       +    int x, y;
       +    GC g;
        
       -        x = p.x;
       -        y = p.y;
       -        if(b->flag&SHIFT){
       -                x -= b->r.min.x;
       -                y -= b->r.min.y;
       -        }
       -        g = _getfillgc(f, b, v);
       -        XDrawPoint(_dpy, (Drawable)b->id, g, x, y);
       +    x = p.x;
       +    y = p.y;
       +    if(b->flag&SHIFT){
       +        x -= b->r.min.x;
       +        y -= b->r.min.y;
       +    }
       +    g = _getfillgc(f, b, v);
       +    XDrawPoint(_dpy, (Drawable)b->id, g, x, y);
        }
 (DIR) diff --git a/libXg/polysegment.c b/libXg/polysegment.c
       @@ -7,21 +7,21 @@
        void
        polysegment(Bitmap *d, int n, Point *pp, int v, Fcode f)
        {
       -        XPoint *xp;
       -        int i;
       -        GC g;
       +    XPoint *xp;
       +    int i;
       +    GC g;
        
       -        if (!(xp = (XPoint *)calloc(n, sizeof(XPoint))))
       -                berror("polysegment: could not allocate XPoints");
       -        for (i = 0; i < n; i++, pp++)
       -                if(d->flag&SHIFT){
       -                        xp[i].x = pp->x - d->r.min.x;
       -                        xp[i].y = pp->y - d->r.min.y;
       -                } else {
       -                        xp[i].x = pp->x;
       -                        xp[i].y = pp->y;
       -                }
       -        g = _getfillgc(f, d, v);
       -        XDrawLines(_dpy, (Drawable)d->id, g, xp, n, CoordModeOrigin);
       -        free(xp);
       +    if (!(xp = (XPoint *)calloc(n, sizeof(XPoint))))
       +        berror("polysegment: could not allocate XPoints");
       +    for (i = 0; i < n; i++, pp++)
       +        if(d->flag&SHIFT){
       +            xp[i].x = pp->x - d->r.min.x;
       +            xp[i].y = pp->y - d->r.min.y;
       +        } else {
       +            xp[i].x = pp->x;
       +            xp[i].y = pp->y;
       +        }
       +    g = _getfillgc(f, d, v);
       +    XDrawLines(_dpy, (Drawable)d->id, g, xp, n, CoordModeOrigin);
       +    free(xp);
        }
 (DIR) diff --git a/libXg/rdbitmap.c b/libXg/rdbitmap.c
       @@ -7,57 +7,57 @@
        void
        rdbitmap(Bitmap *b, int miny, int maxy, unsigned char *data)
        {
       -        XImage *gim, *eim;
       -        int x, y, w, h, pix, l, offset, px;
       -        int inld, outld;
       -        char *tdata;
       +    XImage *gim, *eim;
       +    int x, y, w, h, pix, l, offset, px;
       +    int inld, outld;
       +    char *tdata;
        
       -        /*
       -         * The XGetImage returned image may be wrong in a number of ways:
       -         * wrong bit order, byte order, bit pad, scanline pad,
       -         * and constant shift.
       -         * So use a SLOW loop, for now
       -         */
       -        w = Dx(b->r);
       -        h = maxy - miny;
       -        outld = b->ldepth;
       -        inld = (b->ldepth == 0) ? 0 : screen.ldepth;
       -        gim = XGetImage(_dpy, (Drawable)b->id, 0, miny - b->r.min.y,
       -                        w, h, ~0, ZPixmap);
       -        px = 1<<(3-outld);        /* pixels per byte */
       -        /* set l to number of bytes of data per scan line */
       -        if(b->r.min.x >= 0)
       -                offset = b->r.min.x % px;
       -        else
       -                offset = px - b->r.min.x % px;
       -        l = (-b->r.min.x+px-1)/px;
       -        if(b->r.max.x >= 0)
       -                l += (b->r.max.x+px-1)/px;
       -        else
       -                l -= b->r.max.x/px;
       -        l *= h;
       -        if(l <= 0)
       -                return;
       -        tdata = (char *)malloc(l);
       -        if (tdata == (char *) 0)
       -                berror("rdbitmap malloc");
       -        eim = XCreateImage(_dpy, 0, 1 << inld, ZPixmap, 0, tdata,
       -                        w+offset, h, 8, 0);
       -        eim->bitmap_pad = 8;
       -        eim->bitmap_bit_order = MSBFirst;
       -        eim->byte_order = MSBFirst;
       +    /*
       +     * The XGetImage returned image may be wrong in a number of ways:
       +     * wrong bit order, byte order, bit pad, scanline pad,
       +     * and constant shift.
       +     * So use a SLOW loop, for now
       +     */
       +    w = Dx(b->r);
       +    h = maxy - miny;
       +    outld = b->ldepth;
       +    inld = (b->ldepth == 0) ? 0 : screen.ldepth;
       +    gim = XGetImage(_dpy, (Drawable)b->id, 0, miny - b->r.min.y,
       +            w, h, ~0, ZPixmap);
       +    px = 1<<(3-outld);  /* pixels per byte */
       +    /* set l to number of bytes of data per scan line */
       +    if(b->r.min.x >= 0)
       +        offset = b->r.min.x % px;
       +    else
       +        offset = px - b->r.min.x % px;
       +    l = (-b->r.min.x+px-1)/px;
       +    if(b->r.max.x >= 0)
       +        l += (b->r.max.x+px-1)/px;
       +    else
       +        l -= b->r.max.x/px;
       +    l *= h;
       +    if(l <= 0)
       +        return;
       +    tdata = (char *)malloc(l);
       +    if (tdata == (char *) 0)
       +        berror("rdbitmap malloc");
       +    eim = XCreateImage(_dpy, 0, 1 << inld, ZPixmap, 0, tdata,
       +            w+offset, h, 8, 0);
       +    eim->bitmap_pad = 8;
       +    eim->bitmap_bit_order = MSBFirst;
       +    eim->byte_order = MSBFirst;
        
       -        for(y = 0; y < h; y++)
       -                for(x = 0; x < w; x++) {
       -                        pix = XGetPixel(gim, x, y);
       -                        XPutPixel(eim, x+offset, y, pix);
       -                }
       +    for(y = 0; y < h; y++)
       +        for(x = 0; x < w; x++) {
       +            pix = XGetPixel(gim, x, y);
       +            XPutPixel(eim, x+offset, y, pix);
       +        }
        
       -        if (inld == outld)
       -                memcpy((char *)data, tdata, l);
       -        else
       -                _ldconvert(tdata, inld, (char*)data, outld, w, h);
       +    if (inld == outld)
       +        memcpy((char *)data, tdata, l);
       +    else
       +        _ldconvert(tdata, inld, (char*)data, outld, w, h);
        
       -        XDestroyImage(gim);
       -        XDestroyImage(eim);
       +    XDestroyImage(gim);
       +    XDestroyImage(eim);
        }
 (DIR) diff --git a/libXg/rdbitmapfile.c b/libXg/rdbitmapfile.c
       @@ -9,57 +9,57 @@
        Bitmap*
        rdbitmapfile(int fd)
        {
       -        char hdr[5*12+1];
       -        unsigned char *data;
       -        long dy, px;
       -        unsigned long l, t, n;
       -        long miny, maxy;
       -        Rectangle r;
       -        int ld;
       -        Bitmap *b;
       +    char hdr[5*12+1];
       +    unsigned char *data;
       +    long dy, px;
       +    unsigned long l, t, n;
       +    long miny, maxy;
       +    Rectangle r;
       +    int ld;
       +    Bitmap *b;
        
       -        if(read(fd, hdr, 5*12)!=5*12)
       -                berror("rdbitmapfile read");
       -        ld = atoi(hdr+0*12);
       -        r.min.x = atoi(hdr+1*12);
       -        r.min.y = atoi(hdr+2*12);
       -        r.max.x = atoi(hdr+3*12);
       -        r.max.y = atoi(hdr+4*12);
       -        if(ld<0 || ld>1)
       -                berror("rdbitmapfile ldepth");
       -        if(r.min.x>r.max.x || r.min.y>r.max.y)
       -                berror("rdbitmapfile rectangle");
       +    if(read(fd, hdr, 5*12)!=5*12)
       +        berror("rdbitmapfile read");
       +    ld = atoi(hdr+0*12);
       +    r.min.x = atoi(hdr+1*12);
       +    r.min.y = atoi(hdr+2*12);
       +    r.max.x = atoi(hdr+3*12);
       +    r.max.y = atoi(hdr+4*12);
       +    if(ld<0 || ld>1)
       +        berror("rdbitmapfile ldepth");
       +    if(r.min.x>r.max.x || r.min.y>r.max.y)
       +        berror("rdbitmapfile rectangle");
        
       -        miny = r.min.y;
       -        maxy = r.max.y;
       -        px = 1<<(3-ld);        /* pixels per byte */
       -        /* set l to number of bytes of data per scan line */
       -        if(r.min.x >= 0)
       -                l = (r.max.x+px-1)/px - r.min.x/px;
       -        else{        /* make positive before divide */
       -                t = (-r.min.x)+px-1;
       -                t = (t/px)*px;
       -                l = (t+r.max.x+px-1)/px;
       -        }
       -        b = balloc(r, ld);
       -        if(b == 0)
       -                return 0;
       -        data = (unsigned char *)malloc(CHUNK);
       -        if(data == 0)
       -                berror("rdbitmapfile malloc");
       -        while(maxy > miny){
       -                dy = maxy - miny;
       -                if(dy*l > CHUNK)
       -                        dy = CHUNK/l;
       -                n = dy*l;
       -                if(read(fd, data, n) != n){
       -                        free(data);
       -                        bfree(b);
       -                        berror("rdbitmapfile read");
       -                }
       -                wrbitmap(b, miny, miny+dy, data);
       -                miny += dy;
       -        }
       -        free(data);
       -        return b;
       +    miny = r.min.y;
       +    maxy = r.max.y;
       +    px = 1<<(3-ld); /* pixels per byte */
       +    /* set l to number of bytes of data per scan line */
       +    if(r.min.x >= 0)
       +        l = (r.max.x+px-1)/px - r.min.x/px;
       +    else{   /* make positive before divide */
       +        t = (-r.min.x)+px-1;
       +        t = (t/px)*px;
       +        l = (t+r.max.x+px-1)/px;
       +    }
       +    b = balloc(r, ld);
       +    if(b == 0)
       +        return 0;
       +    data = (unsigned char *)malloc(CHUNK);
       +    if(data == 0)
       +        berror("rdbitmapfile malloc");
       +    while(maxy > miny){
       +        dy = maxy - miny;
       +        if(dy*l > CHUNK)
       +            dy = CHUNK/l;
       +        n = dy*l;
       +        if(read(fd, data, n) != n){
       +            free(data);
       +            bfree(b);
       +            berror("rdbitmapfile read");
       +        }
       +        wrbitmap(b, miny, miny+dy, data);
       +        miny += dy;
       +    }
       +    free(data);
       +    return b;
        }
 (DIR) diff --git a/libXg/rectclip.c b/libXg/rectclip.c
       @@ -3,23 +3,23 @@
        #include <libc.h>
        #include <libg.h>
        
       -int rectclip(Rectangle *rp, Rectangle b)                /* first by reference, second by value */
       +int rectclip(Rectangle *rp, Rectangle b)        /* first by reference, second by value */
        {
       -        Rectangle *bp = &b;
       -        /*
       -         * Expand rectXrect() in line for speed
       -         */
       -        if((rp->min.x<bp->max.x && bp->min.x<rp->max.x &&
       -            rp->min.y<bp->max.y && bp->min.y<rp->max.y)==0)
       -                return 0;
       -        /* They must overlap */
       -        if(rp->min.x < bp->min.x)
       -                rp->min.x = bp->min.x;
       -        if(rp->min.y < bp->min.y)
       -                rp->min.y = bp->min.y;
       -        if(rp->max.x > bp->max.x)
       -                rp->max.x = bp->max.x;
       -        if(rp->max.y > bp->max.y)
       -                rp->max.y = bp->max.y;
       -        return 1;
       +    Rectangle *bp = &b;
       +    /*
       +     * Expand rectXrect() in line for speed
       +     */
       +    if((rp->min.x<bp->max.x && bp->min.x<rp->max.x &&
       +        rp->min.y<bp->max.y && bp->min.y<rp->max.y)==0)
       +        return 0;
       +    /* They must overlap */
       +    if(rp->min.x < bp->min.x)
       +        rp->min.x = bp->min.x;
       +    if(rp->min.y < bp->min.y)
       +        rp->min.y = bp->min.y;
       +    if(rp->max.x > bp->max.x)
       +        rp->max.x = bp->max.x;
       +    if(rp->max.y > bp->max.y)
       +        rp->max.y = bp->max.y;
       +    return 1;
        }
 (DIR) diff --git a/libXg/rune.c b/libXg/rune.c
       @@ -1,213 +1,213 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
       -#include        <u.h>
       -#include        <libc.h>
       +#include    <u.h>
       +#include    <libc.h>
        #include <string.h>
        
        enum
        {
       -        Bit1        = 7,
       -        Bitx        = 6,
       -        Bit2        = 5,
       -        Bit3        = 4,
       -        Bit4        = 3,
       -
       -        T1        = ((1<<(Bit1+1))-1) ^ 0xFF,        /* 0000 0000 */
       -        Tx        = ((1<<(Bitx+1))-1) ^ 0xFF,        /* 1000 0000 */
       -        T2        = ((1<<(Bit2+1))-1) ^ 0xFF,        /* 1100 0000 */
       -        T3        = ((1<<(Bit3+1))-1) ^ 0xFF,        /* 1110 0000 */
       -        T4        = ((1<<(Bit4+1))-1) ^ 0xFF,        /* 1111 0000 */
       -
       -        Rune1        = (1<<(Bit1+0*Bitx))-1,                /* 0000 0000 0111 1111 */
       -        Rune2        = (1<<(Bit2+1*Bitx))-1,                /* 0000 0111 1111 1111 */
       -        Rune3        = (1<<(Bit3+2*Bitx))-1,                /* 1111 1111 1111 1111 */
       -
       -        Maskx        = (1<<Bitx)-1,                        /* 0011 1111 */
       -        Testx        = Maskx ^ 0xFF,                        /* 1100 0000 */
       -
       -        Bad        = Runeerror
       +    Bit1    = 7,
       +    Bitx    = 6,
       +    Bit2    = 5,
       +    Bit3    = 4,
       +    Bit4    = 3,
       +
       +    T1  = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */
       +    Tx  = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */
       +    T2  = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */
       +    T3  = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */
       +    T4  = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */
       +
       +    Rune1   = (1<<(Bit1+0*Bitx))-1,     /* 0000 0000 0111 1111 */
       +    Rune2   = (1<<(Bit2+1*Bitx))-1,     /* 0000 0111 1111 1111 */
       +    Rune3   = (1<<(Bit3+2*Bitx))-1,     /* 1111 1111 1111 1111 */
       +
       +    Maskx   = (1<<Bitx)-1,          /* 0011 1111 */
       +    Testx   = Maskx ^ 0xFF,         /* 1100 0000 */
       +
       +    Bad = Runeerror
        };
        
        int
        chartorune(Rune *rune, char *str)
        {
       -        int c, c1, c2;
       -        long l;
       -
       -        /*
       -         * one character sequence
       -         *        00000-0007F => T1
       -         */
       -        c = *(uchar*)str;
       -        if(c < Tx) {
       -                *rune = c;
       -                return 1;
       -        }
       -
       -        /*
       -         * two character sequence
       -         *        0080-07FF => T2 Tx
       -         */
       -        c1 = *(uchar*)(str+1) ^ Tx;
       -        if(c1 & Testx)
       -                goto bad;
       -        if(c < T3) {
       -                if(c < T2)
       -                        goto bad;
       -                l = ((c << Bitx) | c1) & Rune2;
       -                if(l <= Rune1)
       -                        goto bad;
       -                *rune = l;
       -                return 2;
       -        }
       -
       -        /*
       -         * three character sequence
       -         *        0800-FFFF => T3 Tx Tx
       -         */
       -        c2 = *(uchar*)(str+2) ^ Tx;
       -        if(c2 & Testx)
       -                goto bad;
       -        if(c < T4) {
       -                l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
       -                if(l <= Rune2)
       -                        goto bad;
       -                *rune = l;
       -                return 3;
       -        }
       -
       -        /*
       -         * bad decoding
       -         */
       +    int c, c1, c2;
       +    long l;
       +
       +    /*
       +     * one character sequence
       +     *  00000-0007F => T1
       +     */
       +    c = *(uchar*)str;
       +    if(c < Tx) {
       +        *rune = c;
       +        return 1;
       +    }
       +
       +    /*
       +     * two character sequence
       +     *  0080-07FF => T2 Tx
       +     */
       +    c1 = *(uchar*)(str+1) ^ Tx;
       +    if(c1 & Testx)
       +        goto bad;
       +    if(c < T3) {
       +        if(c < T2)
       +            goto bad;
       +        l = ((c << Bitx) | c1) & Rune2;
       +        if(l <= Rune1)
       +            goto bad;
       +        *rune = l;
       +        return 2;
       +    }
       +
       +    /*
       +     * three character sequence
       +     *  0800-FFFF => T3 Tx Tx
       +     */
       +    c2 = *(uchar*)(str+2) ^ Tx;
       +    if(c2 & Testx)
       +        goto bad;
       +    if(c < T4) {
       +        l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
       +        if(l <= Rune2)
       +            goto bad;
       +        *rune = l;
       +        return 3;
       +    }
       +
       +    /*
       +     * bad decoding
       +     */
        bad:
       -        *rune = Bad;
       -        return 1;
       +    *rune = Bad;
       +    return 1;
        }
        
        int
        runetochar(char *str, Rune *rune)
        {
       -        long c;
       -
       -        /*
       -         * one character sequence
       -         *        00000-0007F => 00-7F
       -         */
       -        c = *rune;
       -        if(c <= Rune1) {
       -                str[0] = c;
       -                return 1;
       -        }
       -
       -        /*
       -         * two character sequence
       -         *        0080-07FF => T2 Tx
       -         */
       -        if(c <= Rune2) {
       -                str[0] = T2 | (c >> 1*Bitx);
       -                str[1] = Tx | (c & Maskx);
       -                return 2;
       -        }
       -
       -        /*
       -         * three character sequence
       -         *        0800-FFFF => T3 Tx Tx
       -         */
       -        str[0] = T3 |  (c >> 2*Bitx);
       -        str[1] = Tx | ((c >> 1*Bitx) & Maskx);
       -        str[2] = Tx |  (c & Maskx);
       -        return 3;
       +    long c;
       +
       +    /*
       +     * one character sequence
       +     *  00000-0007F => 00-7F
       +     */
       +    c = *rune;
       +    if(c <= Rune1) {
       +        str[0] = c;
       +        return 1;
       +    }
       +
       +    /*
       +     * two character sequence
       +     *  0080-07FF => T2 Tx
       +     */
       +    if(c <= Rune2) {
       +        str[0] = T2 | (c >> 1*Bitx);
       +        str[1] = Tx | (c & Maskx);
       +        return 2;
       +    }
       +
       +    /*
       +     * three character sequence
       +     *  0800-FFFF => T3 Tx Tx
       +     */
       +    str[0] = T3 |  (c >> 2*Bitx);
       +    str[1] = Tx | ((c >> 1*Bitx) & Maskx);
       +    str[2] = Tx |  (c & Maskx);
       +    return 3;
        }
        
        int
        runelen(long c)
        {
       -        Rune rune;
       -        char str[10];
       +    Rune rune;
       +    char str[10];
        
       -        rune = c;
       -        return runetochar(str, &rune);
       +    rune = c;
       +    return runetochar(str, &rune);
        }
        
        int
        runenlen(Rune *r, int nrune)
        {
       -        int nb, c;
       -
       -        nb = 0;
       -        while(nrune--) {
       -                c = *r++;
       -                if(c <= Rune1)
       -                        nb++;
       -                else
       -                if(c <= Rune2)
       -                        nb += 2;
       -                else
       -                        nb += 3;
       -        }
       -        return nb;
       +    int nb, c;
       +
       +    nb = 0;
       +    while(nrune--) {
       +        c = *r++;
       +        if(c <= Rune1)
       +            nb++;
       +        else
       +        if(c <= Rune2)
       +            nb += 2;
       +        else
       +            nb += 3;
       +    }
       +    return nb;
        }
        
        int
        fullrune(char *str, int n)
        {
       -        int c;
       -
       -        if(n > 0) {
       -                c = *(uchar*)str;
       -                if(c < Tx)
       -                        return 1;
       -                if(n > 1)
       -                        if(c < T3 || n > 2)
       -                                return 1;
       -        }
       -        return 0;
       +    int c;
       +
       +    if(n > 0) {
       +        c = *(uchar*)str;
       +        if(c < Tx)
       +            return 1;
       +        if(n > 1)
       +            if(c < T3 || n > 2)
       +                return 1;
       +    }
       +    return 0;
        }
        
        char*
        utfrune(char *s, long c)
        {
       -        long c1;
       -        Rune r;
       -        int n;
       -
       -        if(c < Runesync)                /* not part of utf sequence */
       -                return strchr(s, c);
       -
       -        for(;;) {
       -                c1 = *(uchar*)s;
       -                if(c1 < Runeself) {        /* one byte rune */
       -                        if(c1 == 0)
       -                                return 0;
       -                        if(c1 == c)
       -                                return s;
       -                        s++;
       -                        continue;
       -                }
       -                n = chartorune(&r, s);
       -                if(r == c)
       -                        return s;
       -                s += n;
       -        }
       -        return 0;
       +    long c1;
       +    Rune r;
       +    int n;
       +
       +    if(c < Runesync)        /* not part of utf sequence */
       +        return strchr(s, c);
       +
       +    for(;;) {
       +        c1 = *(uchar*)s;
       +        if(c1 < Runeself) { /* one byte rune */
       +            if(c1 == 0)
       +                return 0;
       +            if(c1 == c)
       +                return s;
       +            s++;
       +            continue;
       +        }
       +        n = chartorune(&r, s);
       +        if(r == c)
       +            return s;
       +        s += n;
       +    }
       +    return 0;
        }
        
        int
        utflen(char *s)
        {
       -        int c;
       -        long n;
       -        Rune rune;
       -
       -        n = 0;
       -        for(;;) {
       -                c = *(uchar*)s;
       -                if(c < Runeself) {
       -                        if(c == 0)
       -                                return n;
       -                        s++;
       -                } else
       -                        s += chartorune(&rune, s);
       -                n++;
       -        }
       -        return 0;
       +    int c;
       +    long n;
       +    Rune rune;
       +
       +    n = 0;
       +    for(;;) {
       +        c = *(uchar*)s;
       +        if(c < Runeself) {
       +            if(c == 0)
       +                return n;
       +            s++;
       +        } else
       +            s += chartorune(&rune, s);
       +        n++;
       +    }
       +    return 0;
        }
 (DIR) diff --git a/libXg/segment.c b/libXg/segment.c
       @@ -7,19 +7,19 @@
        void
        segment(Bitmap *d, Point p1, Point p2, int v, Fcode f)
        {
       -        int x1, y1, x2, y2;
       -        GC g;
       +    int x1, y1, x2, y2;
       +    GC g;
        
       -        x1 = p1.x;
       -        y1 = p1.y;
       -        x2 = p2.x;
       -        y2 = p2.y;
       -        if(d->flag&SHIFT){
       -                x1 -= d->r.min.x;
       -                y1 -= d->r.min.y;
       -                x2 -= d->r.min.x;
       -                y2 -= d->r.min.y;
       -        }
       -        g = _getfillgc(f, d, v);
       -        XDrawLine(_dpy, (Drawable)d->id, g, x1, y1, x2, y2);
       +    x1 = p1.x;
       +    y1 = p1.y;
       +    x2 = p2.x;
       +    y2 = p2.y;
       +    if(d->flag&SHIFT){
       +        x1 -= d->r.min.x;
       +        y1 -= d->r.min.y;
       +        x2 -= d->r.min.x;
       +        y2 -= d->r.min.y;
       +    }
       +    g = _getfillgc(f, d, v);
       +    XDrawLine(_dpy, (Drawable)d->id, g, x1, y1, x2, y2);
        }
 (DIR) diff --git a/libXg/string.c b/libXg/string.c
       @@ -5,33 +5,33 @@
        #include <libg.h>
        #include "libgint.h"
        
       -enum        { Max = 128 };
       +enum    { Max = 128 };
        
        Point
        string(Bitmap *b, Point p, XftFont *ft, char *s, Fcode f)
        {
       -        size_t     length  = strlen(s);
       -        XGlyphInfo extents = {0};
       -        int        x       = p.x;
       -        int        y       = p.y;
       +    size_t     length  = strlen(s);
       +    XGlyphInfo extents = {0};
       +    int        x       = p.x;
       +    int        y       = p.y;
        
       -        XftTextExtentsUtf8(_dpy, ft, s, length, &extents);
       +    XftTextExtentsUtf8(_dpy, ft, s, length, &extents);
        
       -        x = p.x;
       -        y = p.y;
       -        if (b->flag & SHIFT){
       -                x -= b->r.min.x;
       -                y -= b->r.min.y;
       -        }
       -        y += ft->ascent;
       +    x = p.x;
       +    y = p.y;
       +    if (b->flag & SHIFT){
       +        x -= b->r.min.x;
       +        y -= b->r.min.y;
       +    }
       +    y += ft->ascent;
        
            if (!b->fd)
       -            b->fd = XftDrawCreate(_dpy, (Drawable)(b->id), DefaultVisual(_dpy, DefaultScreen(_dpy)), DefaultColormap(_dpy, DefaultScreen(_dpy)));
       -        XftDrawStringUtf8(b->fd, &fontcolor, ft, x, y, s, length);
       +        b->fd = XftDrawCreate(_dpy, (Drawable)(b->id), DefaultVisual(_dpy, DefaultScreen(_dpy)), DefaultColormap(_dpy, DefaultScreen(_dpy)));
       +    XftDrawStringUtf8(b->fd, &fontcolor, ft, x, y, s, length);
        
       -        x += extents.xOff;
       +    x += extents.xOff;
        
       -        p.x = (b->flag & SHIFT) ? x + b->r.min.x : x;
       -        p.x = x + b->r.min.x;
       -        return p;
       +    p.x = (b->flag & SHIFT) ? x + b->r.min.x : x;
       +    p.x = x + b->r.min.x;
       +    return p;
        }
 (DIR) diff --git a/libXg/strwidth.c b/libXg/strwidth.c
       @@ -19,5 +19,5 @@ strsize(XftFont *f, char *s)
            XGlyphInfo extents = {0};
            XftTextExtentsUtf8(_dpy, f, s, strlen(s), &extents);
        
       -        return Pt(strwidth(f, s), extents.yOff);
       +    return Pt(strwidth(f, s), extents.yOff);
        }
 (DIR) diff --git a/libXg/texture.c b/libXg/texture.c
       @@ -7,36 +7,36 @@
        void
        texture(Bitmap *d, Rectangle r, Bitmap *s, Fcode f)
        {
       -        int x, y, w, h, bfunc;
       -        GC g;
       +    int x, y, w, h, bfunc;
       +    GC g;
        
       -        x = r.min.x;
       -        y = r.min.y;
       -        if(d->flag&SHIFT){
       -                x -= d->r.min.x;
       -                y -= d->r.min.y;
       -        }
       -        g = _getcopygc(f, d, s, &bfunc);
       -        if(d->flag&SHIFT){
       -                XSetTSOrigin(_dpy, g, -d->r.min.x, -d->r.min.y);
       -        }else
       -                XSetTSOrigin(_dpy, g, 0, 0);
       -        w = Dx(r);
       -        h = Dy(r);
       -        if(bfunc == UseFillRectangle){
       -                /* source isn't involved at all */
       -                XFillRectangle(_dpy, (Drawable)d->id, g, x, y, w, h);
       -        }else if(bfunc == UseCopyArea){
       -                XSetTile(_dpy, g, (Drawable)s->id);
       -                XSetFillStyle(_dpy, g, FillTiled);
       -                XFillRectangle(_dpy, (Drawable)d->id, g, x, y, w, h);
       -                XSetFillStyle(_dpy, g, FillSolid);
       -        }else{
       -                if(s->ldepth != 0)
       -                        berror("unsupported texture");
       -                XSetStipple(_dpy, g, (Drawable)s->id);
       -                XSetFillStyle(_dpy, g, FillOpaqueStippled);
       -                XFillRectangle(_dpy, (Drawable)d->id, g, x, y, w, h);
       -                XSetFillStyle(_dpy, g, FillSolid);
       -        }
       +    x = r.min.x;
       +    y = r.min.y;
       +    if(d->flag&SHIFT){
       +        x -= d->r.min.x;
       +        y -= d->r.min.y;
       +    }
       +    g = _getcopygc(f, d, s, &bfunc);
       +    if(d->flag&SHIFT){
       +        XSetTSOrigin(_dpy, g, -d->r.min.x, -d->r.min.y);
       +    }else
       +        XSetTSOrigin(_dpy, g, 0, 0);
       +    w = Dx(r);
       +    h = Dy(r);
       +    if(bfunc == UseFillRectangle){
       +        /* source isn't involved at all */
       +        XFillRectangle(_dpy, (Drawable)d->id, g, x, y, w, h);
       +    }else if(bfunc == UseCopyArea){
       +        XSetTile(_dpy, g, (Drawable)s->id);
       +        XSetFillStyle(_dpy, g, FillTiled);
       +        XFillRectangle(_dpy, (Drawable)d->id, g, x, y, w, h);
       +        XSetFillStyle(_dpy, g, FillSolid);
       +    }else{
       +        if(s->ldepth != 0)
       +            berror("unsupported texture");
       +        XSetStipple(_dpy, g, (Drawable)s->id);
       +        XSetFillStyle(_dpy, g, FillOpaqueStippled);
       +        XFillRectangle(_dpy, (Drawable)d->id, g, x, y, w, h);
       +        XSetFillStyle(_dpy, g, FillSolid);
       +    }
        }
 (DIR) diff --git a/libXg/wrbitmap.c b/libXg/wrbitmap.c
       @@ -12,44 +12,44 @@
        void
        wrbitmap(Bitmap *b, int miny, int maxy, unsigned char *data)
        {
       -        XImage *im;
       -        int w, h, inld, outld, l, offset, px;
       -        GC g;
       -        char *tdata;
       +    XImage *im;
       +    int w, h, inld, outld, l, offset, px;
       +    GC g;
       +    char *tdata;
        
       -        w = Dx(b->r);
       -        h = maxy - miny;
       -        inld = b->ldepth;
       -        outld = (b->ldepth == 0) ? 0 : screen.ldepth;
       -        px = 1<<(3-outld);        /* pixels per byte */
       -        /* set l to number of bytes of data per scan line */
       -        if(b->r.min.x >= 0)
       -                offset = b->r.min.x % px;
       -        else
       -                offset = px - b->r.min.x % px;
       -        l = (-b->r.min.x+px-1)/px;
       -        if(b->r.max.x >= 0)
       -                l += (b->r.max.x+px-1)/px;
       -        else
       -                l -= b->r.max.x/px;
       -        l *= h;
       +    w = Dx(b->r);
       +    h = maxy - miny;
       +    inld = b->ldepth;
       +    outld = (b->ldepth == 0) ? 0 : screen.ldepth;
       +    px = 1<<(3-outld);  /* pixels per byte */
       +    /* set l to number of bytes of data per scan line */
       +    if(b->r.min.x >= 0)
       +        offset = b->r.min.x % px;
       +    else
       +        offset = px - b->r.min.x % px;
       +    l = (-b->r.min.x+px-1)/px;
       +    if(b->r.max.x >= 0)
       +        l += (b->r.max.x+px-1)/px;
       +    else
       +        l -= b->r.max.x/px;
       +    l *= h;
        
       -        tdata = (char *)malloc(l);
       -        if (tdata == (char *) 0)
       -                        berror("wrbitmap malloc");
       -        if (inld == outld)
       -                memcpy((void*)tdata, (void*)data, l);
       -        else
       -                _ldconvert((char*)data, inld, tdata, outld, w, h);
       +    tdata = (char *)malloc(l);
       +    if (tdata == (char *) 0)
       +            berror("wrbitmap malloc");
       +    if (inld == outld)
       +        memcpy((void*)tdata, (void*)data, l);
       +    else
       +        _ldconvert((char*)data, inld, tdata, outld, w, h);
        
       -        im = XCreateImage(_dpy, 0, 1 << outld, ZPixmap, 0, tdata, w, h, 8, 0);
       +    im = XCreateImage(_dpy, 0, 1 << outld, ZPixmap, 0, tdata, w, h, 8, 0);
        
       -        /* Botched interface to XCreateImage doesn't let you set these: */
       -        im->bitmap_bit_order = MSBFirst;
       -        im->byte_order = MSBFirst;
       +    /* Botched interface to XCreateImage doesn't let you set these: */
       +    im->bitmap_bit_order = MSBFirst;
       +    im->byte_order = MSBFirst;
        
       -        g = _getfillgc(S, b, ~0);
       -        XSetBackground(_dpy, g, b->flag&DP1 ? 0 : _bgpixel);
       -        XPutImage(_dpy, (Drawable)b->id, g, im, offset, 0, 0, miny - b->r.min.y, w-offset, h);
       -        XDestroyImage(im);
       +    g = _getfillgc(S, b, ~0);
       +    XSetBackground(_dpy, g, b->flag&DP1 ? 0 : _bgpixel);
       +    XPutImage(_dpy, (Drawable)b->id, g, im, offset, 0, 0, miny - b->r.min.y, w-offset, h);
       +    XDestroyImage(im);
        }
 (DIR) diff --git a/libXg/wrbitmapfile.c b/libXg/wrbitmapfile.c
       @@ -4,47 +4,47 @@
        #include <libg.h>
        #include "libgint.h"
        
       -#define        CHUNK        4096
       +#define CHUNK   4096
        
        void
        wrbitmapfile(int fd, Bitmap *b)
        {
       -        char hdr[5*12+1];
       -        unsigned char *data;
       -        long dy, px;
       -        unsigned long l, t, n;
       -        long miny, maxy;
       +    char hdr[5*12+1];
       +    unsigned char *data;
       +    long dy, px;
       +    unsigned long l, t, n;
       +    long miny, maxy;
        
       -        sprint(hdr, "%11d %11d %11d %11d %11d ",
       -                b->ldepth, b->r.min.x, b->r.min.y, b->r.max.x, b->r.max.y);
       -        if(write(fd, hdr, 5*12) != 5*12)
       -                berror("wrbitmapfile write");
       +    sprint(hdr, "%11d %11d %11d %11d %11d ",
       +        b->ldepth, b->r.min.x, b->r.min.y, b->r.max.x, b->r.max.y);
       +    if(write(fd, hdr, 5*12) != 5*12)
       +        berror("wrbitmapfile write");
        
       -        px = 1<<(3-b->ldepth);        /* pixels per byte */
       -        /* set l to number of bytes of data per scan line */
       -        if(b->r.min.x >= 0)
       -                l = (b->r.max.x+px-1)/px - b->r.min.x/px;
       -        else{        /* make positive before divide */
       -                t = (-b->r.min.x)+px-1;
       -                t = (t/px)*px;
       -                l = (t+b->r.max.x+px-1)/px;
       -        }
       -        miny = b->r.min.y;
       -        maxy = b->r.max.y;
       -        data = (unsigned char *)malloc(CHUNK);
       -        if(data == 0)
       -                berror("wrbitmapfile malloc");
       -        while(maxy > miny){
       -                dy = maxy - miny;
       -                if(dy*l > CHUNK)
       -                        dy = CHUNK/l;
       -                rdbitmap(b, miny, miny+dy, data);
       -                n = dy*l;
       -                if(write(fd, data, n) != n){
       -                        free(data);
       -                        berror("wrbitmapfile write");
       -                }
       -                miny += dy;
       -        }
       -        free(data);
       +    px = 1<<(3-b->ldepth);  /* pixels per byte */
       +    /* set l to number of bytes of data per scan line */
       +    if(b->r.min.x >= 0)
       +        l = (b->r.max.x+px-1)/px - b->r.min.x/px;
       +    else{   /* make positive before divide */
       +        t = (-b->r.min.x)+px-1;
       +        t = (t/px)*px;
       +        l = (t+b->r.max.x+px-1)/px;
       +    }
       +    miny = b->r.min.y;
       +    maxy = b->r.max.y;
       +    data = (unsigned char *)malloc(CHUNK);
       +    if(data == 0)
       +        berror("wrbitmapfile malloc");
       +    while(maxy > miny){
       +        dy = maxy - miny;
       +        if(dy*l > CHUNK)
       +            dy = CHUNK/l;
       +        rdbitmap(b, miny, miny+dy, data);
       +        n = dy*l;
       +        if(write(fd, data, n) != n){
       +            free(data);
       +            berror("wrbitmapfile write");
       +        }
       +        miny += dy;
       +    }
       +    free(data);
        }
 (DIR) diff --git a/libXg/xtbinit.c b/libXg/xtbinit.c
       @@ -49,23 +49,23 @@
        #endif
        
        /* libg globals */
       -Bitmap        screen;
       -XftFont        *font;
       +Bitmap  screen;
       +XftFont *font;
        XftColor fontcolor;
        
        /* implementation globals */
        extern char *machine;
       -Display                *_dpy;
       -Widget                _toplevel;
       +Display     *_dpy;
       +Widget      _toplevel;
        Window _topwindow;
        unsigned long _bgpixels[MAX_BACKGROUNDS];
        int _nbgs;
       -unsigned long        _fgpixel, _bgpixel, _borderpixel;
       -XColor                _fgcolor, _bgcolor, _bordercolor;
       -int                _ld2d[6] = { 1, 2, 4, 8, 16, 24 };
       -unsigned long        _ld2dmask[6] = { 0x1, 0x3, 0xF, 0xFF, 0xFFFF, 0x00FFFFFF };
       -Colormap        _libg_cmap;
       -int                _cmap_installed;
       +unsigned long   _fgpixel, _bgpixel, _borderpixel;
       +XColor      _fgcolor, _bgcolor, _bordercolor;
       +int     _ld2d[6] = { 1, 2, 4, 8, 16, 24 };
       +unsigned long   _ld2dmask[6] = { 0x1, 0x3, 0xF, 0xFF, 0xFFFF, 0x00FFFFFF };
       +Colormap    _libg_cmap;
       +int     _cmap_installed;
        
        /* xbinit implementation globals */
        #ifndef R3
       @@ -77,24 +77,24 @@ static Atom wm_take_focus;
        static Mouse lastmouse;
        
        typedef struct Ebuf {
       -    struct Ebuf        *next;
       -    int                n;
       -    unsigned char        buf[4];
       +    struct Ebuf *next;
       +    int     n;
       +    unsigned char   buf[4];
        } Ebuf;
        
        typedef struct Esrc {
       -    int        inuse;
       -    int        size;
       -    int        count;
       -    Ebuf        *head;
       -    Ebuf        *tail;
       +    int inuse;
       +    int size;
       +    int count;
       +    Ebuf    *head;
       +    Ebuf    *tail;
        } Esrc;
        
       -#define        MAXINPUT        1024                /* number of queued input events */
       -#define MAXSRC                 10
       +#define MAXINPUT    1024        /* number of queued input events */
       +#define MAXSRC      10
        
       -static Esrc        esrc[MAXSRC];
       -static int        nsrc;
       +static Esrc esrc[MAXSRC];
       +static int  nsrc;
        
        
        static int einitcalled = 0;
       @@ -103,19 +103,19 @@ static int Skeyboard = -1;
        static int Stimer = -1;
        
        
       -static void        reshaped(int, int, int, int);
       -static void        gotchar(int, int, int, int, int);
       -static void        gotmouse(Gwinmouse *);
       -static int        ilog2(int);
       -static void        pixtocolor(Pixel, XColor *);
       -static Ebuf        *ebread(Esrc *);
       -static Ebuf        *ebadd(Esrc *, int);
       -static void        focinit(Widget);
       -static void        wmproto(Widget, XEvent *, String *, Cardinal *);
       -static void        waitevent(void);
       +static void reshaped(int, int, int, int);
       +static void gotchar(int, int, int, int, int);
       +static void gotmouse(Gwinmouse *);
       +static int  ilog2(int);
       +static void pixtocolor(Pixel, XColor *);
       +static Ebuf *ebread(Esrc *);
       +static Ebuf *ebadd(Esrc *, int);
       +static void focinit(Widget);
       +static void wmproto(Widget, XEvent *, String *, Cardinal *);
       +static void waitevent(void);
        void initlatin();
        
       -static Errfunc        onerr;
       +static Errfunc  onerr;
        
        String _fallbacks[] = {
            "*gwin.width: 400",
       @@ -149,19 +149,19 @@ xtbinit(Errfunc f, char *class, int *pargc, char **argv, char **fallbacks)
            initlatin();
        
            if(!class && argv[0]){
       -            p = strrchr(argv[0], '/');
       -            if(p)
       -                    class = XtNewString(p+1);
       -            else
       -                    class = XtNewString(argv[0]);
       -            if(class[0] >= 'a' && class[0] <= 'z')
       -                    class[0] += 'A' - 'a';
       +        p = strrchr(argv[0], '/');
       +        if(p)
       +            class = XtNewString(p+1);
       +        else
       +            class = XtNewString(argv[0]);
       +        if(class[0] >= 'a' && class[0] <= 'z')
       +            class[0] += 'A' - 'a';
            }
            onerr = f;
            if (!fallbacks)
       -            fallbacks = _fallbacks;
       +        fallbacks = _fallbacks;
            n = 0;
       -    XtSetArg(args[n], XtNinput, TRUE);                n++;
       +    XtSetArg(args[n], XtNinput, TRUE);      n++;
        
            char name[512] = {0};
            snprintf(name, sizeof(name) - 1, "samterm on %s", machine);
       @@ -169,14 +169,14 @@ xtbinit(Errfunc f, char *class, int *pargc, char **argv, char **fallbacks)
            XtSetArg(args[n], XtNiconName, XtNewString(name)); n++;
        
            _toplevel = XtAppInitialize(&app, class,
       -                    optable, sizeof(optable)/sizeof(optable[0]),
       -                    pargc, argv, fallbacks, args, n);
       +            optable, sizeof(optable)/sizeof(optable[0]),
       +            pargc, argv, fallbacks, args, n);
        
        
            n = 0;
       -    XtSetArg(args[n], XtNreshaped, reshaped);        n++;
       -    XtSetArg(args[n], XtNgotchar, gotchar);                n++;
       -    XtSetArg(args[n], XtNgotmouse, gotmouse);        n++;
       +    XtSetArg(args[n], XtNreshaped, reshaped);   n++;
       +    XtSetArg(args[n], XtNgotchar, gotchar);     n++;
       +    XtSetArg(args[n], XtNgotmouse, gotmouse);   n++;
            widg = XtCreateManagedWidget("gwin", gwinWidgetClass, _toplevel, args, n);
        
            _dpy = XtDisplay(widg);
       @@ -199,14 +199,14 @@ xtbinit(Errfunc f, char *class, int *pargc, char **argv, char **fallbacks)
            _bgpixel = _bgpixels[0];
        
            n = 0;
       -    XtSetArg(args[n], XtNdepth, &depth);                n++; 
       -    XtSetArg(args[n], XtNcomposeMod, &compose);        n++;
       +    XtSetArg(args[n], XtNdepth, &depth);        n++; 
       +    XtSetArg(args[n], XtNcomposeMod, &compose); n++;
            XtGetValues(widg, args, n);
        
            if (compose < 0 || compose > 5) {
       -            n = 0;
       -            XtSetArg(args[n], XtNcomposeMod, 0);        n++;
       -            XtSetValues(widg, args, n);
       +        n = 0;
       +        XtSetArg(args[n], XtNcomposeMod, 0);    n++;
       +        XtSetValues(widg, args, n);
            }
        
            initcursors();
       @@ -233,13 +233,13 @@ xtbinit(Errfunc f, char *class, int *pargc, char **argv, char **fallbacks)
            screen.ldepth = ilog2(depth);
            screen.flag = SCR;
            if(_fgpixel != 0)
       -            screen.flag |= BL1;
       +        screen.flag |= BL1;
            if(depth == 1)
       -            screen.flag |= DP1;
       +        screen.flag |= DP1;
            /* leave screen rect at all zeros until reshaped() sets it */
            while(!exposed) {
       -            XFlush(_dpy);
       -            XtAppProcessEvent(app, XtIMXEvent);
       +        XFlush(_dpy);
       +        XtAppProcessEvent(app, XtIMXEvent);
            }
            XFlush(_dpy);
            focinit(_toplevel);
       @@ -270,8 +270,8 @@ wmproto(Widget w, XEvent *e , String *p, Cardinal *np)
        
            if(e->type == ClientMessage &&
                  (Atom)(e->xclient.data.l[0]) == wm_take_focus) {
       -            t = (Time) e->xclient.data.l[1];
       -            XtCallAcceptFocus(widg, &t);
       +        t = (Time) e->xclient.data.l[1];
       +        XtCallAcceptFocus(widg, &t);
            }
        }
        #endif
       @@ -285,19 +285,19 @@ reshaped(int minx, int miny, int maxx, int maxy)
            screen.r = Rect(minx, miny, maxx, maxy);
            screen.clipr = screen.r;
            if (screen.id) {
       -            exposed = 1;
       -            ereshaped(screen.r);
       +        exposed = 1;
       +        ereshaped(screen.r);
            }
            if(einitcalled){
       -            /*
       -             * Cause a mouse event, so programs like sam
       -             * will get out of eread and REALLY do the reshape
       -             */
       -            eb = ebadd(&esrc[Smouse], 0);
       -            if (eb == 0)
       -                    berror("eballoc can't malloc");
       -            memcpy((void*)eb->buf, (void*)&lastmouse, sizeof lastmouse);
       -            esrc[Smouse].count++;
       +        /*
       +         * Cause a mouse event, so programs like sam
       +         * will get out of eread and REALLY do the reshape
       +         */
       +        eb = ebadd(&esrc[Smouse], 0);
       +        if (eb == 0)
       +            berror("eballoc can't malloc");
       +        memcpy((void*)eb->buf, (void*)&lastmouse, sizeof lastmouse);
       +        esrc[Smouse].count++;
            }
        }
        
       @@ -308,10 +308,10 @@ gotchar(int c, int kind, int target, int x, int y)
            Keystroke k;
        
            if(!einitcalled || Skeyboard == -1)
       -            return;
       +        return;
            eb = ebadd(&esrc[Skeyboard], 0);
            if (eb == NULL)
       -            berror("eballoc can't malloc");
       +        berror("eballoc can't malloc");
            k.c = c;
            k.k = kind;
            k.t = target;
       @@ -327,7 +327,7 @@ gotmouse(Gwinmouse *gm)
            Mouse m;
        
            if(!einitcalled || Smouse == -1)
       -            return;
       +        return;
            m.buttons = gm->buttons;
            m.xy.x = gm->xy.x;
            m.xy.y = gm->xy.y;
       @@ -335,7 +335,7 @@ gotmouse(Gwinmouse *gm)
            lastmouse = m;
            eb = ebadd(&esrc[Smouse], 0);
            if (eb == 0)
       -            berror("eballoc can't malloc");
       +        berror("eballoc can't malloc");
            memcpy((void*)eb->buf, (void*)&m, sizeof m);
            esrc[Smouse].count++;
        }
       @@ -348,27 +348,27 @@ gotinput(XtPointer cldata, int *pfd, XtInputId *id)
            int n;
        
            if(!einitcalled)
       -            return;
       +        return;
            es = (Esrc *)cldata;
            if (es->count >= MAXINPUT)
       -            return;
       +        return;
            lasttail = es->tail;
            eb = ebadd(es, 0);
            if (eb == 0)
       -            return;
       +        return;
            if(es->size){
       -            n = read(*pfd, (char *)eb->buf, es->size);
       -            if (n < 0)
       -                    n = 0;
       -            if(n < es->size) {
       -                    newe = realloc(eb, sizeof(Ebuf)+n);
       -                    newe->n = n;
       -                    if (es->head == eb)
       -                            es->head = newe;
       -                    else
       -                            lasttail->next = newe;
       -                    es->tail = newe;
       -            }
       +        n = read(*pfd, (char *)eb->buf, es->size);
       +        if (n < 0)
       +            n = 0;
       +        if(n < es->size) {
       +            newe = realloc(eb, sizeof(Ebuf)+n);
       +            newe->n = n;
       +            if (es->head == eb)
       +                es->head = newe;
       +            else
       +                lasttail->next = newe;
       +            es->tail = newe;
       +        }
            }
            es->count++;
        }
       @@ -377,7 +377,7 @@ static void
        gottimeout(XtPointer cldata, XtIntervalId *id)
        {
            if(!einitcalled || Stimer == -1)
       -            return;
       +        return;
            /*
             * Don't queue up timeouts, because there's
             * too big a danger that they might pile up
       @@ -394,8 +394,8 @@ ilog2(int n)
            int i, v;
        
            for(i=0, v=1; i < 6; i++, v<<=1)
       -            if(n <= v)
       -                    break;
       +        if(n <= v)
       +            break;
            return i;
        }
        
       @@ -408,7 +408,7 @@ pixtocolor(Pixel p, XColor *pc)
            int n;
        
            n = 0;
       -    XtSetArg(args[n], XtNcolormap, &cmap);        n++;
       +    XtSetArg(args[n], XtNcolormap, &cmap);  n++;
            XtGetValues(_toplevel, args, n);
            pc->pixel = p;
            XQueryColor(_dpy, cmap, pc);
       @@ -420,7 +420,7 @@ pixtocolor(Pixel p, XColor *pc)
            xvt.size = sizeof(XColor);
            xvt.addr = (XtPointer)pc;
            if(!XtConvertAndStore(_toplevel, XtRPixel, &xvf, XtRColor, &xvt))
       -            pc->pixel = p;        /* maybe that's enough */
       +        pc->pixel = p;  /* maybe that's enough */
        #endif
        }
        
       @@ -435,30 +435,30 @@ rgbpix(Bitmap *b, RGB col)
            unsigned long d, max, pixel;
        
            if (!_cmap_installed) {
       -            n = 0;
       -            XtSetArg(args[n], XtNcolormap, &cmap);        n++;
       -            XtGetValues(_toplevel, args, n);
       -            c.red = col.red>>16;
       -            c.green = col.green>>16;
       -            c.blue = col.blue>>16;
       -            c.flags = DoRed|DoGreen|DoBlue;
       -            if(XAllocColor(_dpy, cmap, &c))
       -                    return (unsigned long)(c.pixel);
       +        n = 0;
       +        XtSetArg(args[n], XtNcolormap, &cmap);  n++;
       +        XtGetValues(_toplevel, args, n);
       +        c.red = col.red>>16;
       +        c.green = col.green>>16;
       +        c.blue = col.blue>>16;
       +        c.flags = DoRed|DoGreen|DoBlue;
       +        if(XAllocColor(_dpy, cmap, &c))
       +            return (unsigned long)(c.pixel);
            }
            depth = _ld2d[screen.ldepth];
            rdcolmap(&screen, map);
            max = -1;
            for (n = 0, m = map; n < (1 << depth); n++, m++)
            {
       -            dr = m->red - col.red;
       -            dg = m->green - col.green;
       -            db = m->blue - col.blue;
       -            d = dr*dr+dg*dg+db*db;
       -            if (d < max || max == -1)
       -            {
       -                    max = d;
       -                    pixel = n;
       -            }
       +        dr = m->red - col.red;
       +        dg = m->green - col.green;
       +        db = m->blue - col.blue;
       +        d = dr*dr+dg*dg+db*db;
       +        if (d < max || max == -1)
       +        {
       +            max = d;
       +            pixel = n;
       +        }
            }
            return pixel;
        }
       @@ -472,32 +472,32 @@ rdcolmap(Bitmap *b, RGB *map)
            Arg args[2];
        
            if (_cmap_installed) {
       -            cmap = _libg_cmap;
       +        cmap = _libg_cmap;
            } else {
       -            i = 0;
       -            XtSetArg(args[i], XtNcolormap, &cmap);        i++;
       -            XtGetValues(_toplevel, args, i);
       +        i = 0;
       +        XtSetArg(args[i], XtNcolormap, &cmap);  i++;
       +        XtGetValues(_toplevel, args, i);
            }
        
            depth = _ld2d[screen.ldepth];
            n = 1 << depth;
            if (depth == 1) {
       -            map[0].red = map[0].green = map[0].blue = ~0;
       -            map[1].red = map[1].green = map[1].blue = 0;
       +        map[0].red = map[0].green = map[0].blue = ~0;
       +        map[1].red = map[1].green = map[1].blue = 0;
            }
            else {
       -            if (n > 256) {
       -                    berror("rdcolmap bitmap too deep");
       -                    return;
       -            }
       -            for (i = 0; i < n; i++)
       -                    cols[i].pixel = i;
       -            XQueryColors(_dpy, cmap, cols, n);
       -            for (i = 0; i < n; i++) {
       -                    map[i].red = (cols[i].red << 16) | cols[i].red;
       -                    map[i].green = (cols[i].green << 16) | cols[i].green;
       -                    map[i].blue = (cols[i].blue << 16) | cols[i].blue;
       -            }
       +        if (n > 256) {
       +            berror("rdcolmap bitmap too deep");
       +            return;
       +        }
       +        for (i = 0; i < n; i++)
       +            cols[i].pixel = i;
       +        XQueryColors(_dpy, cmap, cols, n);
       +        for (i = 0; i < n; i++) {
       +            map[i].red = (cols[i].red << 16) | cols[i].red;
       +            map[i].green = (cols[i].green << 16) | cols[i].green;
       +            map[i].blue = (cols[i].blue << 16) | cols[i].blue;
       +        }
            }
        }
        
       @@ -515,29 +515,29 @@ wrcolmap(Bitmap *b, RGB *map)
            depth = _ld2d[screen.ldepth];
            n = 1 << depth;
            if (n > 256) {
       -            berror("wrcolmap bitmap too deep");
       -            return;
       +        berror("wrcolmap bitmap too deep");
       +        return;
            } else if (depth > 1) {
       -            for (i = 0; i < n; i++) {
       -                    cols[i].red = map[i].red >> 16;
       -                    cols[i].green = map[i].green >> 16;
       -                    cols[i].blue = map[i].blue >> 16;
       -                    cols[i].pixel = i;
       -                    cols[i].flags = DoRed|DoGreen|DoBlue;
       -            }
       -            if (!XMatchVisualInfo(_dpy, XScreenNumberOfScreen(scr),
       -                                    depth, PseudoColor, &vi)) {
       -                    berror("wrcolmap can't get visual");
       -                    return;
       -            }
       -            w = XtWindow(_toplevel);
       -            _libg_cmap = XCreateColormap(_dpy, w, vi.visual, AllocAll);
       -            XStoreColors(_dpy, _libg_cmap, cols, n);
       -
       -            i = 0;
       -            XtSetArg(args[i], XtNcolormap, _libg_cmap);        i++;
       -            XtSetValues(_toplevel, args, i);
       -            _cmap_installed = 1;
       +        for (i = 0; i < n; i++) {
       +            cols[i].red = map[i].red >> 16;
       +            cols[i].green = map[i].green >> 16;
       +            cols[i].blue = map[i].blue >> 16;
       +            cols[i].pixel = i;
       +            cols[i].flags = DoRed|DoGreen|DoBlue;
       +        }
       +        if (!XMatchVisualInfo(_dpy, XScreenNumberOfScreen(scr),
       +                    depth, PseudoColor, &vi)) {
       +            berror("wrcolmap can't get visual");
       +            return;
       +        }
       +        w = XtWindow(_toplevel);
       +        _libg_cmap = XCreateColormap(_dpy, w, vi.visual, AllocAll);
       +        XStoreColors(_dpy, _libg_cmap, cols, n);
       +
       +        i = 0;
       +        XtSetArg(args[i], XtNcolormap, _libg_cmap); i++;
       +        XtSetValues(_toplevel, args, i);
       +        _cmap_installed = 1;
            }
        }
        
       @@ -561,19 +561,19 @@ einit(unsigned long keys)
             */
            nsrc = 0;
            if(keys&Emouse){
       -            Smouse = 0;
       -            esrc[Smouse].inuse = 1;
       -            esrc[Smouse].size = sizeof(Mouse);
       -            esrc[Smouse].count = 0;
       -            nsrc = Smouse+1;
       +        Smouse = 0;
       +        esrc[Smouse].inuse = 1;
       +        esrc[Smouse].size = sizeof(Mouse);
       +        esrc[Smouse].count = 0;
       +        nsrc = Smouse+1;
            }
            if(keys&Ekeyboard){
       -            Skeyboard = 1;
       -            esrc[Skeyboard].inuse = 1;
       -            esrc[Skeyboard].size = sizeof(Keystroke);
       -            esrc[Skeyboard].count = 0;
       -            if(Skeyboard >= nsrc)
       -                    nsrc = Skeyboard+1;
       +        Skeyboard = 1;
       +        esrc[Skeyboard].inuse = 1;
       +        esrc[Skeyboard].size = sizeof(Keystroke);
       +        esrc[Skeyboard].count = 0;
       +        if(Skeyboard >= nsrc)
       +            nsrc = Skeyboard+1;
            }
            einitcalled = 1;
        }
       @@ -584,20 +584,20 @@ estart(unsigned long key, int fd, int n)
            int i;
        
            if(fd < 0)
       -            berror("bad fd to estart");
       +        berror("bad fd to estart");
            if(n <= 0 || n > EMAXMSG)
       -            n = EMAXMSG;
       +        n = EMAXMSG;
            for(i=0; i<MAXSRC; i++)
       -            if((key & ~(1<<i)) == 0 && !esrc[i].inuse){
       -                    if(nsrc <= i)
       -                            nsrc = i+1;
       -                    esrc[i].inuse = 1;
       -                    esrc[i].size = n;
       -                    esrc[i].count = 0;
       -                    XtAppAddInput(app, fd, (XtPointer)XtInputReadMask,
       -                            gotinput, (XtPointer) &esrc[i]);
       -                    return 1<<i;
       -            }
       +        if((key & ~(1<<i)) == 0 && !esrc[i].inuse){
       +            if(nsrc <= i)
       +                nsrc = i+1;
       +            esrc[i].inuse = 1;
       +            esrc[i].size = n;
       +            esrc[i].count = 0;
       +            XtAppAddInput(app, fd, (XtPointer)XtInputReadMask,
       +                gotinput, (XtPointer) &esrc[i]);
       +            return 1<<i;
       +        }
            return 0;
        }
        
       @@ -607,20 +607,20 @@ etimer(unsigned long key, long n)
            int i;
        
            if(Stimer != -1)
       -            berror("timer started twice");
       +        berror("timer started twice");
            if(n <= 0)
       -            n = 1000;
       +        n = 1000;
            for(i=0; i<MAXSRC; i++)
       -            if((key & ~(1<<i)) == 0 && !esrc[i].inuse){
       -                    if(nsrc <= i)
       -                            nsrc = i+1;
       -                    esrc[i].inuse = 1;
       -                    esrc[i].size = 0;
       -                    esrc[i].count = 0;
       -                    XtAppAddTimeOut(app, n, gottimeout, (XtPointer)n);
       -                    Stimer = i;
       -                    return 1<<i;
       -            }
       +        if((key & ~(1<<i)) == 0 && !esrc[i].inuse){
       +            if(nsrc <= i)
       +                nsrc = i+1;
       +            esrc[i].inuse = 1;
       +            esrc[i].size = 0;
       +            esrc[i].count = 0;
       +            XtAppAddTimeOut(app, n, gottimeout, (XtPointer)n);
       +            Stimer = i;
       +            return 1<<i;
       +        }
            return 0;
        }
        
       @@ -637,31 +637,31 @@ eread(unsigned long keys, Event *e)
            int i;
        
            if(keys == 0)
       -            return 0;
       -            /* Give Priority to X events */
       +        return 0;
       +        /* Give Priority to X events */
            if (XtAppPending(app) & XtIMXEvent)
       -            XtAppProcessEvent(app, XtIMXEvent);
       +        XtAppProcessEvent(app, XtIMXEvent);
        
            for(;;){
       -            for(i=0; i<nsrc; i++)
       -                    if((keys & (1<<i)) && esrc[i].head){
       -                            if(i == Smouse)
       -                                    e->mouse = emouse();
       -                            else if(i == Skeyboard)
       -                                    e->keystroke = ekbd();
       -                            else if(i == Stimer) {
       -                                    esrc[i].head = 0;
       -                                    esrc[i].count = 0;
       -                            } else {
       -                                    eb = ebread(&esrc[i]);
       -                                    e->n = eb->n;
       -                                    if(e->n > 0)
       -                                            memcpy((void*)e->data, (void*)eb->buf, e->n);
       -                                    free(eb);
       -                            }
       -                            return 1<<i;
       -                    }
       -            waitevent();
       +        for(i=0; i<nsrc; i++)
       +            if((keys & (1<<i)) && esrc[i].head){
       +                if(i == Smouse)
       +                    e->mouse = emouse();
       +                else if(i == Skeyboard)
       +                    e->keystroke = ekbd();
       +                else if(i == Stimer) {
       +                    esrc[i].head = 0;
       +                    esrc[i].count = 0;
       +                } else {
       +                    eb = ebread(&esrc[i]);
       +                    e->n = eb->n;
       +                    if(e->n > 0)
       +                        memcpy((void*)e->data, (void*)eb->buf, e->n);
       +                    free(eb);
       +                }
       +                return 1<<i;
       +            }
       +        waitevent();
            }
        }
        
       @@ -672,18 +672,18 @@ eflush(unsigned long keys)
            Ebuf *eb, *enext;
        
            if(keys == 0)
       -            return;
       +        return;
        
            for(i=0; i<nsrc; i++)
       -            if((keys & (1<<i))){
       -                    for (eb = esrc[i].head; eb; eb = enext) {
       -                            enext = eb->next;
       -                            free(eb);
       -                    }
       -                    esrc[i].count = 0;
       -                    esrc[i].head = 0;
       -                    esrc[i].tail = 0;
       -            }
       +        if((keys & (1<<i))){
       +            for (eb = esrc[i].head; eb; eb = enext) {
       +                enext = eb->next;
       +                free(eb);
       +            }
       +            esrc[i].count = 0;
       +            esrc[i].head = 0;
       +            esrc[i].tail = 0;
       +        }
        }
        
        Mouse
       @@ -693,7 +693,7 @@ emouse(void)
            Ebuf *eb;
        
            if(!esrc[Smouse].inuse)
       -            berror("mouse events not selected");
       +        berror("mouse events not selected");
            eb = ebread(&esrc[Smouse]);
            memcpy((void*)&m, (void*)eb->buf, sizeof(Mouse));
            free(eb);
       @@ -708,7 +708,7 @@ ekbd(void)
            Keystroke k;
        
            if(!esrc[Skeyboard].inuse)
       -            berror("keyboard events not selected");
       +        berror("keyboard events not selected");
            eb = ebread(&esrc[Skeyboard]);
            memcpy(&k, eb->buf, sizeof(Keystroke));
            free(eb);
       @@ -722,10 +722,10 @@ pushkbd(int c)
            Keystroke k;
        
            if(!einitcalled || Skeyboard == -1)
       -            return;
       +        return;
            eb = ebadd(&esrc[Skeyboard], 1);
            if (eb == 0)
       -            berror("eballoc can't malloc");
       +        berror("eballoc can't malloc");
            k.c = c;
            k.k = Kcomposed;
            memcpy(eb->buf, &k, sizeof(Keystroke));
       @@ -738,14 +738,14 @@ ecanread(unsigned long keys)
            int i;
        
            for(;;){
       -            for(i=0; i<nsrc; i++){
       -                    if((keys & (1<<i)) && esrc[i].head)
       -                            return 1<<i;
       -            }
       -            if(XtAppPending(app))
       -                    waitevent();
       -            else
       -                    return 0;
       +        for(i=0; i<nsrc; i++){
       +            if((keys & (1<<i)) && esrc[i].head)
       +                return 1<<i;
       +        }
       +        if(XtAppPending(app))
       +            waitevent();
       +        else
       +            return 0;
            }
        }
        
       @@ -753,7 +753,7 @@ int
        ecanmouse(void)
        {
            if(Smouse == -1)
       -            berror("mouse events not selected");
       +        berror("mouse events not selected");
            return ecanread(Emouse);
        }
        
       @@ -761,7 +761,7 @@ int
        ecankbd(void)
        {
            if(Skeyboard == -1)
       -            berror("keyboard events not selected");
       +        berror("keyboard events not selected");
            return ecanread(Ekeyboard);
        }
        
       @@ -771,24 +771,24 @@ ebread(Esrc *s)
            Ebuf *eb;
        
            while(s->head == 0)
       -            waitevent();
       +        waitevent();
            eb = s->head;
        #ifdef COMPRESSMOUSE
            if(s == &esrc[Smouse]) {
       -            while(eb->next) {
       -                    s->head = eb->next;
       -                    s->count--;
       -                    free(eb);
       -                    eb = s->head;
       -            }
       +        while(eb->next) {
       +            s->head = eb->next;
       +            s->count--;
       +            free(eb);
       +            eb = s->head;
       +        }
            }
        #endif
            s->head = s->head->next;
            if(s->head == 0) {
       -            s->tail = 0;
       -            s->count = 0;
       +        s->tail = 0;
       +        s->count = 0;
            } else
       -            s->count--;
       +        s->count--;
            return eb;
        }
        
       @@ -817,11 +817,11 @@ ebadd(Esrc *s, int prepend)
        
            m = sizeof(Ebuf);
            if(s->size > 1)
       -            m += (s->size-1);        /* overestimate, because of alignment */
       +        m += (s->size-1);   /* overestimate, because of alignment */
            eb = (Ebuf *)malloc(m);
            if(eb) {
                eb->next = 0;
       -            eb->n = s->size;
       +        eb->n = s->size;
                if (prepend)
                    ebprepend(eb, s);
                else
       @@ -834,10 +834,10 @@ void
        berror(char *s)
        {
            if(onerr)
       -            (*onerr)(s);
       +        (*onerr)(s);
            else{
       -            fprintf(stderr, "libg error: %s:\n", s);
       -            exit(1);
       +        fprintf(stderr, "libg error: %s:\n", s);
       +        exit(1);
            }
        }
        
       @@ -845,7 +845,7 @@ void
        bflush(void)
        {
            while(XtAppPending(app) & XtIMXEvent)
       -            waitevent();
       +        waitevent();
        }
        
        static void
       @@ -853,26 +853,26 @@ waitevent(void)
        {
            XFlush(_dpy);
            if (XtAppPending(app) & XtIMXEvent)
       -            XtAppProcessEvent(app, XtIMXEvent);
       +        XtAppProcessEvent(app, XtIMXEvent);
            else
       -            XtAppProcessEvent(app, XtIMAll);
       +        XtAppProcessEvent(app, XtIMAll);
        }
       -            
       +        
        int
        snarfswap(char *s, int n, char **t)
        {
            *t = GwinSelectionSwap(widg, s);
            if (*t)
       -            return strlen(*t);
       +        return strlen(*t);
            return 0;
        }
        
        int scrpix(int *w, int *h)
        {
            if (w)
       -            *w = WidthOfScreen(XtScreen(_toplevel));
       +        *w = WidthOfScreen(XtScreen(_toplevel));
            if (h)
       -            *h = HeightOfScreen(XtScreen(_toplevel));
       +        *h = HeightOfScreen(XtScreen(_toplevel));
            return 1;
        }
        
       @@ -883,11 +883,11 @@ printgc(char *msg, GC g)
            XGCValues v;
        
            XGetGCValues(_dpy, g, GCFunction|GCForeground|GCBackground|GCFont|
       -                    GCTile|GCFillStyle|GCStipple, &v);
       +            GCTile|GCFillStyle|GCStipple, &v);
            fprintf(stderr, "%s: gc %x\n", msg, g);
            fprintf(stderr, "  fg %d bg %d func %d fillstyle %d font %x tile %x stipple %x\n",
       -            v.foreground, v.background, v.function, v.fill_style,
       -            v.font, v.tile, v.stipple);
       +        v.foreground, v.background, v.function, v.fill_style,
       +        v.font, v.tile, v.stipple);
        }
        #endif
        
 (DIR) diff --git a/libframe/frbox.c b/libframe/frbox.c
       @@ -4,153 +4,153 @@
        #include <libg.h>
        #include <frame.h>
        
       -#define        SLOP        25
       +#define SLOP    25
        
        void
       -_fraddbox(Frame *f, int bn, int n)        /* add n boxes after bn, shift the rest up,
       -                                 * box[bn+n]==box[bn] */
       +_fraddbox(Frame *f, int bn, int n)  /* add n boxes after bn, shift the rest up,
       +                 * box[bn+n]==box[bn] */
        {
       -        int i;
       -
       -        if(bn > f->nbox)
       -                berror("_fraddbox");
       -        if(f->nbox+n > f->nalloc)
       -                _frgrowbox(f, n+SLOP);
       -        for(i=f->nbox; --i>=bn; )
       -                f->box[i+n] = f->box[i];
       -        f->nbox+=n;
       +    int i;
       +
       +    if(bn > f->nbox)
       +        berror("_fraddbox");
       +    if(f->nbox+n > f->nalloc)
       +        _frgrowbox(f, n+SLOP);
       +    for(i=f->nbox; --i>=bn; )
       +        f->box[i+n] = f->box[i];
       +    f->nbox+=n;
        }
        
        void
       -_frclosebox(Frame *f, int n0, int n1)        /* inclusive */
       +_frclosebox(Frame *f, int n0, int n1)   /* inclusive */
        {
       -        int i;
       -
       -        if(n0>=f->nbox || n1>=f->nbox || n1<n0)
       -                berror("_frclosebox");
       -        n1++;
       -        for(i=n1; i<f->nbox; i++)
       -                f->box[i-(n1-n0)] = f->box[i];
       -        f->nbox -= n1-n0;
       +    int i;
       +
       +    if(n0>=f->nbox || n1>=f->nbox || n1<n0)
       +        berror("_frclosebox");
       +    n1++;
       +    for(i=n1; i<f->nbox; i++)
       +        f->box[i-(n1-n0)] = f->box[i];
       +    f->nbox -= n1-n0;
        }
        
        void
       -_frdelbox(Frame *f, int n0, int n1)        /* inclusive */
       +_frdelbox(Frame *f, int n0, int n1) /* inclusive */
        {
       -        if(n0>=f->nbox || n1>=f->nbox || n1<n0)
       -                berror("_frdelbox");
       -        _frfreebox(f, n0, n1);
       -        _frclosebox(f, n0, n1);
       +    if(n0>=f->nbox || n1>=f->nbox || n1<n0)
       +        berror("_frdelbox");
       +    _frfreebox(f, n0, n1);
       +    _frclosebox(f, n0, n1);
        }
        
        void
       -_frfreebox(Frame *f, int n0, int n1)        /* inclusive */
       +_frfreebox(Frame *f, int n0, int n1)    /* inclusive */
        {
       -        int i;
       -
       -        if(n1<n0)
       -                return;
       -        if(n0>=f->nbox || n1>=f->nbox)
       -                berror("_frfreebox");
       -        n1++;
       -        for(i=n0; i<n1; i++)
       -                if(f->box[i].nrune >= 0)
       -                        free(f->box[i].a.ptr);
       +    int i;
       +
       +    if(n1<n0)
       +        return;
       +    if(n0>=f->nbox || n1>=f->nbox)
       +        berror("_frfreebox");
       +    n1++;
       +    for(i=n0; i<n1; i++)
       +        if(f->box[i].nrune >= 0)
       +            free(f->box[i].a.ptr);
        }
        
        void
        _frgrowbox(Frame *f, int delta)
        {
       -        f->nalloc += delta;
       -        f->box = realloc(f->box, f->nalloc*sizeof(Frbox));
       -        if(f->box == 0)
       -                berror("_frgrowbox");
       +    f->nalloc += delta;
       +    f->box = realloc(f->box, f->nalloc*sizeof(Frbox));
       +    if(f->box == 0)
       +        berror("_frgrowbox");
        }
        
        static
        void
        dupbox(Frame *f, int bn)
        {
       -        uchar *p;
       -
       -        if(f->box[bn].nrune < 0)
       -                berror("dupbox");
       -        _fraddbox(f, bn, 1);
       -        if(f->box[bn].nrune >= 0){
       -                p = _frallocstr(NBYTE(&f->box[bn])+1);
       -                strcpy((char*)p, (char*)f->box[bn].a.ptr);
       -                f->box[bn+1].a.ptr = p;
       -        }
       +    uchar *p;
       +
       +    if(f->box[bn].nrune < 0)
       +        berror("dupbox");
       +    _fraddbox(f, bn, 1);
       +    if(f->box[bn].nrune >= 0){
       +        p = _frallocstr(NBYTE(&f->box[bn])+1);
       +        strcpy((char*)p, (char*)f->box[bn].a.ptr);
       +        f->box[bn+1].a.ptr = p;
       +    }
        }
        
        static
        uchar*
        runeindex(uchar *p, int n)
        {
       -        int i, w;
       -        Rune rune;
       -
       -        for(i=0; i<n; i++,p+=w)
       -                if(*p < Runeself)
       -                        w = 1;
       -                else{
       -                        w = chartorune(&rune, (char*)p);
       -                        USED(rune);
       -                }
       -        return p;
       +    int i, w;
       +    Rune rune;
       +
       +    for(i=0; i<n; i++,p+=w)
       +        if(*p < Runeself)
       +            w = 1;
       +        else{
       +            w = chartorune(&rune, (char*)p);
       +            USED(rune);
       +        }
       +    return p;
        }
        
        static
        void
       -truncatebox(Frame *f, Frbox *b, int n)        /* drop last n chars; no allocation done */
       +truncatebox(Frame *f, Frbox *b, int n)  /* drop last n chars; no allocation done */
        {
       -        if(b->nrune<0 || b->nrune<n)
       -                berror("truncatebox");
       -        b->nrune -= n;
       -        runeindex(b->a.ptr, b->nrune)[0] = 0;
       -        b->wid = strwidth(f->font, (char *)b->a.ptr);
       +    if(b->nrune<0 || b->nrune<n)
       +        berror("truncatebox");
       +    b->nrune -= n;
       +    runeindex(b->a.ptr, b->nrune)[0] = 0;
       +    b->wid = strwidth(f->font, (char *)b->a.ptr);
        }
        
        static
        void
       -chopbox(Frame *f, Frbox *b, int n)        /* drop first n chars; no allocation done */
       +chopbox(Frame *f, Frbox *b, int n)  /* drop first n chars; no allocation done */
        {
       -        if(b->nrune<0 || b->nrune<n)
       -                berror("chopbox");
       -        strcpy((char*)b->a.ptr, (char*)runeindex(b->a.ptr, n));
       -        b->nrune -= n;
       -        b->wid = strwidth(f->font, (char *)b->a.ptr);
       +    if(b->nrune<0 || b->nrune<n)
       +        berror("chopbox");
       +    strcpy((char*)b->a.ptr, (char*)runeindex(b->a.ptr, n));
       +    b->nrune -= n;
       +    b->wid = strwidth(f->font, (char *)b->a.ptr);
        }
        
        void
        _frsplitbox(Frame *f, int bn, int n)
        {
       -        dupbox(f, bn);
       -        truncatebox(f, &f->box[bn], f->box[bn].nrune-n);
       -        chopbox(f, &f->box[bn+1], n);
       +    dupbox(f, bn);
       +    truncatebox(f, &f->box[bn], f->box[bn].nrune-n);
       +    chopbox(f, &f->box[bn+1], n);
        }
        
        void
       -_frmergebox(Frame *f, int bn)                /* merge bn and bn+1 */
       +_frmergebox(Frame *f, int bn)       /* merge bn and bn+1 */
        {
       -        Frbox *b;
       -
       -        b = &f->box[bn];
       -        _frinsure(f, bn, NBYTE(&b[0])+NBYTE(&b[1])+1);
       -        strcpy((char*)runeindex(b[0].a.ptr, b[0].nrune), (char*)b[1].a.ptr);
       -        b[0].wid += b[1].wid;
       -        b[0].nrune += b[1].nrune;
       -        _frdelbox(f, bn+1, bn+1);
       +    Frbox *b;
       +
       +    b = &f->box[bn];
       +    _frinsure(f, bn, NBYTE(&b[0])+NBYTE(&b[1])+1);
       +    strcpy((char*)runeindex(b[0].a.ptr, b[0].nrune), (char*)b[1].a.ptr);
       +    b[0].wid += b[1].wid;
       +    b[0].nrune += b[1].nrune;
       +    _frdelbox(f, bn+1, bn+1);
        }
        
        int
       -_frfindbox(Frame *f, int bn, ulong p, ulong q)        /* find box containing q and put q on a box boundary */
       +_frfindbox(Frame *f, int bn, ulong p, ulong q)  /* find box containing q and put q on a box boundary */
        {
       -        Frbox *b;
       +    Frbox *b;
        
       -        for(b = &f->box[bn]; bn<f->nbox && p+NRUNE(b)<=q; bn++, b++)
       -                p += NRUNE(b);
       -        if(p != q)
       -                _frsplitbox(f, bn++, (int)(q-p));
       -        return bn;
       +    for(b = &f->box[bn]; bn<f->nbox && p+NRUNE(b)<=q; bn++, b++)
       +        p += NRUNE(b);
       +    if(p != q)
       +        _frsplitbox(f, bn++, (int)(q-p));
       +    return bn;
        }
 (DIR) diff --git a/libframe/frdelete.c b/libframe/frdelete.c
       @@ -6,99 +6,99 @@
        int
        frdelete(Frame *f, ulong p0, ulong p1)
        {
       -        Point pt0, pt1, ppt0;
       -        Frbox *b;
       -        int n0, n1, n;
       -        Rectangle r;
       -        int nn0;
       +    Point pt0, pt1, ppt0;
       +    Frbox *b;
       +    int n0, n1, n;
       +    Rectangle r;
       +    int nn0;
        
       -        if(p0>=f->nchars || p0==p1 || f->b==0)
       -                return 0;
       -        if(p1 > f->nchars)
       -                p1 = f->nchars;
       -        n0 = _frfindbox(f, 0, (unsigned long)0, p0);
       -        n1 = _frfindbox(f, n0, p0, p1);
       -        pt0 = _frptofcharnb(f, p0, n0);
       -        pt1 = frptofchar(f, p1);
       -        if(f->p0!=p0 || f->p1!=p1)        /* likely they ARE equal */
       -                frselectp(f, F&~D);        /* can do better some day */
       -        frselectf(f, pt0, pt1, 0);
       -        if(n0 == f->nbox)
       -                berror("off end in frdelete");
       -        nn0 = n0;
       -        ppt0 = pt0;
       -        _frfreebox(f, n0, n1-1);
       -        f->modified = 1;
       +    if(p0>=f->nchars || p0==p1 || f->b==0)
       +        return 0;
       +    if(p1 > f->nchars)
       +        p1 = f->nchars;
       +    n0 = _frfindbox(f, 0, (unsigned long)0, p0);
       +    n1 = _frfindbox(f, n0, p0, p1);
       +    pt0 = _frptofcharnb(f, p0, n0);
       +    pt1 = frptofchar(f, p1);
       +    if(f->p0!=p0 || f->p1!=p1)  /* likely they ARE equal */
       +        frselectp(f, F&~D); /* can do better some day */
       +    frselectf(f, pt0, pt1, 0);
       +    if(n0 == f->nbox)
       +        berror("off end in frdelete");
       +    nn0 = n0;
       +    ppt0 = pt0;
       +    _frfreebox(f, n0, n1-1);
       +    f->modified = 1;
        
       -        /*
       -         * Invariants:
       -         *  pt0 points to beginning, pt1 points to end
       -         *  n0 is box containing beginning of stuff being deleted
       -         *  n1, b are box containing beginning of stuff to be kept after deletion
       -         *  region between pt0 and pt1 is clear
       -         */
       -        b = &f->box[n1];
       -        while(pt1.x!=pt0.x && n1<f->nbox){
       -                _frcklinewrap0(f, &pt0, b);
       -                _frcklinewrap(f, &pt1, b);
       -                if(b->nrune > 0){
       -                        n = _frcanfit(f, pt0, b);
       -                        if(n==0)
       -                                berror("_frcanfit==0");
       -                        if(n != b->nrune){
       -                                _frsplitbox(f, n1, n);
       -                                b = &f->box[n1];
       -                        }
       -                        r.min = pt1;
       -                        r.max = pt1;
       -                        r.max.x += b->wid;
       -                        r.max.y += f->fheight;
       -                        bitblt2(f->b, pt0, f->b, r, S, 0, f->bg);
       -                        if(pt0.y == pt1.y)
       -                                r.min.x = r.max.x-(pt1.x-pt0.x);
       -                        bitblt2(f->b, r.min, f->b, r, 0, 0, f->bg);
       -                }
       -                _fradvance(f, &pt1, b);
       -                pt0.x += _frnewwid(f, pt0, b);
       -                f->box[n0++] = f->box[n1++];
       -                b++;
       -        }
       -        if(pt1.y != pt0.y){
       -                Point pt2;
       +    /*
       +     * Invariants:
       +     *  pt0 points to beginning, pt1 points to end
       +     *  n0 is box containing beginning of stuff being deleted
       +     *  n1, b are box containing beginning of stuff to be kept after deletion
       +     *  region between pt0 and pt1 is clear
       +     */
       +    b = &f->box[n1];
       +    while(pt1.x!=pt0.x && n1<f->nbox){
       +        _frcklinewrap0(f, &pt0, b);
       +        _frcklinewrap(f, &pt1, b);
       +        if(b->nrune > 0){
       +            n = _frcanfit(f, pt0, b);
       +            if(n==0)
       +                berror("_frcanfit==0");
       +            if(n != b->nrune){
       +                _frsplitbox(f, n1, n);
       +                b = &f->box[n1];
       +            }
       +            r.min = pt1;
       +            r.max = pt1;
       +            r.max.x += b->wid;
       +            r.max.y += f->fheight;
       +            bitblt2(f->b, pt0, f->b, r, S, 0, f->bg);
       +            if(pt0.y == pt1.y)
       +                r.min.x = r.max.x-(pt1.x-pt0.x);
       +            bitblt2(f->b, r.min, f->b, r, 0, 0, f->bg);
       +        }
       +        _fradvance(f, &pt1, b);
       +        pt0.x += _frnewwid(f, pt0, b);
       +        f->box[n0++] = f->box[n1++];
       +        b++;
       +    }
       +    if(pt1.y != pt0.y){
       +        Point pt2;
        
       -                pt2 = _frptofcharptb(f, 32767, pt1, n1);
       -                if(pt2.y > f->r.max.y)
       -                        berror("frptofchar in frdelete");
       -                if(n1 < f->nbox){
       -                        int q0, q1, q2;
       +        pt2 = _frptofcharptb(f, 32767, pt1, n1);
       +        if(pt2.y > f->r.max.y)
       +            berror("frptofchar in frdelete");
       +        if(n1 < f->nbox){
       +            int q0, q1, q2;
        
       -                        q0 = pt0.y+f->fheight;
       -                        q1 = pt1.y+f->fheight;
       -                        q2 = pt2.y+f->fheight;
       -                        bitblt2(f->b, pt0, f->b, Rect(pt1.x, pt1.y, f->r.max.x, q1), S, 0, f->bg);
       -                        bitblt2(f->b, Pt(f->r.min.x, q0), f->b, Rect(f->r.min.x, q1, f->r.max.x, q2), S, 0, f->bg);
       -                        frselectf(f, Pt(pt2.x, pt2.y-(pt1.y-pt0.y)), pt2, 0);
       -                }else
       -                        frselectf(f, pt0, pt2, 0);
       -        }
       -        _frclosebox(f, n0, n1-1);
       -        if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=(int)f->left){
       -                --nn0;
       -                ppt0.x -= f->box[nn0].wid;
       -        }
       -        _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
       -        if(f->p1 > p1)
       -                f->p1 -= p1-p0;
       -        else if(f->p1 > p0)
       -                f->p1 = p0;
       -        if(f->p0 > p1)
       -                f->p0 -= p1-p0;
       -        else if(f->p0 > p0)
       -                f->p0 = p0;
       -        frselectp(f, F&~D);
       -        f->nchars -= p1-p0;
       -        pt0 = frptofchar(f, f->nchars);
       -        n = f->nlines;
       -        f->nlines = (pt0.y-f->r.min.y)/f->fheight+(pt0.x>f->left);
       -        return n - f->nlines;
       +            q0 = pt0.y+f->fheight;
       +            q1 = pt1.y+f->fheight;
       +            q2 = pt2.y+f->fheight;
       +            bitblt2(f->b, pt0, f->b, Rect(pt1.x, pt1.y, f->r.max.x, q1), S, 0, f->bg);
       +            bitblt2(f->b, Pt(f->r.min.x, q0), f->b, Rect(f->r.min.x, q1, f->r.max.x, q2), S, 0, f->bg);
       +            frselectf(f, Pt(pt2.x, pt2.y-(pt1.y-pt0.y)), pt2, 0);
       +        }else
       +            frselectf(f, pt0, pt2, 0);
       +    }
       +    _frclosebox(f, n0, n1-1);
       +    if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=(int)f->left){
       +        --nn0;
       +        ppt0.x -= f->box[nn0].wid;
       +    }
       +    _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
       +    if(f->p1 > p1)
       +        f->p1 -= p1-p0;
       +    else if(f->p1 > p0)
       +        f->p1 = p0;
       +    if(f->p0 > p1)
       +        f->p0 -= p1-p0;
       +    else if(f->p0 > p0)
       +        f->p0 = p0;
       +    frselectp(f, F&~D);
       +    f->nchars -= p1-p0;
       +    pt0 = frptofchar(f, f->nchars);
       +    n = f->nlines;
       +    f->nlines = (pt0.y-f->r.min.y)/f->fheight+(pt0.x>f->left);
       +    return n - f->nlines;
        }
 (DIR) diff --git a/libframe/frdraw.c b/libframe/frdraw.c
       @@ -7,53 +7,53 @@
        void
        _frredraw(Frame *f, Point pt)
        {
       -        Frbox *b;
       -        int nb;
       -        for(nb=0,b=f->box; nb<f->nbox; nb++, b++){
       -                _frcklinewrap(f, &pt, b);
       -                if(b->nrune >= 0)
       -                        string(f->b, pt, f->font, (char *)b->a.ptr, S^D);
       -                pt.x += b->wid;
       -        }
       +    Frbox *b;
       +    int nb;
       +    for(nb=0,b=f->box; nb<f->nbox; nb++, b++){
       +        _frcklinewrap(f, &pt, b);
       +        if(b->nrune >= 0)
       +            string(f->b, pt, f->font, (char *)b->a.ptr, S^D);
       +        pt.x += b->wid;
       +    }
        }
        
        Point
        _frdraw(Frame *f, Point pt)
        {
       -        Frbox *b;
       -        int nb, n;
       -        for(b=f->box,nb=0; nb<f->nbox; nb++, b++){
       -                _frcklinewrap0(f, &pt, b);
       -                if(pt.y == f->r.max.y){
       -                        f->nchars -= _frstrlen(f, nb);
       -                        _frdelbox(f, nb, f->nbox-1);
       -                        break;
       -                }
       -                if(b->nrune > 0){
       -                        n = _frcanfit(f, pt, b);
       -                        if(n == 0)
       -                                berror("draw: _frcanfit==0");
       -                        if(n != b->nrune){
       -                                _frsplitbox(f, nb, n);
       -                                b = &f->box[nb];
       -                        }
       -                        pt.x += b->wid;
       -                }else{
       -                        if(b->a.b.bc == '\n')
       -                                pt.x = f->left, pt.y+=f->fheight;
       -                        else
       -                                pt.x += _frnewwid(f, pt, b);
       -                }
       -        }
       -        return pt;
       +    Frbox *b;
       +    int nb, n;
       +    for(b=f->box,nb=0; nb<f->nbox; nb++, b++){
       +        _frcklinewrap0(f, &pt, b);
       +        if(pt.y == f->r.max.y){
       +            f->nchars -= _frstrlen(f, nb);
       +            _frdelbox(f, nb, f->nbox-1);
       +            break;
       +        }
       +        if(b->nrune > 0){
       +            n = _frcanfit(f, pt, b);
       +            if(n == 0)
       +                berror("draw: _frcanfit==0");
       +            if(n != b->nrune){
       +                _frsplitbox(f, nb, n);
       +                b = &f->box[nb];
       +            }
       +            pt.x += b->wid;
       +        }else{
       +            if(b->a.b.bc == '\n')
       +                pt.x = f->left, pt.y+=f->fheight;
       +            else
       +                pt.x += _frnewwid(f, pt, b);
       +        }
       +    }
       +    return pt;
        }
        
        int
        _frstrlen(Frame *f, int nb)
        {
       -        int n;
       +    int n;
        
       -        for(n=0; nb<f->nbox; nb++)
       -                n += NRUNE(&f->box[nb]);
       -        return n;
       +    for(n=0; nb<f->nbox; nb++)
       +        n += NRUNE(&f->box[nb]);
       +    return n;
        }
 (DIR) diff --git a/libframe/frinit.c b/libframe/frinit.c
       @@ -10,49 +10,49 @@ extern int expandtabs;
        void
        frinit(Frame *f, Rectangle r, XftFont *ft, Bitmap *b, unsigned long bg)
        {
       -        int tabs = atoi(getenv("TABS") ? getenv("TABS") : "");
       +    int tabs = atoi(getenv("TABS") ? getenv("TABS") : "");
            if (tabs < 0){
                tabs = -tabs;
                expandtabs = 1;
            }
        
       -        if (tabs > 0 && tabs <= 12)
       -                tabwidth = tabs;
       +    if (tabs > 0 && tabs <= 12)
       +        tabwidth = tabs;
        
       -        f->font = ft;
       -        /* ft->height is NOT CORRECT; we must use ascent + descent to
       -           clear the lowest edge of characters. - cks */
       -        f->fheight = ft->ascent + ft->descent;
       -        f->maxtab = tabwidth*charwidth(ft, '0');
       -        f->nbox = 0;
       -        f->nalloc = 0;
       -        f->nchars = 0;
       -        f->nlines = 0;
       -        f->p0 = 0;
       -        f->p1 = 0;
       -        f->box = 0;
       -        f->lastlinefull = 0;
       +    f->font = ft;
       +    /* ft->height is NOT CORRECT; we must use ascent + descent to
       +       clear the lowest edge of characters. - cks */
       +    f->fheight = ft->ascent + ft->descent;
       +    f->maxtab = tabwidth*charwidth(ft, '0');
       +    f->nbox = 0;
       +    f->nalloc = 0;
       +    f->nchars = 0;
       +    f->nlines = 0;
       +    f->p0 = 0;
       +    f->p1 = 0;
       +    f->box = 0;
       +    f->lastlinefull = 0;
            f->bg = bg;
       -        frsetrects(f, r, b);
       +    frsetrects(f, r, b);
        }
        
        void
        frsetrects(Frame *f, Rectangle r, Bitmap *b)
        {
       -        f->b = b;
       -        f->entire = r;
       -        f->r = r;
       -        f->r.max.y -= (r.max.y-r.min.y)%f->fheight;
       -        f->left = r.min.x+1;
       -        f->maxlines = (r.max.y-r.min.y)/f->fheight;
       +    f->b = b;
       +    f->entire = r;
       +    f->r = r;
       +    f->r.max.y -= (r.max.y-r.min.y)%f->fheight;
       +    f->left = r.min.x+1;
       +    f->maxlines = (r.max.y-r.min.y)/f->fheight;
        }
        
        void
        frclear(Frame *f)
        {
       -        if(f->nbox)
       -                _frdelbox(f, 0, f->nbox-1);
       -        if(f->box)
       -                free(f->box);
       -        f->box = 0;
       +    if(f->nbox)
       +        _frdelbox(f, 0, f->nbox-1);
       +    if(f->box)
       +        free(f->box);
       +    f->box = 0;
        }
 (DIR) diff --git a/libframe/frinsert.c b/libframe/frinsert.c
       @@ -4,245 +4,245 @@
        #include <libg.h>
        #include <frame.h>
        
       -#define        DELTA        25
       -#define        TMPSIZE        256
       -static Frame                frame;
       +#define DELTA   25
       +#define TMPSIZE 256
       +static Frame        frame;
        
        static
        Point
        bxscan(Frame *f, Rune *sp, Rune *ep, Point *ppt)
        {
       -        int w, c, nb, delta, nl, nr, rw;
       -        Frbox *b;
       -        char *s, tmp[TMPSIZE+3];        /* +3 for rune overflow */
       -        uchar *p;
       +    int w, c, nb, delta, nl, nr, rw;
       +    Frbox *b;
       +    char *s, tmp[TMPSIZE+3];    /* +3 for rune overflow */
       +    uchar *p;
        
       -        frame.r = f->r;
       -        frame.b = f->b;
       -        frame.font = f->font;
       -        frame.fheight = f->font->ascent + f->font->descent;
       -        frame.maxtab = f->maxtab;
       -        frame.left = f->left;
       -        frame.nbox = 0;
       -        frame.nchars = 0;
       -        delta = DELTA;
       -        nl = 0;
       -        for(nb=0; sp<ep && nl<=f->maxlines; nb++,frame.nbox++){
       -                if(nb == frame.nalloc){
       -                        _frgrowbox(&frame, delta);
       -                        if(delta < 10000)
       -                                delta *= 2;
       -                }
       -                b = &frame.box[nb];
       -                c = *sp;
       -                if(c=='\t' || c=='\n'){
       -                        b->a.b.bc = c;
       -                        b->wid = 5000;
       -                        b->a.b.minwid = (c=='\n')? 0 : charwidth(frame.font, ' ');
       -                        b->nrune = -1;
       -                        if(c=='\n')
       -                                nl++;
       -                        frame.nchars++;
       -                        sp++;
       -                }else{
       -                        s = tmp;
       -                        nr = 0;
       -                        w = 0;
       -                        while(sp < ep){
       -                                c = *sp;
       -                                if(c=='\t' || c=='\n')
       -                                        break;
       -                                rw = runetochar(s, sp);
       -                                if(s+rw >= tmp+TMPSIZE)
       -                                        break;
       -                                w += charwidth(frame.font, c);
       -                                sp++;
       -                                s += rw;
       -                                nr++;
       -                        }
       -                        *s++ = 0;
       -                        p = _frallocstr(s-tmp);
       -                        b = &frame.box[nb];
       -                        b->a.ptr = p;
       -                        memmove(p, tmp, s-tmp);
       -                        b->wid = w;
       -                        b->nrune = nr;
       -                        frame.nchars += nr;
       -                }
       -        }
       -        _frcklinewrap0(f, ppt, &frame.box[0]);
       -        return _frdraw(&frame, *ppt);
       +    frame.r = f->r;
       +    frame.b = f->b;
       +    frame.font = f->font;
       +    frame.fheight = f->font->ascent + f->font->descent;
       +    frame.maxtab = f->maxtab;
       +    frame.left = f->left;
       +    frame.nbox = 0;
       +    frame.nchars = 0;
       +    delta = DELTA;
       +    nl = 0;
       +    for(nb=0; sp<ep && nl<=f->maxlines; nb++,frame.nbox++){
       +        if(nb == frame.nalloc){
       +            _frgrowbox(&frame, delta);
       +            if(delta < 10000)
       +                delta *= 2;
       +        }
       +        b = &frame.box[nb];
       +        c = *sp;
       +        if(c=='\t' || c=='\n'){
       +            b->a.b.bc = c;
       +            b->wid = 5000;
       +            b->a.b.minwid = (c=='\n')? 0 : charwidth(frame.font, ' ');
       +            b->nrune = -1;
       +            if(c=='\n')
       +                nl++;
       +            frame.nchars++;
       +            sp++;
       +        }else{
       +            s = tmp;
       +            nr = 0;
       +            w = 0;
       +            while(sp < ep){
       +                c = *sp;
       +                if(c=='\t' || c=='\n')
       +                    break;
       +                rw = runetochar(s, sp);
       +                if(s+rw >= tmp+TMPSIZE)
       +                    break;
       +                w += charwidth(frame.font, c);
       +                sp++;
       +                s += rw;
       +                nr++;
       +            }
       +            *s++ = 0;
       +            p = _frallocstr(s-tmp);
       +            b = &frame.box[nb];
       +            b->a.ptr = p;
       +            memmove(p, tmp, s-tmp);
       +            b->wid = w;
       +            b->nrune = nr;
       +            frame.nchars += nr;
       +        }
       +    }
       +    _frcklinewrap0(f, ppt, &frame.box[0]);
       +    return _frdraw(&frame, *ppt);
        }
        
        static
        void
        chopframe(Frame *f, Point pt, ulong p, int bn)
        {
       -        Frbox *b;
       +    Frbox *b;
        
       -        for(b = &f->box[bn]; ; b++){
       -                if(b >= &f->box[f->nbox])
       -                        berror("endofframe");
       -                _frcklinewrap(f, &pt, b);
       -                if(pt.y >= f->r.max.y)
       -                        break;
       -                p += NRUNE(b);
       -                _fradvance(f, &pt, b);
       -        }
       -        f->nchars = p;
       -        f->nlines = f->maxlines;
       -        if(b<&f->box[f->nbox])                                /* BUG */
       -                _frdelbox(f, (int)(b-f->box), f->nbox-1);
       +    for(b = &f->box[bn]; ; b++){
       +        if(b >= &f->box[f->nbox])
       +            berror("endofframe");
       +        _frcklinewrap(f, &pt, b);
       +        if(pt.y >= f->r.max.y)
       +            break;
       +        p += NRUNE(b);
       +        _fradvance(f, &pt, b);
       +    }
       +    f->nchars = p;
       +    f->nlines = f->maxlines;
       +    if(b<&f->box[f->nbox])              /* BUG */
       +        _frdelbox(f, (int)(b-f->box), f->nbox-1);
        }
        
        void
        frinsert(Frame *f, Rune *sp, Rune *ep, ulong p0)
        {
       -        Point pt0, pt1, ppt0, ppt1, pt;
       -        Frbox *b;
       -        int n, n0, nn0, y;
       -        Rectangle r;
       -        static struct{
       -                Point pt0, pt1;
       -        }*pts;
       -        static int nalloc=0;
       -        int npts;
       +    Point pt0, pt1, ppt0, ppt1, pt;
       +    Frbox *b;
       +    int n, n0, nn0, y;
       +    Rectangle r;
       +    static struct{
       +        Point pt0, pt1;
       +    }*pts;
       +    static int nalloc=0;
       +    int npts;
        
       -        if(p0>f->nchars || sp==ep || f->b==0)
       -                return;
       -        n0 = _frfindbox(f, 0, 0, p0);
       -        nn0 = n0;
       -        pt0 = _frptofcharnb(f, p0, n0);
       -        ppt0 = pt0;
       -        pt1 = bxscan(f, sp, ep, &ppt0);
       -        ppt1 = pt1;
       -        if(n0 < f->nbox){
       -                _frcklinewrap(f, &pt0, b = &f->box[n0]);        /* for frselectf() */
       -                _frcklinewrap0(f, &ppt1, b);
       -        }
       -        f->modified = 1;
       -        /*
       -         * ppt0 and ppt1 are start and end of insertion as they will appear when
       -         * insertion is complete. pt0 is current location of insertion position
       -         * (p0); pt1 is terminal point (without line wrap) of insertion.
       -         */
       -        if(p0==f->p0 && p0==f->p1)                /* quite likely */
       -                frselectf(f, pt0, pt0, F&~D);
       -        else
       -                frselectp(f, F&~D);
       -        /*
       -         * Find point where old and new x's line up
       -         * Invariants:
       -         *        pt0 is where the next box (b, n0) is now
       -         *        pt1 is where it will be after then insertion
       -         * If pt1 goes off the rectangle, we can toss everything from there on
       -         */
       -        for(b = &f->box[n0],npts=0;
       -             pt1.x!=pt0.x && pt1.y!=f->r.max.y && n0<f->nbox; b++,n0++,npts++){
       -                _frcklinewrap(f, &pt0, b);
       -                _frcklinewrap0(f, &pt1, b);
       -                if(b->nrune > 0){
       -                        n = _frcanfit(f, pt1, b);
       -                        if(n == 0)
       -                                berror("_frcanfit==0");
       -                        if(n != b->nrune){
       -                                _frsplitbox(f, n0, n);
       -                                b = &f->box[n0];
       -                        }
       -                }
       -                if(npts == nalloc){
       -                        pts = realloc(pts, (npts+DELTA)*sizeof(pts[0]));
       -                        nalloc += DELTA;
       -                        b = &f->box[n0];
       -                }
       -                pts[npts].pt0 = pt0;
       -                pts[npts].pt1 = pt1;
       -                /* has a text box overflowed off the frame? */
       -                if(pt1.y == f->r.max.y)
       -                        break;
       -                _fradvance(f, &pt0, b);
       -                pt1.x += _frnewwid(f, pt1, b);
       -        }
       -        if(pt1.y > f->r.max.y)
       -                berror("frinsert pt1 too far");
       -        if(pt1.y==f->r.max.y && n0<f->nbox){
       -                f->nchars -= _frstrlen(f, n0);
       -                _frdelbox(f, n0, f->nbox-1);
       -        }
       -        if(n0 == f->nbox)
       -                f->nlines = (pt1.y-f->r.min.y)/f->fheight+(pt1.x>f->left);
       -        else if(pt1.y!=pt0.y){
       -                int q0, q1;
       +    if(p0>f->nchars || sp==ep || f->b==0)
       +        return;
       +    n0 = _frfindbox(f, 0, 0, p0);
       +    nn0 = n0;
       +    pt0 = _frptofcharnb(f, p0, n0);
       +    ppt0 = pt0;
       +    pt1 = bxscan(f, sp, ep, &ppt0);
       +    ppt1 = pt1;
       +    if(n0 < f->nbox){
       +        _frcklinewrap(f, &pt0, b = &f->box[n0]);    /* for frselectf() */
       +        _frcklinewrap0(f, &ppt1, b);
       +    }
       +    f->modified = 1;
       +    /*
       +     * ppt0 and ppt1 are start and end of insertion as they will appear when
       +     * insertion is complete. pt0 is current location of insertion position
       +     * (p0); pt1 is terminal point (without line wrap) of insertion.
       +     */
       +    if(p0==f->p0 && p0==f->p1)      /* quite likely */
       +        frselectf(f, pt0, pt0, F&~D);
       +    else
       +        frselectp(f, F&~D);
       +    /*
       +     * Find point where old and new x's line up
       +     * Invariants:
       +     *  pt0 is where the next box (b, n0) is now
       +     *  pt1 is where it will be after then insertion
       +     * If pt1 goes off the rectangle, we can toss everything from there on
       +     */
       +    for(b = &f->box[n0],npts=0;
       +         pt1.x!=pt0.x && pt1.y!=f->r.max.y && n0<f->nbox; b++,n0++,npts++){
       +        _frcklinewrap(f, &pt0, b);
       +        _frcklinewrap0(f, &pt1, b);
       +        if(b->nrune > 0){
       +            n = _frcanfit(f, pt1, b);
       +            if(n == 0)
       +                berror("_frcanfit==0");
       +            if(n != b->nrune){
       +                _frsplitbox(f, n0, n);
       +                b = &f->box[n0];
       +            }
       +        }
       +        if(npts == nalloc){
       +            pts = realloc(pts, (npts+DELTA)*sizeof(pts[0]));
       +            nalloc += DELTA;
       +            b = &f->box[n0];
       +        }
       +        pts[npts].pt0 = pt0;
       +        pts[npts].pt1 = pt1;
       +        /* has a text box overflowed off the frame? */
       +        if(pt1.y == f->r.max.y)
       +            break;
       +        _fradvance(f, &pt0, b);
       +        pt1.x += _frnewwid(f, pt1, b);
       +    }
       +    if(pt1.y > f->r.max.y)
       +        berror("frinsert pt1 too far");
       +    if(pt1.y==f->r.max.y && n0<f->nbox){
       +        f->nchars -= _frstrlen(f, n0);
       +        _frdelbox(f, n0, f->nbox-1);
       +    }
       +    if(n0 == f->nbox)
       +        f->nlines = (pt1.y-f->r.min.y)/f->fheight+(pt1.x>f->left);
       +    else if(pt1.y!=pt0.y){
       +        int q0, q1;
        
       -                y = f->r.max.y;
       -                q0 = pt0.y+f->fheight;
       -                q1 = pt1.y+f->fheight;
       -                f->nlines += (q1-q0)/f->fheight;
       -                if(f->nlines > f->maxlines)
       -                        chopframe(f, ppt1, p0, nn0);
       -                if(pt1.y < y){
       -                        r = f->r;
       -                        r.min.y = q0;
       -                        r.max.y = y-(q1-q0);
       -                        if(q1 < y)
       -                                bitblt2(f->b, Pt(f->r.min.x, q1), f->b, r, S, 0, f->bg);
       -                        r.min = pt0;
       -                        r.max.y = q0;
       -                        bitblt2(f->b, pt1, f->b, r, S, 0, f->bg);
       -                }
       -        }
       -        /*
       -         * Move the old stuff down to make room.  The loop will move the stuff
       -         * between the insertion and the point where the x's lined up.
       -         * The bitblt2 above moved everything down after the point they lined up.
       -         */
       -        for((y=pt1.y==f->r.max.y?pt1.y:0),b = &f->box[n0-1]; --npts>=0; --b){
       -                pt = pts[npts].pt1;
       -                if(b->nrune > 0){
       -                        r.min = pts[npts].pt0;
       -                        r.max = r.min;
       -                        r.max.x += b->wid;
       -                        r.max.y += f->fheight;
       -                        bitblt2(f->b, pt, f->b, r, S, 0, f->bg);
       -                        if(pt.y < y){        /* clear bit hanging off right */
       -                                r.min = pt;
       -                                r.max = pt;
       -                                r.min.x += b->wid;
       -                                r.max.x = f->r.max.x;
       -                                r.max.y += f->fheight;
       -                                bitblt2(f->b, r.min, f->b, r, 0, 0, f->bg);
       -                        }
       -                        y = pt.y;
       -                }else{
       -                        r.min = pt;
       -                        r.max = pt;
       -                        r.max.x += b->wid;
       -                        r.max.y += f->fheight;
       -                        if(r.max.x >= f->r.max.x)
       -                                r.max.x = f->r.max.x;
       -                        bitblt2(f->b, r.min, f->b, r, 0, 0, f->bg);
       -                        y = (pt.x == f->left)? pt.y : 0;
       -                }
       -        }
       -        frselectf(f, ppt0, ppt1, 0);
       -        _frredraw(&frame, ppt0);
       -        _fraddbox(f, nn0, frame.nbox);
       -        for(n=0; n<frame.nbox; n++)
       -                f->box[nn0+n] = frame.box[n];
       -        if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=(int)f->left){
       -                --nn0;
       -                ppt0.x -= f->box[nn0].wid;
       -        }
       -        n0 += frame.nbox;
       -        _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
       -        f->nchars += frame.nchars;
       -        if(f->p0 >= p0)
       -                f->p0 += frame.nchars;
       -        if(f->p0 > f->nchars)
       -                f->p0 = f->nchars;
       -        if(f->p1 >= p0)
       -                f->p1 += frame.nchars;
       -        if(f->p1 > f->nchars)
       -                f->p1 = f->nchars;
       -        frselectp(f, F&~D);
       +        y = f->r.max.y;
       +        q0 = pt0.y+f->fheight;
       +        q1 = pt1.y+f->fheight;
       +        f->nlines += (q1-q0)/f->fheight;
       +        if(f->nlines > f->maxlines)
       +            chopframe(f, ppt1, p0, nn0);
       +        if(pt1.y < y){
       +            r = f->r;
       +            r.min.y = q0;
       +            r.max.y = y-(q1-q0);
       +            if(q1 < y)
       +                bitblt2(f->b, Pt(f->r.min.x, q1), f->b, r, S, 0, f->bg);
       +            r.min = pt0;
       +            r.max.y = q0;
       +            bitblt2(f->b, pt1, f->b, r, S, 0, f->bg);
       +        }
       +    }
       +    /*
       +     * Move the old stuff down to make room.  The loop will move the stuff
       +     * between the insertion and the point where the x's lined up.
       +     * The bitblt2 above moved everything down after the point they lined up.
       +     */
       +    for((y=pt1.y==f->r.max.y?pt1.y:0),b = &f->box[n0-1]; --npts>=0; --b){
       +        pt = pts[npts].pt1;
       +        if(b->nrune > 0){
       +            r.min = pts[npts].pt0;
       +            r.max = r.min;
       +            r.max.x += b->wid;
       +            r.max.y += f->fheight;
       +            bitblt2(f->b, pt, f->b, r, S, 0, f->bg);
       +            if(pt.y < y){   /* clear bit hanging off right */
       +                r.min = pt;
       +                r.max = pt;
       +                r.min.x += b->wid;
       +                r.max.x = f->r.max.x;
       +                r.max.y += f->fheight;
       +                bitblt2(f->b, r.min, f->b, r, 0, 0, f->bg);
       +            }
       +            y = pt.y;
       +        }else{
       +            r.min = pt;
       +            r.max = pt;
       +            r.max.x += b->wid;
       +            r.max.y += f->fheight;
       +            if(r.max.x >= f->r.max.x)
       +                r.max.x = f->r.max.x;
       +            bitblt2(f->b, r.min, f->b, r, 0, 0, f->bg);
       +            y = (pt.x == f->left)? pt.y : 0;
       +        }
       +    }
       +    frselectf(f, ppt0, ppt1, 0);
       +    _frredraw(&frame, ppt0);
       +    _fraddbox(f, nn0, frame.nbox);
       +    for(n=0; n<frame.nbox; n++)
       +        f->box[nn0+n] = frame.box[n];
       +    if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=(int)f->left){
       +        --nn0;
       +        ppt0.x -= f->box[nn0].wid;
       +    }
       +    n0 += frame.nbox;
       +    _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
       +    f->nchars += frame.nchars;
       +    if(f->p0 >= p0)
       +        f->p0 += frame.nchars;
       +    if(f->p0 > f->nchars)
       +        f->p0 = f->nchars;
       +    if(f->p1 >= p0)
       +        f->p1 += frame.nchars;
       +    if(f->p1 > f->nchars)
       +        f->p1 = f->nchars;
       +    frselectp(f, F&~D);
        }
 (DIR) diff --git a/libframe/frptofchar.c b/libframe/frptofchar.c
       @@ -7,110 +7,110 @@
        Point
        _frptofcharptb(Frame *f, ulong p, Point pt, int bn)
        {
       -        uchar *s;
       -        Frbox *b;
       -        int w, l;
       -        Rune r;
       +    uchar *s;
       +    Frbox *b;
       +    int w, l;
       +    Rune r;
        
       -        for(b = &f->box[bn]; bn<f->nbox; bn++,b++){
       -                _frcklinewrap(f, &pt, b);
       -                if(p < (l=NRUNE(b))){
       -                        if(b->nrune > 0)
       -                                for(s=b->a.ptr; p>0; s+=w, p--){
       -                                        if((r = *s) < Runeself)
       -                                                w = 1;
       -                                        else
       -                                                w = chartorune(&r, (char*)s);
       -                                        pt.x += charwidth(f->font, r);
       -                                        if(r==0 || pt.x>f->r.max.x)
       -                                                berror("frptofchar");
       -                                }
       -                        break;
       -                }
       -                p -= l;
       -                _fradvance(f, &pt, b);
       -        }
       -        return pt;
       +    for(b = &f->box[bn]; bn<f->nbox; bn++,b++){
       +        _frcklinewrap(f, &pt, b);
       +        if(p < (l=NRUNE(b))){
       +            if(b->nrune > 0)
       +                for(s=b->a.ptr; p>0; s+=w, p--){
       +                    if((r = *s) < Runeself)
       +                        w = 1;
       +                    else
       +                        w = chartorune(&r, (char*)s);
       +                    pt.x += charwidth(f->font, r);
       +                    if(r==0 || pt.x>f->r.max.x)
       +                        berror("frptofchar");
       +                }
       +            break;
       +        }
       +        p -= l;
       +        _fradvance(f, &pt, b);
       +    }
       +    return pt;
        }
        
        Point
        frptofchar(Frame *f, ulong p)
        {
       -        return _frptofcharptb(f, p, Pt(f->left, f->r.min.y), 0);
       +    return _frptofcharptb(f, p, Pt(f->left, f->r.min.y), 0);
        }
        
        Point
       -_frptofcharnb(Frame *f, ulong p, int nb)        /* doesn't do final _fradvance to next line */
       +_frptofcharnb(Frame *f, ulong p, int nb)    /* doesn't do final _fradvance to next line */
        {
       -        Point pt;
       -        int nbox;
       +    Point pt;
       +    int nbox;
        
       -        nbox = f->nbox;
       -        f->nbox = nb;
       -        pt = _frptofcharptb(f, p, Pt(f->left, f->r.min.y), 0);
       -        f->nbox = nbox;
       -        return pt;
       +    nbox = f->nbox;
       +    f->nbox = nb;
       +    pt = _frptofcharptb(f, p, Pt(f->left, f->r.min.y), 0);
       +    f->nbox = nbox;
       +    return pt;
        }
        
        static
        Point
        _frgrid(Frame *f, Point p)
        {
       -        p.y -= f->r.min.y;
       -        p.y -= p.y%f->fheight;
       -        p.y += f->r.min.y;
       -        if(p.x > f->r.max.x)
       -                p.x = f->r.max.x;
       -        return p;
       +    p.y -= f->r.min.y;
       +    p.y -= p.y%f->fheight;
       +    p.y += f->r.min.y;
       +    if(p.x > f->r.max.x)
       +        p.x = f->r.max.x;
       +    return p;
        }
        
        ulong
        frcharofpt(Frame *f, Point pt)
        {
       -        Point qt;
       -        int w, bn;
       -        uchar *s;
       -        Frbox *b;
       -        ulong p;
       -        Rune r;
       +    Point qt;
       +    int w, bn;
       +    uchar *s;
       +    Frbox *b;
       +    ulong p;
       +    Rune r;
        
       -        pt = _frgrid(f, pt);
       -        qt.x = f->left;
       -        qt.y = f->r.min.y;
       -        for(b=f->box,bn=0,p=0; bn<f->nbox && qt.y<pt.y; bn++,b++){
       -                _frcklinewrap(f, &qt, b);
       -                if(qt.y >= pt.y)
       -                        break;
       -                _fradvance(f, &qt, b);
       -                p += NRUNE(b);
       -        }
       -        for(; bn<f->nbox && qt.x<=pt.x; bn++,b++){
       -                _frcklinewrap(f, &qt, b);
       -                if(qt.y > pt.y)
       -                        break;
       -                if(qt.x+b->wid > pt.x){
       -                        if(b->nrune < 0)
       -                                _fradvance(f, &qt, b);
       -                        else{
       -                                s = b->a.ptr;
       -                                for(;;){
       -                                        if((r = *s) < Runeself)
       -                                                w = 1;
       -                                        else
       -                                                w = chartorune(&r, (char*)s);
       -                                        if(r == 0)
       -                                                berror("end of string in frcharofpt");
       -                                        s += w;
       -                                        qt.x += charwidth(f->font, r);
       -                                        if(qt.x > pt.x)
       -                                                break;
       -                                        p++;
       -                                }
       -                        }
       -                }else{
       -                        p += NRUNE(b);
       -                        _fradvance(f, &qt, b);
       -                }
       -        }
       -        return p;
       +    pt = _frgrid(f, pt);
       +    qt.x = f->left;
       +    qt.y = f->r.min.y;
       +    for(b=f->box,bn=0,p=0; bn<f->nbox && qt.y<pt.y; bn++,b++){
       +        _frcklinewrap(f, &qt, b);
       +        if(qt.y >= pt.y)
       +            break;
       +        _fradvance(f, &qt, b);
       +        p += NRUNE(b);
       +    }
       +    for(; bn<f->nbox && qt.x<=pt.x; bn++,b++){
       +        _frcklinewrap(f, &qt, b);
       +        if(qt.y > pt.y)
       +            break;
       +        if(qt.x+b->wid > pt.x){
       +            if(b->nrune < 0)
       +                _fradvance(f, &qt, b);
       +            else{
       +                s = b->a.ptr;
       +                for(;;){
       +                    if((r = *s) < Runeself)
       +                        w = 1;
       +                    else
       +                        w = chartorune(&r, (char*)s);
       +                    if(r == 0)
       +                        berror("end of string in frcharofpt");
       +                    s += w;
       +                    qt.x += charwidth(f->font, r);
       +                    if(qt.x > pt.x)
       +                        break;
       +                    p++;
       +                }
       +            }
       +        }else{
       +            p += NRUNE(b);
       +            _fradvance(f, &qt, b);
       +        }
       +    }
       +    return p;
        }
 (DIR) diff --git a/libframe/frselect.c b/libframe/frselect.c
       @@ -5,90 +5,90 @@
        #include <frame.h>
        
        void
       -frselect(Frame *f, Mouse *m)        /* when called, button 1 is down */
       +frselect(Frame *f, Mouse *m)    /* when called, button 1 is down */
        {
       -        ulong p0, p1, q;
       -        Point mp, pt0, pt1, qt;
       +    ulong p0, p1, q;
       +    Point mp, pt0, pt1, qt;
        
       -        mp = m->xy;
       +    mp = m->xy;
        
            Again:
       -        f->modified = 0;
       -        frselectp(f, F&~D);
       -        p0 = p1 = frcharofpt(f, mp);
       -        pt0 = frptofchar(f, p0);
       -        pt1 = frptofchar(f, p1);
       -        frselectf(f, pt0, pt1, F&~D);
       -        do{
       -                if(f->modified)        /* special hack so 8½ can frselect in parallel */
       -                        goto Again;
       -                q = frcharofpt(f, m->xy);
       -                if(p1 != q){
       -                        if(p0 == p1)
       -                                frselectf(f, pt0, pt1, F&~D);
       -                        qt = frptofchar(f, q);
       -                        if(p1 < q)
       -                                frselectf(f, pt1, qt, F&~D);
       -                        else
       -                                frselectf(f, qt, pt1, F&~D);
       -                        p1 = q;
       -                        pt1 = qt;
       -                        if(p0 == p1)
       -                                frselectf(f, pt0, pt1, F&~D);
       -                }
       -                f->modified = 0;
       -                if(p0 < p1)
       -                        f->p0 = p0, f->p1 = p1;
       -                else
       -                        f->p0 = p1, f->p1 = p0;
       -                frgetmouse();
       -        }while((m->buttons & 7) == 1);
       +    f->modified = 0;
       +    frselectp(f, F&~D);
       +    p0 = p1 = frcharofpt(f, mp);
       +    pt0 = frptofchar(f, p0);
       +    pt1 = frptofchar(f, p1);
       +    frselectf(f, pt0, pt1, F&~D);
       +    do{
       +        if(f->modified) /* special hack so 8½ can frselect in parallel */
       +            goto Again;
       +        q = frcharofpt(f, m->xy);
       +        if(p1 != q){
       +            if(p0 == p1)
       +                frselectf(f, pt0, pt1, F&~D);
       +            qt = frptofchar(f, q);
       +            if(p1 < q)
       +                frselectf(f, pt1, qt, F&~D);
       +            else
       +                frselectf(f, qt, pt1, F&~D);
       +            p1 = q;
       +            pt1 = qt;
       +            if(p0 == p1)
       +                frselectf(f, pt0, pt1, F&~D);
       +        }
       +        f->modified = 0;
       +        if(p0 < p1)
       +            f->p0 = p0, f->p1 = p1;
       +        else
       +            f->p0 = p1, f->p1 = p0;
       +        frgetmouse();
       +    }while((m->buttons & 7) == 1);
        }
        /* it is assumed p0<=p1 and both were generated by frptofchar() */
        void
        frselectf(Frame *f, Point p0, Point p1, Fcode c)
        {
       -        int n;
       -        Point q0, q1;
       +    int n;
       +    Point q0, q1;
        
       -        if(p0.x == f->left)
       -                p0.x = f->r.min.x;
       -        if(p1.x == f->left)
       -                p1.x = f->r.min.x;
       -        q0 = p0;
       -        q1 = p1;
       -        q0.y += f->fheight;
       -        q1.y += f->fheight;
       -        n = (p1.y-p0.y)/f->fheight;
       -        if(f->b == 0)
       -                berror("frselectf b==0");
       -        if(p0.y == f->r.max.y)
       -                return;
       -        if(n == 0){
       -                if(p0.x == p1.x)
       -                        if(p0.x == f->r.min.x)
       -                                q1.x++;
       -                        else
       -                                p0.x--;
       -                bitblt2(f->b, p0, f->b, Rpt(p0, q1), c, 0, f->bg);
       -        }else{
       -                if(p0.x >= f->r.max.x)
       -                        p0.x = f->r.max.x-1;
       -                bitblt2(f->b, p0, f->b, Rect(p0.x, p0.y, f->r.max.x, q0.y), c, 0, f->bg);
       -                if(n > 1)
       -                        bitblt2(f->b, Pt(f->r.min.x, q0.y),
       -                                f->b, Rect(f->r.min.x, q0.y, f->r.max.x, p1.y), c, 0, f->bg);
       -                bitblt2(f->b, Pt(f->r.min.x, p1.y),
       -                                f->b, Rect(f->r.min.x, p1.y, q1.x, q1.y), c, 0, f->bg);
       -        }
       +    if(p0.x == f->left)
       +        p0.x = f->r.min.x;
       +    if(p1.x == f->left)
       +        p1.x = f->r.min.x;
       +    q0 = p0;
       +    q1 = p1;
       +    q0.y += f->fheight;
       +    q1.y += f->fheight;
       +    n = (p1.y-p0.y)/f->fheight;
       +    if(f->b == 0)
       +        berror("frselectf b==0");
       +    if(p0.y == f->r.max.y)
       +        return;
       +    if(n == 0){
       +        if(p0.x == p1.x)
       +            if(p0.x == f->r.min.x)
       +                q1.x++;
       +            else
       +                p0.x--;
       +        bitblt2(f->b, p0, f->b, Rpt(p0, q1), c, 0, f->bg);
       +    }else{
       +        if(p0.x >= f->r.max.x)
       +            p0.x = f->r.max.x-1;
       +        bitblt2(f->b, p0, f->b, Rect(p0.x, p0.y, f->r.max.x, q0.y), c, 0, f->bg);
       +        if(n > 1)
       +            bitblt2(f->b, Pt(f->r.min.x, q0.y),
       +                f->b, Rect(f->r.min.x, q0.y, f->r.max.x, p1.y), c, 0, f->bg);
       +        bitblt2(f->b, Pt(f->r.min.x, p1.y),
       +                f->b, Rect(f->r.min.x, p1.y, q1.x, q1.y), c, 0, f->bg);
       +    }
        }
        
        void
        frselectp(Frame *f, Fcode c)
        {
       -        Point pt0, pt1;
       +    Point pt0, pt1;
        
       -        pt0 = frptofchar(f, f->p0);
       -        pt1 = (f->p0==f->p1)? pt0 : frptofchar(f, f->p1);
       -        frselectf(f, pt0, pt1, c);
       +    pt0 = frptofchar(f, f->p0);
       +    pt1 = (f->p0==f->p1)? pt0 : frptofchar(f, f->p1);
       +    frselectf(f, pt0, pt1, c);
        }
 (DIR) diff --git a/libframe/frstr.c b/libframe/frstr.c
       @@ -8,34 +8,34 @@
         * The code here and elsewhere requires that strings not be gcalloc()ed
         */
        
       -#define        CHUNK        16
       -#define        ROUNDUP(n)        ((n+CHUNK)&~(CHUNK-1))
       +#define CHUNK   16
       +#define ROUNDUP(n)  ((n+CHUNK)&~(CHUNK-1))
        
        uchar *
        _frallocstr(unsigned n)
        {
       -        uchar *p;
       +    uchar *p;
        
       -        p = malloc(ROUNDUP(n));
       -        if(p == 0)
       -                berror("out of memory");
       -        return p;
       +    p = malloc(ROUNDUP(n));
       +    if(p == 0)
       +        berror("out of memory");
       +    return p;
        }
        
        void
        _frinsure(Frame *f, int bn, unsigned n)
        {
       -        Frbox *b;
       -        uchar *p;
       +    Frbox *b;
       +    uchar *p;
        
       -        b = &f->box[bn];
       -        if(b->nrune < 0)
       -                berror("_frinsure");
       -        if(ROUNDUP(b->nrune) > n)        /* > guarantees room for terminal NUL */
       -                return;
       -        p = _frallocstr(n);
       -        b = &f->box[bn];
       -        memmove(p, b->a.ptr, NBYTE(b)+1);
       -        free(b->a.ptr);
       -        b->a.ptr = p;
       +    b = &f->box[bn];
       +    if(b->nrune < 0)
       +        berror("_frinsure");
       +    if(ROUNDUP(b->nrune) > n)   /* > guarantees room for terminal NUL */
       +        return;
       +    p = _frallocstr(n);
       +    b = &f->box[bn];
       +    memmove(p, b->a.ptr, NBYTE(b)+1);
       +    free(b->a.ptr);
       +    b->a.ptr = p;
        }
 (DIR) diff --git a/libframe/frutil.c b/libframe/frutil.c
       @@ -7,101 +7,101 @@
        int
        _frcanfit(Frame *f, Point pt, Frbox *b)
        {
       -        int left, w, nr;
       -        uchar *p;
       -        Rune r;
       +    int left, w, nr;
       +    uchar *p;
       +    Rune r;
        
       -        left = f->r.max.x-pt.x;
       -        if(b->nrune < 0)
       -                return b->a.b.minwid <= left;
       -        if(left >= b->wid)
       -                return b->nrune;
       -        for(nr=0,p=b->a.ptr; *p; p+=w,nr++){
       -                r = *p;
       -                if(r < Runeself)
       -                        w = 1;
       -                else
       -                        w = chartorune(&r, (char*)p);
       -                left -= charwidth(f->font, r);
       -                if(left < 0)
       -                        return nr;
       -        }
       -        berror("_frcanfit can't");
       -        return 0;
       +    left = f->r.max.x-pt.x;
       +    if(b->nrune < 0)
       +        return b->a.b.minwid <= left;
       +    if(left >= b->wid)
       +        return b->nrune;
       +    for(nr=0,p=b->a.ptr; *p; p+=w,nr++){
       +        r = *p;
       +        if(r < Runeself)
       +            w = 1;
       +        else
       +            w = chartorune(&r, (char*)p);
       +        left -= charwidth(f->font, r);
       +        if(left < 0)
       +            return nr;
       +    }
       +    berror("_frcanfit can't");
       +    return 0;
        }
        
        void
        _frcklinewrap(Frame *f, Point *p, Frbox *b)
        {
       -        if((b->nrune<0? b->a.b.minwid : b->wid) > f->r.max.x-p->x){
       -                p->x = f->left;
       -                p->y += f->fheight;
       -        }
       +    if((b->nrune<0? b->a.b.minwid : b->wid) > f->r.max.x-p->x){
       +        p->x = f->left;
       +        p->y += f->fheight;
       +    }
        }
        
        void
        _frcklinewrap0(Frame *f, Point *p, Frbox *b)
        {
       -        if(_frcanfit(f, *p, b) == 0){
       -                p->x = f->left;
       -                p->y += f->fheight;
       -        }
       +    if(_frcanfit(f, *p, b) == 0){
       +        p->x = f->left;
       +        p->y += f->fheight;
       +    }
        }
        
        void
        _fradvance(Frame *f, Point *p, Frbox *b)
        {
       -        if(b->nrune<0 && b->a.b.bc=='\n'){
       -                p->x = f->left;
       -                p->y += f->fheight;
       -        }else
       -                p->x += b->wid;
       +    if(b->nrune<0 && b->a.b.bc=='\n'){
       +        p->x = f->left;
       +        p->y += f->fheight;
       +    }else
       +        p->x += b->wid;
        }
        
        int
        _frnewwid(Frame *f, Point pt, Frbox *b)
        {
       -        int c, x;
       +    int c, x;
        
       -        c = f->r.max.x;
       -        x = pt.x;
       -        if(b->nrune >= 0)
       -                return b->wid;
       -        if(b->a.b.bc == '\t'){
       -                if(x+b->a.b.minwid > c)
       -                        x = pt.x = f->left;
       -                x += f->maxtab;
       -                x -= (x-f->left)%f->maxtab;
       -                if(x-pt.x<b->a.b.minwid || x>c)
       -                        x = pt.x+b->a.b.minwid;
       -                b->wid = x-pt.x;
       -        }
       -        return b->wid;
       +    c = f->r.max.x;
       +    x = pt.x;
       +    if(b->nrune >= 0)
       +        return b->wid;
       +    if(b->a.b.bc == '\t'){
       +        if(x+b->a.b.minwid > c)
       +            x = pt.x = f->left;
       +        x += f->maxtab;
       +        x -= (x-f->left)%f->maxtab;
       +        if(x-pt.x<b->a.b.minwid || x>c)
       +            x = pt.x+b->a.b.minwid;
       +        b->wid = x-pt.x;
       +    }
       +    return b->wid;
        }
        
        void
       -_frclean(Frame *f, Point pt, int n0, int n1)        /* look for mergeable boxes */
       +_frclean(Frame *f, Point pt, int n0, int n1)    /* look for mergeable boxes */
        {
       -        Frbox *b;
       -        int nb, c;
       +    Frbox *b;
       +    int nb, c;
        
       -        c = f->r.max.x;
       -        for(nb=n0; nb<n1-1; nb++){
       -                b = &f->box[nb];
       -                _frcklinewrap(f, &pt, b);
       -                while(b[0].nrune>=0 && nb<n1-1 && b[1].nrune>=0 && pt.x+b[0].wid+b[1].wid<c){
       -                        _frmergebox(f, nb);
       -                        n1--;
       -                        b = &f->box[nb];
       -                }
       -                _fradvance(f, &pt, &f->box[nb]);
       -        }
       -        for(; nb<f->nbox; nb++){
       -                b = &f->box[nb];
       -                _frcklinewrap(f, &pt, b);
       -                _fradvance(f, &pt, &f->box[nb]);
       -        }
       -        f->lastlinefull = 0;
       -        if(pt.y >= f->r.max.y)
       -                f->lastlinefull = 1;
       +    c = f->r.max.x;
       +    for(nb=n0; nb<n1-1; nb++){
       +        b = &f->box[nb];
       +        _frcklinewrap(f, &pt, b);
       +        while(b[0].nrune>=0 && nb<n1-1 && b[1].nrune>=0 && pt.x+b[0].wid+b[1].wid<c){
       +            _frmergebox(f, nb);
       +            n1--;
       +            b = &f->box[nb];
       +        }
       +        _fradvance(f, &pt, &f->box[nb]);
       +    }
       +    for(; nb<f->nbox; nb++){
       +        b = &f->box[nb];
       +        _frcklinewrap(f, &pt, b);
       +        _fradvance(f, &pt, &f->box[nb]);
       +    }
       +    f->lastlinefull = 0;
       +    if(pt.y >= f->r.max.y)
       +        f->lastlinefull = 1;
        }
 (DIR) diff --git a/libframe/misc.c b/libframe/misc.c
       @@ -1,47 +1,47 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
       -#include        <u.h>
       -#include        <libc.h>
       -#include        <pwd.h>
       -#ifdef        NEEDVARARG
       -#include        <varargs.h>
       +#include    <u.h>
       +#include    <libc.h>
       +#include    <pwd.h>
       +#ifdef  NEEDVARARG
       +#include    <varargs.h>
        #else
       -#include        <stdarg.h>
       +#include    <stdarg.h>
        #endif
        #include <errno.h>
        
        int errstr(char *buf)
        {
        
       -        strncpy(buf, strerror(errno), ERRLEN);
       -        return 1;
       +    strncpy(buf, strerror(errno), ERRLEN);
       +    return 1;
        }
        
        char*
        getuser(void)
        {
       -        struct passwd *p;
       +    struct passwd *p;
        
       -        static char *user = 0;
       +    static char *user = 0;
        
       -        if (!user) {
       -                p = getpwuid(getuid());
       -                if (p && p->pw_name) {
       -                        user = malloc(strlen(p->pw_name)+1);
       -                        if (user)
       -                                strcpy(user, p->pw_name);
       -                }
       -        }
       -        if(!user)
       -                user = "unknown";
       -        return user;
       +    if (!user) {
       +        p = getpwuid(getuid());
       +        if (p && p->pw_name) {
       +            user = malloc(strlen(p->pw_name)+1);
       +            if (user)
       +                strcpy(user, p->pw_name);
       +        }
       +    }
       +    if(!user)
       +        user = "unknown";
       +    return user;
        }
        
        #ifdef NEEDSTRERROR
        char *
        strerror(int n)
        {
       -        extern char *sys_errlist[];
       -        return sys_errlist[n];
       +    extern char *sys_errlist[];
       +    return sys_errlist[n];
        }
        #endif /* NEEDSTRERROR */
        
       @@ -52,30 +52,30 @@ strerror(int n)
        void*
        memmove(void *a1, const void *a2, size_t n)
        {
       -        char *s1;
       -        const char *s2;
       +    char *s1;
       +    const char *s2;
        
       -        s1 = a1;
       -        s2 = a2;
       -        if(s1 > s2)
       -                goto back;
       -        if(s1 + n <= s2)
       -                return memcpy(a1, a2, n);
       -        while(n > 0) {
       -                *s1++ = *s2++;
       -                n--;
       -        }
       -        return a1;
       +    s1 = a1;
       +    s2 = a2;
       +    if(s1 > s2)
       +        goto back;
       +    if(s1 + n <= s2)
       +        return memcpy(a1, a2, n);
       +    while(n > 0) {
       +        *s1++ = *s2++;
       +        n--;
       +    }
       +    return a1;
        
        back:
       -        s2 += n;
       -        if(s2 <= s1)
       -                return memcpy(a1, a2, n);
       -        s1 += n;
       -        while(n > 0) {
       -                *--s1 = *--s2;
       -                n--;
       -        }
       -        return a1;
       +    s2 += n;
       +    if(s2 <= s1)
       +        return memcpy(a1, a2, n);
       +    s1 += n;
       +    while(n > 0) {
       +        *--s1 = *--s2;
       +        n--;
       +    }
       +    return a1;
        }
        #endif /* NEEDMEMMOVE */
 (DIR) diff --git a/sam/address.c b/sam/address.c
       @@ -2,240 +2,240 @@
        #include "sam.h"
        #include "parse.h"
        
       -Address        addr;
       -String        lastpat;
       -int        patset;
       -File        *menu;
       +Address addr;
       +String  lastpat;
       +int patset;
       +File    *menu;
        
       -File        *matchfile(String*);
       -Address        charaddr(Posn, Address, int);
       +File    *matchfile(String*);
       +Address charaddr(Posn, Address, int);
        
        Address
        address(Addr *ap, Address a, int sign)
        {
       -        File *f = a.f;
       -        Address a1, a2;
       -
       -        do{
       -                switch(ap->type){
       -                case 'l':
       -                case '#':
       -                        a = (*(ap->type=='#'?charaddr:lineaddr))(ap->num, a, sign);
       -                        break;
       -
       -                case '.':
       -                        a = f->dot;
       -                        break;
       -
       -                case '$':
       -                        a.r.p1 = a.r.p2 = f->nrunes;
       -                        break;
       -
       -                case '\'':
       -                        a.r = f->mark;
       -                        break;
       -
       -                case '?':
       -                        sign = -sign;
       -                        if(sign == 0)
       -                                sign = -1;
       -                        /* fall through */
       -                case '/':
       -                        nextmatch(f, ap->are, sign>=0? a.r.p2 : a.r.p1, sign);
       -                        a.r = sel.p[0];
       -                        break;
       -
       -                case '"':
       -                        a = matchfile(ap->are)->dot;
       -                        f = a.f;
       -                        if(f->state == Unread)
       -                                load(f);
       -                        break;
       -
       -                case '*':
       -                        a.r.p1 = 0, a.r.p2 = f->nrunes;
       -                        return a;
       -
       -                case ',':
       -                case ';':
       -                        if(ap->left)
       -                                a1 = address(ap->left, a, 0);
       -                        else
       -                                a1.f = a.f, a1.r.p1 = a1.r.p2 = 0;
       -                        if(ap->type == ';'){
       -                                f = a1.f;
       -                                f->dot = a = a1;
       -                        }
       -                        if(ap->next)
       -                                a2 = address(ap->next, a, 0);
       -                        else
       -                                a2.f = a.f, a2.r.p1 = a2.r.p2 = f->nrunes;
       -                        if(a1.f != a2.f)
       -                                error(Eorder);
       -                        a.f = a1.f, a.r.p1 = a1.r.p1, a.r.p2 = a2.r.p2;
       -                        if(a.r.p2 < a.r.p1)
       -                                error(Eorder);
       -                        return a;
       -
       -                case '+':
       -                case '-':
       -                        sign = 1;
       -                        if(ap->type == '-')
       -                                sign = -1;
       -                        if(ap->next==0 || ap->next->type=='+' || ap->next->type=='-')
       -                                a = lineaddr(1L, a, sign);
       -                        break;
       -                default:
       -                        panic("address");
       -                        return a;
       -                }
       -        }while(ap = ap->next);        /* assign = */
       -        return a;
       +    File *f = a.f;
       +    Address a1, a2;
       +
       +    do{
       +        switch(ap->type){
       +        case 'l':
       +        case '#':
       +            a = (*(ap->type=='#'?charaddr:lineaddr))(ap->num, a, sign);
       +            break;
       +
       +        case '.':
       +            a = f->dot;
       +            break;
       +
       +        case '$':
       +            a.r.p1 = a.r.p2 = f->nrunes;
       +            break;
       +
       +        case '\'':
       +            a.r = f->mark;
       +            break;
       +
       +        case '?':
       +            sign = -sign;
       +            if(sign == 0)
       +                sign = -1;
       +            /* fall through */
       +        case '/':
       +            nextmatch(f, ap->are, sign>=0? a.r.p2 : a.r.p1, sign);
       +            a.r = sel.p[0];
       +            break;
       +
       +        case '"':
       +            a = matchfile(ap->are)->dot;
       +            f = a.f;
       +            if(f->state == Unread)
       +                load(f);
       +            break;
       +
       +        case '*':
       +            a.r.p1 = 0, a.r.p2 = f->nrunes;
       +            return a;
       +
       +        case ',':
       +        case ';':
       +            if(ap->left)
       +                a1 = address(ap->left, a, 0);
       +            else
       +                a1.f = a.f, a1.r.p1 = a1.r.p2 = 0;
       +            if(ap->type == ';'){
       +                f = a1.f;
       +                f->dot = a = a1;
       +            }
       +            if(ap->next)
       +                a2 = address(ap->next, a, 0);
       +            else
       +                a2.f = a.f, a2.r.p1 = a2.r.p2 = f->nrunes;
       +            if(a1.f != a2.f)
       +                error(Eorder);
       +            a.f = a1.f, a.r.p1 = a1.r.p1, a.r.p2 = a2.r.p2;
       +            if(a.r.p2 < a.r.p1)
       +                error(Eorder);
       +            return a;
       +
       +        case '+':
       +        case '-':
       +            sign = 1;
       +            if(ap->type == '-')
       +                sign = -1;
       +            if(ap->next==0 || ap->next->type=='+' || ap->next->type=='-')
       +                a = lineaddr(1L, a, sign);
       +            break;
       +        default:
       +            panic("address");
       +            return a;
       +        }
       +    }while(ap = ap->next);  /* assign = */
       +    return a;
        }
        
        void
        nextmatch(File *f, String *r, Posn p, int sign)
        {
       -        compile(r);
       -        if(sign >= 0){
       -                if(!execute(f, p, INFINITY))
       -                        error(Esearch);
       -                if(sel.p[0].p1==sel.p[0].p2 && sel.p[0].p1==p){
       -                        if(++p>f->nrunes)
       -                                p = 0;
       -                        if(!execute(f, p, INFINITY))
       -                                panic("address");
       -                }
       -        }else{
       -                if(!bexecute(f, p))
       -                        error(Esearch);
       -                if(sel.p[0].p1==sel.p[0].p2 && sel.p[0].p2==p){
       -                        if(--p<0)
       -                                p = f->nrunes;
       -                        if(!bexecute(f, p))
       -                                panic("address");
       -                }
       -        }
       +    compile(r);
       +    if(sign >= 0){
       +        if(!execute(f, p, INFINITY))
       +            error(Esearch);
       +        if(sel.p[0].p1==sel.p[0].p2 && sel.p[0].p1==p){
       +            if(++p>f->nrunes)
       +                p = 0;
       +            if(!execute(f, p, INFINITY))
       +                panic("address");
       +        }
       +    }else{
       +        if(!bexecute(f, p))
       +            error(Esearch);
       +        if(sel.p[0].p1==sel.p[0].p2 && sel.p[0].p2==p){
       +            if(--p<0)
       +                p = f->nrunes;
       +            if(!bexecute(f, p))
       +                panic("address");
       +        }
       +    }
        }
        
        File *
        matchfile(String *r)
        {
       -        File *f;
       -        File *match = 0;
       -        int i;
       -
       -        for(i = 0; i<file.nused; i++){
       -                f = file.filepptr[i];
       -                if(f == cmd)
       -                        continue;
       -                if(filematch(f, r)){
       -                        if(match)
       -                                error(Emanyfiles);
       -                        match = f;
       -                }
       -        }
       -        if(!match)
       -                error(Efsearch);
       -        return match;
       +    File *f;
       +    File *match = 0;
       +    int i;
       +
       +    for(i = 0; i<file.nused; i++){
       +        f = file.filepptr[i];
       +        if(f == cmd)
       +            continue;
       +        if(filematch(f, r)){
       +            if(match)
       +                error(Emanyfiles);
       +            match = f;
       +        }
       +    }
       +    if(!match)
       +        error(Efsearch);
       +    return match;
        }
        
        int
        filematch(File *f, String *r)
        {
       -        char *c, buf[STRSIZE+100];
       -        String *t;
       -
       -        c = Strtoc(&f->name);
       -        sprint(buf, "%c%c%c %s\n", " '"[f->state==Dirty],
       -                "-+"[f->rasp!=0], " ."[f==curfile], c);
       -        free(c);
       -        t = tmpcstr(buf);
       -        Strduplstr(&genstr, t);
       -        freetmpstr(t);
       -        /* A little dirty... */
       -        if(menu == 0)
       -                (menu=Fopen())->state=Clean;
       -        Bdelete(menu->buf, 0, menu->buf->nrunes);
       -        Binsert(menu->buf, &genstr, 0);
       -        menu->nrunes = menu->buf->nrunes;
       -        compile(r);
       -        return execute(menu, 0, menu->nrunes);
       +    char *c, buf[STRSIZE+100];
       +    String *t;
       +
       +    c = Strtoc(&f->name);
       +    sprint(buf, "%c%c%c %s\n", " '"[f->state==Dirty],
       +        "-+"[f->rasp!=0], " ."[f==curfile], c);
       +    free(c);
       +    t = tmpcstr(buf);
       +    Strduplstr(&genstr, t);
       +    freetmpstr(t);
       +    /* A little dirty... */
       +    if(menu == 0)
       +        (menu=Fopen())->state=Clean;
       +    Bdelete(menu->buf, 0, menu->buf->nrunes);
       +    Binsert(menu->buf, &genstr, 0);
       +    menu->nrunes = menu->buf->nrunes;
       +    compile(r);
       +    return execute(menu, 0, menu->nrunes);
        }
        
        Address
        charaddr(Posn l, Address addr, int sign)
        {
       -        if(sign == 0)
       -                addr.r.p1 = addr.r.p2 = l;
       -        else if(sign < 0)
       -                addr.r.p2 = addr.r.p1-=l;
       -        else if(sign > 0)
       -                addr.r.p1 = addr.r.p2+=l;
       -        if(addr.r.p1<0 || addr.r.p2>addr.f->nrunes)
       -                error(Erange);
       -        return addr;
       +    if(sign == 0)
       +        addr.r.p1 = addr.r.p2 = l;
       +    else if(sign < 0)
       +        addr.r.p2 = addr.r.p1-=l;
       +    else if(sign > 0)
       +        addr.r.p1 = addr.r.p2+=l;
       +    if(addr.r.p1<0 || addr.r.p2>addr.f->nrunes)
       +        error(Erange);
       +    return addr;
        }
        
        Address
        lineaddr(Posn l, Address addr, int sign)
        {
       -        int n;
       -        int c;
       -        File *f = addr.f;
       -        Address a;
       -
       -        SET(c);
       -        a.f = f;
       -        if(sign >= 0){
       -                if(l == 0){
       -                        if(sign==0 || addr.r.p2==0){
       -                                a.r.p1 = a.r.p2 = 0;
       -                                return a;
       -                        }
       -                        a.r.p1 = addr.r.p2;
       -                        Fgetcset(f, addr.r.p2-1);
       -                }else{
       -                        if(sign==0 || addr.r.p2==0){
       -                                Fgetcset(f, (Posn)0);
       -                                n = 1;
       -                        }else{
       -                                Fgetcset(f, addr.r.p2-1);
       -                                n = Fgetc(f)=='\n';
       -                        }
       -                        for(; n<l; ){
       -                                c = Fgetc(f);
       -                                if(c == -1)
       -                                        error(Erange);
       -                                else if(c == '\n')
       -                                        n++;
       -                        }
       -                        a.r.p1 = f->getcp;
       -                }
       -                do; while((c=Fgetc(f))!='\n' && c!=-1);
       -                a.r.p2 = f->getcp;
       -        }else{
       -                Fbgetcset(f, addr.r.p1);
       -                if(l == 0)
       -                        a.r.p2 = addr.r.p1;
       -                else{
       -                        for(n = 0; n<l; ){        /* always runs once */
       -                                c = Fbgetc(f);
       -                                if(c == '\n')
       -                                        n++;
       -                                else if(c == -1){
       -                                        if(++n != l)
       -                                                error(Erange);
       -                                }
       -                        }
       -                        a.r.p2 = f->getcp;
       -                        if(c == '\n')
       -                                a.r.p2++;        /* lines start after a newline */
       -                }
       -                do; while((c=Fbgetc(f))!='\n' && c!=-1);
       -                a.r.p1 = f->getcp;
       -                if(c == '\n')
       -                        a.r.p1++;        /* lines start after a newline */
       -        }
       -        return a;
       +    int n;
       +    int c;
       +    File *f = addr.f;
       +    Address a;
       +
       +    SET(c);
       +    a.f = f;
       +    if(sign >= 0){
       +        if(l == 0){
       +            if(sign==0 || addr.r.p2==0){
       +                a.r.p1 = a.r.p2 = 0;
       +                return a;
       +            }
       +            a.r.p1 = addr.r.p2;
       +            Fgetcset(f, addr.r.p2-1);
       +        }else{
       +            if(sign==0 || addr.r.p2==0){
       +                Fgetcset(f, (Posn)0);
       +                n = 1;
       +            }else{
       +                Fgetcset(f, addr.r.p2-1);
       +                n = Fgetc(f)=='\n';
       +            }
       +            for(; n<l; ){
       +                c = Fgetc(f);
       +                if(c == -1)
       +                    error(Erange);
       +                else if(c == '\n')
       +                    n++;
       +            }
       +            a.r.p1 = f->getcp;
       +        }
       +        do; while((c=Fgetc(f))!='\n' && c!=-1);
       +        a.r.p2 = f->getcp;
       +    }else{
       +        Fbgetcset(f, addr.r.p1);
       +        if(l == 0)
       +            a.r.p2 = addr.r.p1;
       +        else{
       +            for(n = 0; n<l; ){  /* always runs once */
       +                c = Fbgetc(f);
       +                if(c == '\n')
       +                    n++;
       +                else if(c == -1){
       +                    if(++n != l)
       +                        error(Erange);
       +                }
       +            }
       +            a.r.p2 = f->getcp;
       +            if(c == '\n')
       +                a.r.p2++;   /* lines start after a newline */
       +        }
       +        do; while((c=Fbgetc(f))!='\n' && c!=-1);
       +        a.r.p1 = f->getcp;
       +        if(c == '\n')
       +            a.r.p1++;   /* lines start after a newline */
       +    }
       +    return a;
        }
 (DIR) diff --git a/sam/buffer.c b/sam/buffer.c
       @@ -1,172 +1,172 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
        #include "sam.h"
        
       -int        incache(Buffer*, Posn, Posn);
       +int incache(Buffer*, Posn, Posn);
        
        Buffer *
        Bopen(Discdesc *dd)
        {
       -        Buffer *b;
       +    Buffer *b;
        
       -        b = emalloc(sizeof(Buffer));
       -        b->disc = Dopen(dd);
       -        Strinit(&b->cache);
       -        return b;
       +    b = emalloc(sizeof(Buffer));
       +    b->disc = Dopen(dd);
       +    Strinit(&b->cache);
       +    return b;
        }
        
        void
        Bterm(Buffer *b)
        {
       -        Dclose(b->disc);
       -        Strclose(&b->cache);
       -        free(b);
       +    Dclose(b->disc);
       +    Strclose(&b->cache);
       +    free(b);
        }
        
        int
        Bread(Buffer *b, Rune *addr, int n, Posn p0)
        {
       -        int m;
       +    int m;
        
       -        if(b->c2>b->disc->nrunes || b->c1>b->disc->nrunes)
       -                panic("bread cache");
       -        if(p0 < 0)
       -                panic("Bread p0<0");
       -        if(p0+n > b->nrunes){
       -                n = b->nrunes-p0;
       -                if(n < 0)
       -                        panic("Bread<0");
       -        }
       -        if(!incache(b, p0, p0+n)){
       -                Bflush(b);
       -                if(n>=BLOCKSIZE/2)
       -                        return Dread(b->disc, addr, n, p0);
       -                else{
       -                        Posn minp;
       -                        if(b->nrunes-p0>BLOCKSIZE/2)
       -                                m = BLOCKSIZE/2;
       -                        else
       -                                m = b->nrunes-p0;
       -                        if(m<n)
       -                                m = n;
       -                        minp = p0-BLOCKSIZE/2;
       -                        if(minp<0)
       -                                minp = 0;
       -                        m += p0-minp;
       -                        Strinsure(&b->cache, m);
       -                        if(Dread(b->disc, b->cache.s, m, minp)!=m)
       -                                panic("Bread");
       -                        b->cache.n = m;
       -                        b->c1 = minp;
       -                        b->c2 = minp+m;
       -                        b->dirty = FALSE;
       -                }
       -        }
       -        memmove(addr, &b->cache.s[p0-b->c1], n*RUNESIZE);
       -        return n;
       +    if(b->c2>b->disc->nrunes || b->c1>b->disc->nrunes)
       +        panic("bread cache");
       +    if(p0 < 0)
       +        panic("Bread p0<0");
       +    if(p0+n > b->nrunes){
       +        n = b->nrunes-p0;
       +        if(n < 0)
       +            panic("Bread<0");
       +    }
       +    if(!incache(b, p0, p0+n)){
       +        Bflush(b);
       +        if(n>=BLOCKSIZE/2)
       +            return Dread(b->disc, addr, n, p0);
       +        else{
       +            Posn minp;
       +            if(b->nrunes-p0>BLOCKSIZE/2)
       +                m = BLOCKSIZE/2;
       +            else
       +                m = b->nrunes-p0;
       +            if(m<n)
       +                m = n;
       +            minp = p0-BLOCKSIZE/2;
       +            if(minp<0)
       +                minp = 0;
       +            m += p0-minp;
       +            Strinsure(&b->cache, m);
       +            if(Dread(b->disc, b->cache.s, m, minp)!=m)
       +                panic("Bread");
       +            b->cache.n = m;
       +            b->c1 = minp;
       +            b->c2 = minp+m;
       +            b->dirty = FALSE;
       +        }
       +    }
       +    memmove(addr, &b->cache.s[p0-b->c1], n*RUNESIZE);
       +    return n;
        }
        
        void
        Binsert(Buffer *b, String *s, Posn p0)
        {
       -        if(b->c2>b->disc->nrunes || b->c1>b->disc->nrunes)
       -                panic("binsert cache");
       -        if(p0<0)
       -                panic("Binsert p0<0");
       -        if(s->n == 0)
       -                return;
       -        if(incache(b, p0, p0) && b->cache.n+s->n<=STRSIZE){
       -                Strinsert(&b->cache, s, p0-b->c1);
       -                b->dirty = TRUE;
       -                if(b->cache.n > BLOCKSIZE*2){
       -                        b->nrunes += s->n;
       -                        Bflush(b);
       -                        /* try to leave some cache around p0 */
       -                        if(p0 >= b->c1+BLOCKSIZE){
       -                                /* first BLOCKSIZE can go */
       -                                Strdelete(&b->cache, 0, BLOCKSIZE);
       -                                b->c1 += BLOCKSIZE;
       -                        }else if(p0 <= b->c2-BLOCKSIZE){
       -                                /* last BLOCKSIZE can go */
       -                                b->cache.n -= BLOCKSIZE;
       -                                b->c2 -= BLOCKSIZE;
       -                        }else{
       -                                /* too hard; negate the cache and pick up next time */
       -                                Strzero(&b->cache);
       -                                b->c1 = b->c2 = 0;
       -                        }
       -                        return;
       -                }
       -        }else{
       -                Bflush(b);
       -                if(s->n >= BLOCKSIZE/2){
       -                        b->cache.n = 0;
       -                        b->c1 = b->c2 = 0;
       -                        Dinsert(b->disc, s->s, s->n, p0);
       -                }else{
       -                        int m;
       -                        Posn minp;
       -                        if(b->nrunes-p0 > BLOCKSIZE/2)
       -                                m = BLOCKSIZE/2;
       -                        else
       -                                m = b->nrunes-p0;
       -                        minp = p0-BLOCKSIZE/2;
       -                        if(minp < 0)
       -                                minp = 0;
       -                        m += p0-minp;
       -                        Strinsure(&b->cache, m);
       -                        if(Dread(b->disc, b->cache.s, m, minp)!=m)
       -                                panic("Bread");
       -                        b->cache.n = m;
       -                        b->c1 = minp;
       -                        b->c2 = minp+m;
       -                        Strinsert(&b->cache, s, p0-b->c1);
       -                        b->dirty = TRUE;
       -                }
       -        }
       -        b->nrunes += s->n;
       +    if(b->c2>b->disc->nrunes || b->c1>b->disc->nrunes)
       +        panic("binsert cache");
       +    if(p0<0)
       +        panic("Binsert p0<0");
       +    if(s->n == 0)
       +        return;
       +    if(incache(b, p0, p0) && b->cache.n+s->n<=STRSIZE){
       +        Strinsert(&b->cache, s, p0-b->c1);
       +        b->dirty = TRUE;
       +        if(b->cache.n > BLOCKSIZE*2){
       +            b->nrunes += s->n;
       +            Bflush(b);
       +            /* try to leave some cache around p0 */
       +            if(p0 >= b->c1+BLOCKSIZE){
       +                /* first BLOCKSIZE can go */
       +                Strdelete(&b->cache, 0, BLOCKSIZE);
       +                b->c1 += BLOCKSIZE;
       +            }else if(p0 <= b->c2-BLOCKSIZE){
       +                /* last BLOCKSIZE can go */
       +                b->cache.n -= BLOCKSIZE;
       +                b->c2 -= BLOCKSIZE;
       +            }else{
       +                /* too hard; negate the cache and pick up next time */
       +                Strzero(&b->cache);
       +                b->c1 = b->c2 = 0;
       +            }
       +            return;
       +        }
       +    }else{
       +        Bflush(b);
       +        if(s->n >= BLOCKSIZE/2){
       +            b->cache.n = 0;
       +            b->c1 = b->c2 = 0;
       +            Dinsert(b->disc, s->s, s->n, p0);
       +        }else{
       +            int m;
       +            Posn minp;
       +            if(b->nrunes-p0 > BLOCKSIZE/2)
       +                m = BLOCKSIZE/2;
       +            else
       +                m = b->nrunes-p0;
       +            minp = p0-BLOCKSIZE/2;
       +            if(minp < 0)
       +                minp = 0;
       +            m += p0-minp;
       +            Strinsure(&b->cache, m);
       +            if(Dread(b->disc, b->cache.s, m, minp)!=m)
       +                panic("Bread");
       +            b->cache.n = m;
       +            b->c1 = minp;
       +            b->c2 = minp+m;
       +            Strinsert(&b->cache, s, p0-b->c1);
       +            b->dirty = TRUE;
       +        }
       +    }
       +    b->nrunes += s->n;
        }
        
        void
        Bdelete(Buffer *b, Posn p1, Posn p2)
        {
       -        if(p1<0 || p2<0)
       -                panic("Bdelete p<0");
       -        if(b->c2>b->disc->nrunes || b->c1>b->disc->nrunes)
       -                panic("bdelete cache");
       -        if(p1 == p2)
       -                return;
       -        if(incache(b, p1, p2)){
       -                Strdelete(&b->cache, p1-b->c1, p2-b->c1);
       -                b->dirty = TRUE;
       -        }else{
       -                Bflush(b);
       -                Ddelete(b->disc, p1, p2);
       -                b->cache.n = 0;
       -                b->c1 = b->c2 = 0;
       -        }
       -        b->nrunes -= p2-p1;
       +    if(p1<0 || p2<0)
       +        panic("Bdelete p<0");
       +    if(b->c2>b->disc->nrunes || b->c1>b->disc->nrunes)
       +        panic("bdelete cache");
       +    if(p1 == p2)
       +        return;
       +    if(incache(b, p1, p2)){
       +        Strdelete(&b->cache, p1-b->c1, p2-b->c1);
       +        b->dirty = TRUE;
       +    }else{
       +        Bflush(b);
       +        Ddelete(b->disc, p1, p2);
       +        b->cache.n = 0;
       +        b->c1 = b->c2 = 0;
       +    }
       +    b->nrunes -= p2-p1;
        }
        
        void
        Bflush(Buffer *b)
        {
       -        if(b->dirty){
       -                Dreplace(b->disc, b->c1, b->c2, b->cache.s, b->cache.n);
       -                b->c2 = b->c1+b->cache.n;
       -                b->dirty = FALSE;
       -                if(b->nrunes != b->disc->nrunes)
       -                        panic("Bflush");
       -        }
       +    if(b->dirty){
       +        Dreplace(b->disc, b->c1, b->c2, b->cache.s, b->cache.n);
       +        b->c2 = b->c1+b->cache.n;
       +        b->dirty = FALSE;
       +        if(b->nrunes != b->disc->nrunes)
       +            panic("Bflush");
       +    }
        }
        
        void
        Bclean(Buffer *b)
        {
       -        if(b->dirty){
       -                Bflush(b);
       -                b->c1 = b->c2 = 0;
       -                Strzero(&b->cache);
       -        }
       +    if(b->dirty){
       +        Bflush(b);
       +        b->c1 = b->c2 = 0;
       +        Strzero(&b->cache);
       +    }
        }
        
        /*int hits, misses; /**/
       @@ -174,6 +174,6 @@ Bclean(Buffer *b)
        int
        incache(Buffer *b, Posn p1, Posn p2)
        {
       -        /*if(b->c1<=p1 && p2<=b->c1+b->cache.n)hits++; else misses++;/**/
       -        return b->c1<=p1 && p2<=b->c1+b->cache.n;
       +    /*if(b->c1<=p1 && p2<=b->c1+b->cache.n)hits++; else misses++;/**/
       +    return b->c1<=p1 && p2<=b->c1+b->cache.n;
        }
 (DIR) diff --git a/sam/cmd.c b/sam/cmd.c
       @@ -2,590 +2,590 @@
        #include "sam.h"
        #include "parse.h"
        
       -static char        linex[]="\n";
       -static char        wordx[]=" \t\n";
       +static char linex[]="\n";
       +static char wordx[]=" \t\n";
        struct cmdtab cmdtab[]={
       -/*        cmdc        text        regexp        addr        defcmd        defaddr        count        token         fn        */
       -        '\n',        0,        0,        0,        0,        aDot,        0,        0,        nl_cmd,
       -        'a',        1,        0,        0,        0,        aDot,        0,        0,        a_cmd,
       -        'b',        0,        0,        0,        0,        aNo,        0,        linex,        b_cmd,
       -        'B',        0,        0,        0,        0,        aNo,        0,        linex,        b_cmd,
       -        'c',        1,        0,        0,        0,        aDot,        0,        0,        c_cmd,
       -        'd',        0,        0,        0,        0,        aDot,        0,        0,        d_cmd,
       -        'D',        0,        0,        0,        0,        aNo,        0,        linex,        D_cmd,
       -        'e',        0,        0,        0,        0,        aNo,        0,        wordx,        e_cmd,
       -        'f',        0,        0,        0,        0,        aNo,        0,        wordx,        f_cmd,
       -        'g',        0,        1,        0,        'p',        aDot,        0,        0,        g_cmd,
       -        'i',        1,        0,        0,        0,        aDot,        0,        0,        i_cmd,
       -        'k',        0,        0,        0,        0,        aDot,        0,        0,        k_cmd,
       -        'm',        0,        0,        1,        0,        aDot,        0,        0,        m_cmd,
       -        'n',        0,        0,        0,        0,        aNo,        0,        0,        n_cmd,
       -        'p',        0,        0,        0,        0,        aDot,        0,        0,        p_cmd,
       -        'q',        0,        0,        0,        0,        aNo,        0,        0,        q_cmd,
       -        'r',        0,        0,        0,        0,        aDot,        0,        wordx,        e_cmd,
       -        's',        0,        1,        0,        0,        aDot,        1,        0,        s_cmd,
       -        't',        0,        0,        1,        0,        aDot,        0,        0,        m_cmd,
       -        'u',        0,        0,        0,        0,        aNo,        1,        0,        u_cmd,
       -        'v',        0,        1,        0,        'p',        aDot,        0,        0,        g_cmd,
       -        'w',        0,        0,        0,        0,        aAll,        0,        wordx,        w_cmd,
       -        'x',        0,        1,        0,        'p',        aDot,        0,        0,        x_cmd,
       -        'y',        0,        1,        0,        'p',        aDot,        0,        0,        x_cmd,
       -        'X',        0,        1,        0,        'f',        aNo,        0,        0,        X_cmd,
       -        'Y',        0,        1,        0,        'f',        aNo,        0,        0,        X_cmd,
       -        '!',        0,        0,        0,        0,        aNo,        0,        linex,        plan9_cmd,
       -        '>',        0,        0,        0,        0,        aDot,        0,        linex,        plan9_cmd,
       -        '<',        0,        0,        0,        0,        aDot,        0,        linex,        plan9_cmd,
       -        '|',        0,        0,        0,        0,        aDot,        0,        linex,        plan9_cmd,
       -        '=',        0,        0,        0,        0,        aDot,        0,        linex,        eq_cmd,
       -        'c'|0x100,0,        0,        0,        0,        aNo,        0,        wordx,        cd_cmd,
       -        0,        0,        0,        0,        0,        0,        0,        0,
       +/*  cmdc    text    regexp  addr    defcmd  defaddr count   token    fn */
       +    '\n',   0,  0,  0,  0,  aDot,   0,  0,  nl_cmd,
       +    'a',    1,  0,  0,  0,  aDot,   0,  0,  a_cmd,
       +    'b',    0,  0,  0,  0,  aNo,    0,  linex,  b_cmd,
       +    'B',    0,  0,  0,  0,  aNo,    0,  linex,  b_cmd,
       +    'c',    1,  0,  0,  0,  aDot,   0,  0,  c_cmd,
       +    'd',    0,  0,  0,  0,  aDot,   0,  0,  d_cmd,
       +    'D',    0,  0,  0,  0,  aNo,    0,  linex,  D_cmd,
       +    'e',    0,  0,  0,  0,  aNo,    0,  wordx,  e_cmd,
       +    'f',    0,  0,  0,  0,  aNo,    0,  wordx,  f_cmd,
       +    'g',    0,  1,  0,  'p',    aDot,   0,  0,  g_cmd,
       +    'i',    1,  0,  0,  0,  aDot,   0,  0,  i_cmd,
       +    'k',    0,  0,  0,  0,  aDot,   0,  0,  k_cmd,
       +    'm',    0,  0,  1,  0,  aDot,   0,  0,  m_cmd,
       +    'n',    0,  0,  0,  0,  aNo,    0,  0,  n_cmd,
       +    'p',    0,  0,  0,  0,  aDot,   0,  0,  p_cmd,
       +    'q',    0,  0,  0,  0,  aNo,    0,  0,  q_cmd,
       +    'r',    0,  0,  0,  0,  aDot,   0,  wordx,  e_cmd,
       +    's',    0,  1,  0,  0,  aDot,   1,  0,  s_cmd,
       +    't',    0,  0,  1,  0,  aDot,   0,  0,  m_cmd,
       +    'u',    0,  0,  0,  0,  aNo,    1,  0,  u_cmd,
       +    'v',    0,  1,  0,  'p',    aDot,   0,  0,  g_cmd,
       +    'w',    0,  0,  0,  0,  aAll,   0,  wordx,  w_cmd,
       +    'x',    0,  1,  0,  'p',    aDot,   0,  0,  x_cmd,
       +    'y',    0,  1,  0,  'p',    aDot,   0,  0,  x_cmd,
       +    'X',    0,  1,  0,  'f',    aNo,    0,  0,  X_cmd,
       +    'Y',    0,  1,  0,  'f',    aNo,    0,  0,  X_cmd,
       +    '!',    0,  0,  0,  0,  aNo,    0,  linex,  plan9_cmd,
       +    '>',    0,  0,  0,  0,  aDot,   0,  linex,  plan9_cmd,
       +    '<',    0,  0,  0,  0,  aDot,   0,  linex,  plan9_cmd,
       +    '|',    0,  0,  0,  0,  aDot,   0,  linex,  plan9_cmd,
       +    '=',    0,  0,  0,  0,  aDot,   0,  linex,  eq_cmd,
       +    'c'|0x100,0,    0,  0,  0,  aNo,    0,  wordx,  cd_cmd,
       +    0,  0,  0,  0,  0,  0,  0,  0,
        };
       -Cmd        *parsecmd(int);
       -Addr        *compoundaddr(void);
       -Addr        *simpleaddr(void);
       -void        freecmd(void);
       -void        okdelim(int);
       -
       -Rune        line[BLOCKSIZE];
       -Rune        termline[BLOCKSIZE];
       -Rune        *linep = line;
       -Rune        *terminp = termline;
       -Rune        *termoutp = termline;
       -List        cmdlist;
       -List        addrlist;
       -List        relist;
       -List        stringlist;
       -int        eof;
       +Cmd *parsecmd(int);
       +Addr    *compoundaddr(void);
       +Addr    *simpleaddr(void);
       +void    freecmd(void);
       +void    okdelim(int);
       +
       +Rune    line[BLOCKSIZE];
       +Rune    termline[BLOCKSIZE];
       +Rune    *linep = line;
       +Rune    *terminp = termline;
       +Rune    *termoutp = termline;
       +List    cmdlist;
       +List    addrlist;
       +List    relist;
       +List    stringlist;
       +int eof;
        
        void
        resetcmd(void)
        {
       -        linep = line;
       -        *linep = 0;
       -        terminp = termoutp = termline;
       -        freecmd();
       +    linep = line;
       +    *linep = 0;
       +    terminp = termoutp = termline;
       +    freecmd();
        }
        
        int
        inputc(void)
        {
       -        int n, nbuf;
       -        char buf[3];
       -        Rune r;
       +    int n, nbuf;
       +    char buf[3];
       +    Rune r;
        
            Again:
       -        nbuf = 0;
       -        if(downloaded){
       -                while(termoutp == terminp){
       -                        cmdupdate();
       -                        if(patset)
       -                                tellpat();
       -                        while(termlocked > 0){
       -                                outT0(Hunlock);
       -                                termlocked--;
       -                        }
       -                        if(rcv() == 0)
       -                                return -1;
       -                }
       -                r = *termoutp++;
       -                if(termoutp == terminp)
       -                        terminp = termoutp = termline;
       -        }else{
       -                   do{
       -                        n = read(0, buf+nbuf, 1);
       -                        if(n <= 0)
       -                                return -1;
       -                        nbuf += n;
       -                }while(!fullrune(buf, nbuf));
       -                chartorune(&r, buf);
       -        }
       -        if(r == 0){
       -                warn(Wnulls);
       -                goto Again;
       -        }
       -        return r;
       +    nbuf = 0;
       +    if(downloaded){
       +        while(termoutp == terminp){
       +            cmdupdate();
       +            if(patset)
       +                tellpat();
       +            while(termlocked > 0){
       +                outT0(Hunlock);
       +                termlocked--;
       +            }
       +            if(rcv() == 0)
       +                return -1;
       +        }
       +        r = *termoutp++;
       +        if(termoutp == terminp)
       +            terminp = termoutp = termline;
       +    }else{
       +        do{
       +            n = read(0, buf+nbuf, 1);
       +            if(n <= 0)
       +                return -1;
       +            nbuf += n;
       +        }while(!fullrune(buf, nbuf));
       +        chartorune(&r, buf);
       +    }
       +    if(r == 0){
       +        warn(Wnulls);
       +        goto Again;
       +    }
       +    return r;
        }
        
        int
        inputline(void)
        {
       -        int i, c;
       -
       -        linep = line;
       -        i = 0;
       -        do{
       -                if((c = inputc())<=0)
       -                        return -1;
       -                if(i == (sizeof line)/RUNESIZE-1)
       -                        error(Etoolong);
       -        }while((line[i++]=c) != '\n');
       -        line[i] = 0;
       -        return 1;
       +    int i, c;
       +
       +    linep = line;
       +    i = 0;
       +    do{
       +        if((c = inputc())<=0)
       +            return -1;
       +        if(i == (sizeof line)/RUNESIZE-1)
       +            error(Etoolong);
       +    }while((line[i++]=c) != '\n');
       +    line[i] = 0;
       +    return 1;
        }
        
        int
        getch(void)
        {
       -        if(eof)
       -                return -1;
       -        if(*linep==0 && inputline()<0){
       -                eof = TRUE;
       -                return -1;
       -        }
       -        return *linep++;
       +    if(eof)
       +        return -1;
       +    if(*linep==0 && inputline()<0){
       +        eof = TRUE;
       +        return -1;
       +    }
       +    return *linep++;
        }
        
        int
        nextc(void)
        {
       -        if(*linep == 0)
       -                return -1;
       -        return *linep;
       +    if(*linep == 0)
       +        return -1;
       +    return *linep;
        }
        
        void
        ungetch(void)
        {
       -        if(--linep < line)
       -                panic("ungetch");
       +    if(--linep < line)
       +        panic("ungetch");
        }
        
        Posn
        getnum(void)
        {
       -        Posn n=0;
       -        int c;
       -
       -        if((c=nextc())<'0' || '9'<c)        /* no number defaults to 1 */
       -                return 1;
       -        while('0'<=(c=getch()) && c<='9')
       -                n = n*10 + (c-'0');
       -        ungetch();
       -        return n;
       +    Posn n=0;
       +    int c;
       +
       +    if((c=nextc())<'0' || '9'<c)    /* no number defaults to 1 */
       +        return 1;
       +    while('0'<=(c=getch()) && c<='9')
       +        n = n*10 + (c-'0');
       +    ungetch();
       +    return n;
        }
        
        int
        skipbl(void)
        {
       -        int c;
       -        do
       -                c = getch();
       -        while(c==' ' || c=='\t');
       -        if(c >= 0)
       -                ungetch();
       -        return c;
       +    int c;
       +    do
       +        c = getch();
       +    while(c==' ' || c=='\t');
       +    if(c >= 0)
       +        ungetch();
       +    return c;
        }
        
        void
        termcommand(void)
        {
       -        Posn p;
       -
       -        Fgetcset(cmd, cmdpt);
       -        for(p=cmdpt; p<cmd->nrunes; p++){
       -                if(terminp >= &termline[BLOCKSIZE]){
       -                        cmdpt = cmd->nrunes;
       -                        error(Etoolong);
       -                }
       -                *terminp++ = Fgetc(cmd);
       -        }
       -        cmdpt = cmd->nrunes;
       +    Posn p;
       +
       +    Fgetcset(cmd, cmdpt);
       +    for(p=cmdpt; p<cmd->nrunes; p++){
       +        if(terminp >= &termline[BLOCKSIZE]){
       +            cmdpt = cmd->nrunes;
       +            error(Etoolong);
       +        }
       +        *terminp++ = Fgetc(cmd);
       +    }
       +    cmdpt = cmd->nrunes;
        }
        
        void
        cmdloop(void)
        {
       -        Cmd *cmdp;
       -        File *ocurfile;
       -        int loaded;
       -
       -        for(;;){
       -                if(!downloaded && curfile && curfile->state==Unread)
       -                        load(curfile);
       -                if((cmdp = parsecmd(0))==0){
       -                        if(downloaded){
       -                                rescue();
       -                                exits("eof");
       -                        }
       -                        break;
       -                }
       -                ocurfile = curfile;
       -                loaded = curfile && curfile->state!=Unread;
       -                if(cmdexec(curfile, cmdp) == 0)
       -                        break;
       -                freecmd();
       -                cmdupdate();
       -                update();
       -                if(downloaded && curfile &&
       -                    (ocurfile!=curfile || (!loaded && curfile->state!=Unread)))
       -                        outTs(Hcurrent, curfile->tag);
       -                        /* don't allow type ahead on files that aren't bound */
       -                if(downloaded && curfile && curfile->rasp == 0)
       -                        terminp = termoutp;
       -        }
       +    Cmd *cmdp;
       +    File *ocurfile;
       +    int loaded;
       +
       +    for(;;){
       +        if(!downloaded && curfile && curfile->state==Unread)
       +            load(curfile);
       +        if((cmdp = parsecmd(0))==0){
       +            if(downloaded){
       +                rescue();
       +                exits("eof");
       +            }
       +            break;
       +        }
       +        ocurfile = curfile;
       +        loaded = curfile && curfile->state!=Unread;
       +        if(cmdexec(curfile, cmdp) == 0)
       +            break;
       +        freecmd();
       +        cmdupdate();
       +        update();
       +        if(downloaded && curfile &&
       +            (ocurfile!=curfile || (!loaded && curfile->state!=Unread)))
       +            outTs(Hcurrent, curfile->tag);
       +            /* don't allow type ahead on files that aren't bound */
       +        if(downloaded && curfile && curfile->rasp == 0)
       +            terminp = termoutp;
       +    }
        }
        
        Cmd *
        newcmd(void){
       -        Cmd *p;
       +    Cmd *p;
        
       -        p = emalloc(sizeof(Cmd));
       -        inslist(&cmdlist, cmdlist.nused, (long)p);
       -        return p;
       +    p = emalloc(sizeof(Cmd));
       +    inslist(&cmdlist, cmdlist.nused, (long)p);
       +    return p;
        }
        
        Addr*
        newaddr(void)
        {
       -        Addr *p;
       +    Addr *p;
        
       -        p = emalloc(sizeof(Addr));
       -        inslist(&addrlist, addrlist.nused, (long)p);
       -        return p;
       +    p = emalloc(sizeof(Addr));
       +    inslist(&addrlist, addrlist.nused, (long)p);
       +    return p;
        }
        
        String*
        newre(void)
        {
       -        String *p;
       +    String *p;
        
       -        p = emalloc(sizeof(String));
       -        inslist(&relist, relist.nused, (long)p);
       -        Strinit(p);
       -        return p;
       +    p = emalloc(sizeof(String));
       +    inslist(&relist, relist.nused, (long)p);
       +    Strinit(p);
       +    return p;
        }
        
        String*
        newstring(void)
        {
       -        String *p;
       +    String *p;
        
       -        p = emalloc(sizeof(String));
       -        inslist(&stringlist, stringlist.nused, (long)p);
       -        Strinit(p);
       -        return p;
       +    p = emalloc(sizeof(String));
       +    inslist(&stringlist, stringlist.nused, (long)p);
       +    Strinit(p);
       +    return p;
        }
        
        void
        freecmd(void)
        {
       -        int i;
       -
       -        while(cmdlist.nused > 0)
       -                free(cmdlist.ucharpptr[--cmdlist.nused]);
       -        while(addrlist.nused > 0)
       -                free(addrlist.ucharpptr[--addrlist.nused]);
       -        while(relist.nused > 0){
       -                i = --relist.nused;
       -                Strclose(relist.stringpptr[i]);
       -                free(relist.stringpptr[i]);
       -        }
       -        while(stringlist.nused>0){
       -                i = --stringlist.nused;
       -                Strclose(stringlist.stringpptr[i]);
       -                free(stringlist.stringpptr[i]);
       -        }
       +    int i;
       +
       +    while(cmdlist.nused > 0)
       +        free(cmdlist.ucharpptr[--cmdlist.nused]);
       +    while(addrlist.nused > 0)
       +        free(addrlist.ucharpptr[--addrlist.nused]);
       +    while(relist.nused > 0){
       +        i = --relist.nused;
       +        Strclose(relist.stringpptr[i]);
       +        free(relist.stringpptr[i]);
       +    }
       +    while(stringlist.nused>0){
       +        i = --stringlist.nused;
       +        Strclose(stringlist.stringpptr[i]);
       +        free(stringlist.stringpptr[i]);
       +    }
        }
        
        int
        lookup(int c)
        {
       -        int i;
       +    int i;
        
       -        for(i=0; cmdtab[i].cmdc; i++)
       -                if(cmdtab[i].cmdc == c)
       -                        return i;
       -        return -1;
       +    for(i=0; cmdtab[i].cmdc; i++)
       +        if(cmdtab[i].cmdc == c)
       +            return i;
       +    return -1;
        }
        
        void
        okdelim(int c)
        {
       -        if(c=='\\' || ('a'<=c && c<='z')
       -        || ('A'<=c && c<='Z') || ('0'<=c && c<='9'))
       -                error_c(Edelim, c);
       +    if(c=='\\' || ('a'<=c && c<='z')
       +    || ('A'<=c && c<='Z') || ('0'<=c && c<='9'))
       +        error_c(Edelim, c);
        }
        
        void
        atnl(void)
        {
       -        skipbl();
       -        if(getch() != '\n')
       -                error(Enewline);
       +    skipbl();
       +    if(getch() != '\n')
       +        error(Enewline);
        }
        
        void
        getrhs(String *s, int delim, int cmd)
        {
       -        int c;
       -
       -        while((c = getch())>0 && c!=delim && c!='\n'){
       -                if(c == '\\'){
       -                        if((c=getch()) <= 0)
       -                                error(Ebadrhs);
       -                        if(c == '\n'){
       -                                ungetch();
       -                                c='\\';
       -                        }else if(c == 'n')
       -                                c='\n';
       -                        else if(c!=delim && (cmd=='s' || c!='\\'))        /* s does its own */
       -                                Straddc(s, '\\');
       -                }
       -                Straddc(s, c);
       -        }
       -        ungetch();        /* let client read whether delimeter, '\n' or whatever */
       +    int c;
       +
       +    while((c = getch())>0 && c!=delim && c!='\n'){
       +        if(c == '\\'){
       +            if((c=getch()) <= 0)
       +                error(Ebadrhs);
       +            if(c == '\n'){
       +                ungetch();
       +                c='\\';
       +            }else if(c == 'n')
       +                c='\n';
       +            else if(c!=delim && (cmd=='s' || c!='\\'))  /* s does its own */
       +                Straddc(s, '\\');
       +        }
       +        Straddc(s, c);
       +    }
       +    ungetch();  /* let client read whether delimeter, '\n' or whatever */
        }
        
        String *
        collecttoken(char *end)
        {
       -        String *s = newstring();
       -        int c;
       -
       -        while((c=nextc())==' ' || c=='\t')
       -                Straddc(s, getch()); /* blanks significant for getname() */
       -        while((c=getch())>0 && utfrune(end, c)==0)
       -                Straddc(s, c);
       -        Straddc(s, 0);
       -        if(c != '\n')
       -                atnl();
       -        return s;
       +    String *s = newstring();
       +    int c;
       +
       +    while((c=nextc())==' ' || c=='\t')
       +        Straddc(s, getch()); /* blanks significant for getname() */
       +    while((c=getch())>0 && utfrune(end, c)==0)
       +        Straddc(s, c);
       +    Straddc(s, 0);
       +    if(c != '\n')
       +        atnl();
       +    return s;
        }
        
        String *
        collecttext(void)
        {
       -        String *s = newstring();
       -        int begline, i, c, delim;
       -
       -        if(skipbl()=='\n'){
       -                getch();
       -                i = 0;
       -                do{
       -                        begline = i;
       -                        while((c = getch())>0 && c!='\n')
       -                                i++, Straddc(s, c);
       -                        i++, Straddc(s, '\n');
       -                        if(c < 0)
       -                                goto Return;
       -                }while(s->s[begline]!='.' || s->s[begline+1]!='\n');
       -                Strdelete(s, s->n-2, s->n);
       -        }else{
       -                okdelim(delim = getch());
       -                getrhs(s, delim, 'a');
       -                if(nextc()==delim)
       -                        getch();
       -                atnl();
       -        }
       +    String *s = newstring();
       +    int begline, i, c, delim;
       +
       +    if(skipbl()=='\n'){
       +        getch();
       +        i = 0;
       +        do{
       +            begline = i;
       +            while((c = getch())>0 && c!='\n')
       +                i++, Straddc(s, c);
       +            i++, Straddc(s, '\n');
       +            if(c < 0)
       +                goto Return;
       +        }while(s->s[begline]!='.' || s->s[begline+1]!='\n');
       +        Strdelete(s, s->n-2, s->n);
       +    }else{
       +        okdelim(delim = getch());
       +        getrhs(s, delim, 'a');
       +        if(nextc()==delim)
       +            getch();
       +        atnl();
       +    }
            Return:
       -        Straddc(s, 0);                /* JUST FOR CMDPRINT() */
       -        return s;
       +    Straddc(s, 0);      /* JUST FOR CMDPRINT() */
       +    return s;
        }
        
        Cmd *
        parsecmd(int nest)
        {
       -        int i, c;
       -        struct cmdtab *ct;
       -        Cmd *cp, *ncp;
       -        Cmd cmd;
       -
       -        cmd.next = cmd.ccmd = 0;
       -        cmd.re = 0;
       -        cmd.flag = cmd.num = 0;
       -        cmd.addr = compoundaddr();
       -        if(skipbl() == -1)
       -                return 0;
       -        if((c=getch())==-1)
       -                return 0;
       -        cmd.cmdc = c;
       -        if(cmd.cmdc=='c' && nextc()=='d'){        /* sleazy two-character case */
       -                getch();                /* the 'd' */
       -                cmd.cmdc='c'|0x100;
       -        }
       -        i = lookup(cmd.cmdc);
       -        if(i >= 0){
       -                if(cmd.cmdc == '\n')
       -                        goto Return;        /* let nl_cmd work it all out */
       -                ct = &cmdtab[i];
       -                if(ct->defaddr==aNo && cmd.addr)
       -                        error(Enoaddr);
       -                if(ct->count)
       -                        cmd.num = getnum();
       -                if(ct->regexp){
       -                        /* x without pattern -> .*\n, indicated by cmd.re==0 */
       -                        /* X without pattern is all files */
       -                        if((ct->cmdc!='x' && ct->cmdc!='X') ||
       -                           ((c = nextc())!=' ' && c!='\t' && c!='\n')){
       -                                skipbl();
       -                                if((c = getch())=='\n' || c<0)
       -                                        error(Enopattern);
       -                                okdelim(c);
       -                                cmd.re = getregexp(c);
       -                                if(ct->cmdc == 's'){
       -                                        cmd.ctext = newstring();
       -                                        getrhs(cmd.ctext, c, 's');
       -                                        if(nextc() == c){
       -                                                getch();
       -                                                if(nextc() == 'g')
       -                                                        cmd.flag = getch();
       -                                        }
       -                        
       -                                }
       -                        }
       -                }
       -                if(ct->addr && (cmd.caddr=simpleaddr())==0)
       -                        error(Eaddress);
       -                if(ct->defcmd){
       -                        if(skipbl() == '\n'){
       -                                getch();
       -                                cmd.ccmd = newcmd();
       -                                cmd.ccmd->cmdc = ct->defcmd;
       -                        }else if((cmd.ccmd = parsecmd(nest))==0)
       -                                panic("defcmd");
       -                }else if(ct->text)
       -                        cmd.ctext = collecttext();
       -                else if(ct->token)
       -                        cmd.ctext = collecttoken(ct->token);
       -                else
       -                        atnl();
       -        }else
       -                switch(cmd.cmdc){
       -                case '{':
       -                        cp = 0;
       -                        do{
       -                                if(skipbl()=='\n')
       -                                        getch();
       -                                ncp = parsecmd(nest+1);
       -                                if(cp)
       -                                        cp->next = ncp;
       -                                else
       -                                        cmd.ccmd = ncp;
       -                        }while(cp = ncp);
       -                        break;
       -                case '}':
       -                        atnl();
       -                        if(nest==0)
       -                                error(Enolbrace);
       -                        return 0;
       -                default:
       -                        error_c(Eunk, cmd.cmdc);
       -                }
       +    int i, c;
       +    struct cmdtab *ct;
       +    Cmd *cp, *ncp;
       +    Cmd cmd;
       +
       +    cmd.next = cmd.ccmd = 0;
       +    cmd.re = 0;
       +    cmd.flag = cmd.num = 0;
       +    cmd.addr = compoundaddr();
       +    if(skipbl() == -1)
       +        return 0;
       +    if((c=getch())==-1)
       +        return 0;
       +    cmd.cmdc = c;
       +    if(cmd.cmdc=='c' && nextc()=='d'){  /* sleazy two-character case */
       +        getch();        /* the 'd' */
       +        cmd.cmdc='c'|0x100;
       +    }
       +    i = lookup(cmd.cmdc);
       +    if(i >= 0){
       +        if(cmd.cmdc == '\n')
       +            goto Return;    /* let nl_cmd work it all out */
       +        ct = &cmdtab[i];
       +        if(ct->defaddr==aNo && cmd.addr)
       +            error(Enoaddr);
       +        if(ct->count)
       +            cmd.num = getnum();
       +        if(ct->regexp){
       +            /* x without pattern -> .*\n, indicated by cmd.re==0 */
       +            /* X without pattern is all files */
       +            if((ct->cmdc!='x' && ct->cmdc!='X') ||
       +               ((c = nextc())!=' ' && c!='\t' && c!='\n')){
       +                skipbl();
       +                if((c = getch())=='\n' || c<0)
       +                    error(Enopattern);
       +                okdelim(c);
       +                cmd.re = getregexp(c);
       +                if(ct->cmdc == 's'){
       +                    cmd.ctext = newstring();
       +                    getrhs(cmd.ctext, c, 's');
       +                    if(nextc() == c){
       +                        getch();
       +                        if(nextc() == 'g')
       +                            cmd.flag = getch();
       +                    }
       +            
       +                }
       +            }
       +        }
       +        if(ct->addr && (cmd.caddr=simpleaddr())==0)
       +            error(Eaddress);
       +        if(ct->defcmd){
       +            if(skipbl() == '\n'){
       +                getch();
       +                cmd.ccmd = newcmd();
       +                cmd.ccmd->cmdc = ct->defcmd;
       +            }else if((cmd.ccmd = parsecmd(nest))==0)
       +                panic("defcmd");
       +        }else if(ct->text)
       +            cmd.ctext = collecttext();
       +        else if(ct->token)
       +            cmd.ctext = collecttoken(ct->token);
       +        else
       +            atnl();
       +    }else
       +        switch(cmd.cmdc){
       +        case '{':
       +            cp = 0;
       +            do{
       +                if(skipbl()=='\n')
       +                    getch();
       +                ncp = parsecmd(nest+1);
       +                if(cp)
       +                    cp->next = ncp;
       +                else
       +                    cmd.ccmd = ncp;
       +            }while(cp = ncp);
       +            break;
       +        case '}':
       +            atnl();
       +            if(nest==0)
       +                error(Enolbrace);
       +            return 0;
       +        default:
       +            error_c(Eunk, cmd.cmdc);
       +        }
            Return:
       -        cp = newcmd();
       -        *cp = cmd;
       -        return cp;
       +    cp = newcmd();
       +    *cp = cmd;
       +    return cp;
        }
        
       -String*                                /* BUGGERED */
       +String*             /* BUGGERED */
        getregexp(int delim)
        {
       -        String *r = newre();
       -        int c;
       -
       -        for(Strzero(&genstr); ; Straddc(&genstr, c))
       -                if((c = getch())=='\\'){
       -                        if(nextc()==delim)
       -                                c = getch();
       -                        else if(nextc()=='\\'){
       -                                Straddc(&genstr, c);
       -                                c = getch();
       -                        }
       -                }else if(c==delim || c=='\n')
       -                        break;
       -        if(c!=delim && c)
       -                ungetch();
       -        if(genstr.n > 0){
       -                patset = TRUE;
       -                Strduplstr(&lastpat, &genstr);
       -                Straddc(&lastpat, '\0');
       -        }
       -        if(lastpat.n <= 1)
       -                error(Epattern);
       -        Strduplstr(r, &lastpat);
       -        return r;
       +    String *r = newre();
       +    int c;
       +
       +    for(Strzero(&genstr); ; Straddc(&genstr, c))
       +        if((c = getch())=='\\'){
       +            if(nextc()==delim)
       +                c = getch();
       +            else if(nextc()=='\\'){
       +                Straddc(&genstr, c);
       +                c = getch();
       +            }
       +        }else if(c==delim || c=='\n')
       +            break;
       +    if(c!=delim && c)
       +        ungetch();
       +    if(genstr.n > 0){
       +        patset = TRUE;
       +        Strduplstr(&lastpat, &genstr);
       +        Straddc(&lastpat, '\0');
       +    }
       +    if(lastpat.n <= 1)
       +        error(Epattern);
       +    Strduplstr(r, &lastpat);
       +    return r;
        }
        
        Addr *
        simpleaddr(void)
        {
       -        Addr addr;
       -        Addr *ap, *nap;
       -
       -        addr.next = 0;
       -        addr.left = 0;
       -        switch(skipbl()){
       -        case '#':
       -                addr.type = getch();
       -                addr.num = getnum();
       -                break;
       -        case '0': case '1': case '2': case '3': case '4':
       -        case '5': case '6': case '7': case '8': case '9': 
       -                addr.num = getnum();
       -                addr.type='l';
       -                break;
       -        case '/': case '?': case '"':
       -                addr.are = getregexp(addr.type = getch());
       -                break;
       -        case '.':
       -        case '$':
       -        case '+':
       -        case '-':
       -        case '\'':
       -                addr.type = getch();
       -                break;
       -        default:
       -                return 0;
       -        }
       -        if(addr.next = simpleaddr())
       -                switch(addr.next->type){
       -                case '.':
       -                case '$':
       -                case '\'':
       -                        if(addr.type!='"')
       -                case '"':
       -                                error(Eaddress);
       -                        break;
       -                case 'l':
       -                case '#':
       -                        if(addr.type=='"')
       -                                break;
       -                        /* fall through */
       -                case '/':
       -                case '?':
       -                        if(addr.type!='+' && addr.type!='-'){
       -                                /* insert the missing '+' */
       -                                nap = newaddr();
       -                                nap->type='+';
       -                                nap->next = addr.next;
       -                                addr.next = nap;
       -                        }
       -                        break;
       -                case '+':
       -                case '-':
       -                        break;
       -                default:
       -                        panic("simpleaddr");
       -                }
       -        ap = newaddr();
       -        *ap = addr;
       -        return ap;
       +    Addr addr;
       +    Addr *ap, *nap;
       +
       +    addr.next = 0;
       +    addr.left = 0;
       +    switch(skipbl()){
       +    case '#':
       +        addr.type = getch();
       +        addr.num = getnum();
       +        break;
       +    case '0': case '1': case '2': case '3': case '4':
       +    case '5': case '6': case '7': case '8': case '9': 
       +        addr.num = getnum();
       +        addr.type='l';
       +        break;
       +    case '/': case '?': case '"':
       +        addr.are = getregexp(addr.type = getch());
       +        break;
       +    case '.':
       +    case '$':
       +    case '+':
       +    case '-':
       +    case '\'':
       +        addr.type = getch();
       +        break;
       +    default:
       +        return 0;
       +    }
       +    if(addr.next = simpleaddr())
       +        switch(addr.next->type){
       +        case '.':
       +        case '$':
       +        case '\'':
       +            if(addr.type!='"')
       +        case '"':
       +                error(Eaddress);
       +            break;
       +        case 'l':
       +        case '#':
       +            if(addr.type=='"')
       +                break;
       +            /* fall through */
       +        case '/':
       +        case '?':
       +            if(addr.type!='+' && addr.type!='-'){
       +                /* insert the missing '+' */
       +                nap = newaddr();
       +                nap->type='+';
       +                nap->next = addr.next;
       +                addr.next = nap;
       +            }
       +            break;
       +        case '+':
       +        case '-':
       +            break;
       +        default:
       +            panic("simpleaddr");
       +        }
       +    ap = newaddr();
       +    *ap = addr;
       +    return ap;
        }
        
        Addr *
        compoundaddr(void)
        {
       -        Addr addr;
       -        Addr *ap, *next;
       -
       -        addr.left = simpleaddr();
       -        if((addr.type = skipbl())!=',' && addr.type!=';')
       -                return addr.left;
       -        getch();
       -        next = addr.next = compoundaddr();
       -        if(next && (next->type==',' || next->type==';') && next->left==0)
       -                error(Eaddress);
       -        ap = newaddr();
       -        *ap = addr;
       -        return ap;
       +    Addr addr;
       +    Addr *ap, *next;
       +
       +    addr.left = simpleaddr();
       +    if((addr.type = skipbl())!=',' && addr.type!=';')
       +        return addr.left;
       +    getch();
       +    next = addr.next = compoundaddr();
       +    if(next && (next->type==',' || next->type==';') && next->left==0)
       +        error(Eaddress);
       +    ap = newaddr();
       +    *ap = addr;
       +    return ap;
        }
 (DIR) diff --git a/sam/disc.c b/sam/disc.c
       @@ -1,335 +1,335 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
        #include "sam.h"
        
       -#define        BLOCKFILL        (BLOCKSIZE/2)
       +#define BLOCKFILL   (BLOCKSIZE/2)
        
       -static Discdesc        desc[NBUFFILES];
       +static Discdesc desc[NBUFFILES];
        
       -void                bkalloc(Disc*, int);
       -void                bkfree(Disc*, int);
       -void                bkwrite(Disc*, Rune*, int, int, int);
       -void                bkread(Disc*, Rune*, int, int, int);
       +void        bkalloc(Disc*, int);
       +void        bkfree(Disc*, int);
       +void        bkwrite(Disc*, Rune*, int, int, int);
       +void        bkread(Disc*, Rune*, int, int, int);
        
        
        Discdesc *
        Dstart(void)
        {
       -        int i, fd;
       -        Discdesc *dd;
       +    int i, fd;
       +    Discdesc *dd;
        
       -        for(i=0, dd=desc; dd->fd; i++, dd++)
       -                if(i == NBUFFILES-1)
       -                        panic("too many buffer files");
       -        fd = newtmp();
       -        if(fd < 0)
       -                panic("can't create buffer file");
       -        dd->fd = fd;
       -        return dd;
       +    for(i=0, dd=desc; dd->fd; i++, dd++)
       +        if(i == NBUFFILES-1)
       +            panic("too many buffer files");
       +    fd = newtmp();
       +    if(fd < 0)
       +        panic("can't create buffer file");
       +    dd->fd = fd;
       +    return dd;
        }
        
        Disc *
        Dopen(Discdesc *dd)
        {
       -        Disc *d;
       +    Disc *d;
        
       -        d = emalloc(sizeof(Disc));
       -        d->desc = dd;
       -        return d;
       +    d = emalloc(sizeof(Disc));
       +    d->desc = dd;
       +    return d;
        }
        
        void
        Dclose(Disc *d)
        {
       -        int i;
       +    int i;
        
       -        for(i=d->block.nused; --i>=0; )        /* backwards because bkfree() stacks */
       -                bkfree(d, i);
       -        free(d->block.listptr);
       -        free(d);
       +    for(i=d->block.nused; --i>=0; ) /* backwards because bkfree() stacks */
       +        bkfree(d, i);
       +    free(d->block.listptr);
       +    free(d);
        }
        
        int
        Dread(Disc *d, Rune *addr, int n, Posn p1)
        {
       -        int i, nb, nr;
       -        Posn p = 0, p2 = p1+n;
       +    int i, nb, nr;
       +    Posn p = 0, p2 = p1+n;
        
       -        for(i=0; i<d->block.nused; i++){
       -                if((p+=d->block.blkptr[i].nrunes) > p1){
       -                        p -= d->block.blkptr[i].nrunes;
       -                        goto out;
       -                }
       -        }
       -        if(p == p1)
       -                return 0;        /* eof */
       -        return -1;                /* past eof */
       +    for(i=0; i<d->block.nused; i++){
       +        if((p+=d->block.blkptr[i].nrunes) > p1){
       +            p -= d->block.blkptr[i].nrunes;
       +            goto out;
       +        }
       +    }
       +    if(p == p1)
       +        return 0;   /* eof */
       +    return -1;      /* past eof */
        
            out:
       -        n = 0;
       -        if(p != p1){        /* trailing partial block */
       -                nb = d->block.blkptr[i].nrunes;
       -                if(p2 > p+nb)
       -                        nr = nb-(p1-p);
       -                else
       -                        nr = p2-p1;
       -                bkread(d, addr, nr, i, p1-p);
       -                /* advance to next block */
       -                p += nb;
       -                addr += nr;
       -                n += nr;
       -                i++;
       -        }
       -        /* whole blocks */
       -        while(p<p2 && (nb = d->block.blkptr[i].nrunes)<=p2-p){
       -                if(i >= d->block.nused)
       -                        return n;        /* eof */
       -                bkread(d, addr, nb, i, 0);
       -                p += nb;
       -                addr += nb;
       -                n += nb;
       -                i++;
       -        }
       -        if(p < p2){        /* any initial partial block left? */
       -                nr = p2-p;
       -                nb = d->block.blkptr[i].nrunes;
       -                if(nr>nb)
       -                        nr = nb;                /* eof */
       -                /* just read in the part that survives */
       -                bkread(d, addr, nr, i, 0);
       -                n += nr;
       -        }
       -        return n;
       +    n = 0;
       +    if(p != p1){    /* trailing partial block */
       +        nb = d->block.blkptr[i].nrunes;
       +        if(p2 > p+nb)
       +            nr = nb-(p1-p);
       +        else
       +            nr = p2-p1;
       +        bkread(d, addr, nr, i, p1-p);
       +        /* advance to next block */
       +        p += nb;
       +        addr += nr;
       +        n += nr;
       +        i++;
       +    }
       +    /* whole blocks */
       +    while(p<p2 && (nb = d->block.blkptr[i].nrunes)<=p2-p){
       +        if(i >= d->block.nused)
       +            return n;   /* eof */
       +        bkread(d, addr, nb, i, 0);
       +        p += nb;
       +        addr += nb;
       +        n += nb;
       +        i++;
       +    }
       +    if(p < p2){ /* any initial partial block left? */
       +        nr = p2-p;
       +        nb = d->block.blkptr[i].nrunes;
       +        if(nr>nb)
       +            nr = nb;        /* eof */
       +        /* just read in the part that survives */
       +        bkread(d, addr, nr, i, 0);
       +        n += nr;
       +    }
       +    return n;
        }
        
        void
        Dinsert(Disc *d, Rune *addr, int n, Posn p0) /* if addr null, just make space */
        {
       -        int i, nb, ni;
       -        Posn p = 0;
       -        Rune hold[BLOCKSIZE];
       -        int nhold;
       +    int i, nb, ni;
       +    Posn p = 0;
       +    Rune hold[BLOCKSIZE];
       +    int nhold;
        
       -        for(i=0; i<d->block.nused; i++){
       -                if((p+=d->block.blkptr[i].nrunes) >= p0){
       -                        p -= d->block.blkptr[i].nrunes;
       -                        goto out;
       -                }
       -        }
       -        if(p != p0)
       -                panic("Dinsert");        /* beyond eof */
       +    for(i=0; i<d->block.nused; i++){
       +        if((p+=d->block.blkptr[i].nrunes) >= p0){
       +            p -= d->block.blkptr[i].nrunes;
       +            goto out;
       +        }
       +    }
       +    if(p != p0)
       +        panic("Dinsert");   /* beyond eof */
        
            out:
       -        d->nrunes += n;
       -        nhold = 0;
       -        if(i<d->block.nused && (nb=d->block.blkptr[i].nrunes)>p0-p){
       -                nhold = nb-(p0-p);
       -                bkread(d, hold, nhold, i, p0-p);
       -                d->block.blkptr[i].nrunes -= nhold;        /* no write necessary */
       -        }
       -        /* insertion point is now at end of block i (which may not exist) */
       -        while(n > 0){
       -                if(i < d->block.nused
       -                && (nb=d->block.blkptr[i].nrunes) < BLOCKFILL){
       -                        /* fill this block */
       -                        if(nb+n > BLOCKSIZE)
       -                                ni = BLOCKFILL-nb;
       -                        else
       -                                ni = n;
       -                        if(addr)
       -                                bkwrite(d, addr, ni, i, nb);
       -                        nb += ni;
       -                }else{        /* make new block */
       -                        if(i < d->block.nused)
       -                                i++;        /* put after this block, if it exists */
       -                        bkalloc(d, i);
       -                        if(n > BLOCKSIZE)
       -                                ni = BLOCKFILL;
       -                        else
       -                                ni = n;
       -                        if(addr)
       -                                bkwrite(d, addr, ni, i, 0);
       -                        nb = ni;
       -                }
       -                d->block.blkptr[i].nrunes = nb;
       -                if(addr)
       -                        addr += ni;
       -                n -= ni;
       -        }
       -        if(nhold){
       -                if(i < d->block.nused
       -                && (nb=d->block.blkptr[i].nrunes)+nhold < BLOCKSIZE){
       -                        /* fill this block */
       -                        bkwrite(d, hold, nhold, i, nb);
       -                        nb += nhold;
       -                }else{        /* make new block */
       -                        if(i < d->block.nused)
       -                                i++;        /* put after this block, if it exists */
       -                        bkalloc(d, i);
       -                        bkwrite(d, hold, nhold, i, 0);
       -                        nb = nhold;
       -                }
       -                d->block.blkptr[i].nrunes = nb;
       -        }
       +    d->nrunes += n;
       +    nhold = 0;
       +    if(i<d->block.nused && (nb=d->block.blkptr[i].nrunes)>p0-p){
       +        nhold = nb-(p0-p);
       +        bkread(d, hold, nhold, i, p0-p);
       +        d->block.blkptr[i].nrunes -= nhold; /* no write necessary */
       +    }
       +    /* insertion point is now at end of block i (which may not exist) */
       +    while(n > 0){
       +        if(i < d->block.nused
       +        && (nb=d->block.blkptr[i].nrunes) < BLOCKFILL){
       +            /* fill this block */
       +            if(nb+n > BLOCKSIZE)
       +                ni = BLOCKFILL-nb;
       +            else
       +                ni = n;
       +            if(addr)
       +                bkwrite(d, addr, ni, i, nb);
       +            nb += ni;
       +        }else{  /* make new block */
       +            if(i < d->block.nused)
       +                i++;    /* put after this block, if it exists */
       +            bkalloc(d, i);
       +            if(n > BLOCKSIZE)
       +                ni = BLOCKFILL;
       +            else
       +                ni = n;
       +            if(addr)
       +                bkwrite(d, addr, ni, i, 0);
       +            nb = ni;
       +        }
       +        d->block.blkptr[i].nrunes = nb;
       +        if(addr)
       +            addr += ni;
       +        n -= ni;
       +    }
       +    if(nhold){
       +        if(i < d->block.nused
       +        && (nb=d->block.blkptr[i].nrunes)+nhold < BLOCKSIZE){
       +            /* fill this block */
       +            bkwrite(d, hold, nhold, i, nb);
       +            nb += nhold;
       +        }else{  /* make new block */
       +            if(i < d->block.nused)
       +                i++;    /* put after this block, if it exists */
       +            bkalloc(d, i);
       +            bkwrite(d, hold, nhold, i, 0);
       +            nb = nhold;
       +        }
       +        d->block.blkptr[i].nrunes = nb;
       +    }
        }
        
        void
        Ddelete(Disc *d, Posn p1, Posn p2)
        {
       -        int i, nb, nd;
       -        Posn p = 0;
       -        Rune buf[BLOCKSIZE];
       +    int i, nb, nd;
       +    Posn p = 0;
       +    Rune buf[BLOCKSIZE];
        
       -        for(i = 0; i<d->block.nused; i++){
       -                if((p+=d->block.blkptr[i].nrunes) > p1){
       -                        p -= d->block.blkptr[i].nrunes;
       -                        goto out;
       -                }
       -        }
       -        if(p1!=d->nrunes || p2!=p1)
       -                panic("Ddelete");
       -        return;        /* beyond eof */
       +    for(i = 0; i<d->block.nused; i++){
       +        if((p+=d->block.blkptr[i].nrunes) > p1){
       +            p -= d->block.blkptr[i].nrunes;
       +            goto out;
       +        }
       +    }
       +    if(p1!=d->nrunes || p2!=p1)
       +        panic("Ddelete");
       +    return; /* beyond eof */
        
            out:
       -        d->nrunes -= p2-p1;
       -        if(p != p1){        /* throw away partial block */
       -                nb = d->block.blkptr[i].nrunes;
       -                bkread(d, buf, nb, i, 0);
       -                if(p2 >= p+nb)
       -                        nd = nb-(p1-p);
       -                else{
       -                        nd = p2-p1;
       -                        memmove(buf+(p1-p), buf+(p1-p)+nd, RUNESIZE*(nb-((p1-p)+nd)));
       -                }
       -                nb -= nd;
       -                bkwrite(d, buf, nb, i, 0);
       -                d->block.blkptr[i].nrunes = nb;
       -                p2 -= nd;
       -                /* advance to next block */
       -                p += nb;
       -                i++;
       -        }
       -        /* throw away whole blocks */
       -        while(p<p2 && (nb = d->block.blkptr[i].nrunes)<=p2-p){
       -                if(i >= d->block.nused)
       -                        panic("Ddelete 2");
       -                bkfree(d, i);
       -                p2 -= nb;
       -        }
       -        if(p >= p2)        /* any initial partial block left to delete? */
       -                return;        /* no */
       -        nd = p2-p;
       -        nb = d->block.blkptr[i].nrunes;
       -        /* just read in the part that survives */
       -        bkread(d, buf, nb-=nd, i, nd);
       -        /* a little block merging */
       -        if(nb<BLOCKSIZE/2 && i>0 && (nd = d->block.blkptr[i-1].nrunes)<BLOCKSIZE/2){
       -                memmove(buf+nd, buf, RUNESIZE*nb);
       -                bkread(d, buf, nd, --i, 0);
       -                bkfree(d, i);
       -                nb += nd;
       -        }
       -        bkwrite(d, buf, nb, i, 0);
       -        d->block.blkptr[i].nrunes = nb;
       +    d->nrunes -= p2-p1;
       +    if(p != p1){    /* throw away partial block */
       +        nb = d->block.blkptr[i].nrunes;
       +        bkread(d, buf, nb, i, 0);
       +        if(p2 >= p+nb)
       +            nd = nb-(p1-p);
       +        else{
       +            nd = p2-p1;
       +            memmove(buf+(p1-p), buf+(p1-p)+nd, RUNESIZE*(nb-((p1-p)+nd)));
       +        }
       +        nb -= nd;
       +        bkwrite(d, buf, nb, i, 0);
       +        d->block.blkptr[i].nrunes = nb;
       +        p2 -= nd;
       +        /* advance to next block */
       +        p += nb;
       +        i++;
       +    }
       +    /* throw away whole blocks */
       +    while(p<p2 && (nb = d->block.blkptr[i].nrunes)<=p2-p){
       +        if(i >= d->block.nused)
       +            panic("Ddelete 2");
       +        bkfree(d, i);
       +        p2 -= nb;
       +    }
       +    if(p >= p2) /* any initial partial block left to delete? */
       +        return; /* no */
       +    nd = p2-p;
       +    nb = d->block.blkptr[i].nrunes;
       +    /* just read in the part that survives */
       +    bkread(d, buf, nb-=nd, i, nd);
       +    /* a little block merging */
       +    if(nb<BLOCKSIZE/2 && i>0 && (nd = d->block.blkptr[i-1].nrunes)<BLOCKSIZE/2){
       +        memmove(buf+nd, buf, RUNESIZE*nb);
       +        bkread(d, buf, nd, --i, 0);
       +        bkfree(d, i);
       +        nb += nd;
       +    }
       +    bkwrite(d, buf, nb, i, 0);
       +    d->block.blkptr[i].nrunes = nb;
        }
        
        void
        Dreplace(Disc *d, Posn p1, Posn p2, Rune *addr, int n)
        {
       -        int i, nb, nr;
       -        Posn p = 0;
       -        Rune buf[BLOCKSIZE];
       +    int i, nb, nr;
       +    Posn p = 0;
       +    Rune buf[BLOCKSIZE];
        
       -        if(p2-p1 > n)
       -                Ddelete(d, p1+n, p2);
       -        else if(p2-p1 < n)
       -                Dinsert(d, 0, n-(p2-p1), p2);
       -        if(n == 0)
       -                return;
       -        p2 = p1+n;
       -        /* they're now conformal; replace in place */
       -        for(i=0; i<d->block.nused; i++){
       -                if((p+=d->block.blkptr[i].nrunes) > p1){
       -                        p -= d->block.blkptr[i].nrunes;
       -                        goto out;
       -                }
       -        }
       -        panic("Dreplace");
       +    if(p2-p1 > n)
       +        Ddelete(d, p1+n, p2);
       +    else if(p2-p1 < n)
       +        Dinsert(d, 0, n-(p2-p1), p2);
       +    if(n == 0)
       +        return;
       +    p2 = p1+n;
       +    /* they're now conformal; replace in place */
       +    for(i=0; i<d->block.nused; i++){
       +        if((p+=d->block.blkptr[i].nrunes) > p1){
       +            p -= d->block.blkptr[i].nrunes;
       +            goto out;
       +        }
       +    }
       +    panic("Dreplace");
        
            out:
       -        if(p != p1){        /* trailing partial block */
       -                nb = d->block.blkptr[i].nrunes;
       -                bkread(d, buf, nb, i, 0);
       -                if(p2 > p+nb)
       -                        nr = nb-(p1-p);
       -                else
       -                        nr = p2-p1;
       -                memmove(buf+p1-p, addr, RUNESIZE*nr);
       -                bkwrite(d, buf, nb, i, 0);
       -                /* advance to next block */
       -                p += nb;
       -                addr += nr;
       -                i++;
       -        }
       -        /* whole blocks */
       -        while(p<p2 && (nb = d->block.blkptr[i].nrunes)<=p2-p){
       -                if(i >= d->block.nused)
       -                        panic("Dreplace 2");
       -                bkwrite(d, addr, nb, i, 0);
       -                p += nb;
       -                addr += nb;
       -                i++;
       -        }
       -        if(p < p2){        /* any initial partial block left? */
       -                nr = p2-p;
       -                nb = d->block.blkptr[i].nrunes;
       -                /* just read in the part that survives */
       -                bkread(d, buf+nr, nb-nr, i, nr);
       -                memmove(buf, addr, RUNESIZE*nr);
       -                bkwrite(d, buf, nb, i, 0);
       -        }
       +    if(p != p1){    /* trailing partial block */
       +        nb = d->block.blkptr[i].nrunes;
       +        bkread(d, buf, nb, i, 0);
       +        if(p2 > p+nb)
       +            nr = nb-(p1-p);
       +        else
       +            nr = p2-p1;
       +        memmove(buf+p1-p, addr, RUNESIZE*nr);
       +        bkwrite(d, buf, nb, i, 0);
       +        /* advance to next block */
       +        p += nb;
       +        addr += nr;
       +        i++;
       +    }
       +    /* whole blocks */
       +    while(p<p2 && (nb = d->block.blkptr[i].nrunes)<=p2-p){
       +        if(i >= d->block.nused)
       +            panic("Dreplace 2");
       +        bkwrite(d, addr, nb, i, 0);
       +        p += nb;
       +        addr += nb;
       +        i++;
       +    }
       +    if(p < p2){ /* any initial partial block left? */
       +        nr = p2-p;
       +        nb = d->block.blkptr[i].nrunes;
       +        /* just read in the part that survives */
       +        bkread(d, buf+nr, nb-nr, i, nr);
       +        memmove(buf, addr, RUNESIZE*nr);
       +        bkwrite(d, buf, nb, i, 0);
       +    }
        }
        
        void
        bkread(Disc *d, Rune *loc, int n, int bk, int off)
        {
       -        Seek(d->desc->fd, RUNESIZE*(BLOCKSIZE*d->block.blkptr[bk].bnum+off), 0);
       -        Read(d->desc->fd, loc, n*RUNESIZE);
       +    Seek(d->desc->fd, RUNESIZE*(BLOCKSIZE*d->block.blkptr[bk].bnum+off), 0);
       +    Read(d->desc->fd, loc, n*RUNESIZE);
        }
        
        void
        bkwrite(Disc *d, Rune *loc, int n, int bk, int off)
        {
       -        Seek(d->desc->fd, RUNESIZE*(BLOCKSIZE*d->block.blkptr[bk].bnum+off), 0);
       -        Write(d->desc->fd, loc, n*RUNESIZE);
       -                /*
       -                 * sleazy hack to avoid silly SGI kernel bug
       -                 *        fill partial block with garbage
       -                 */
       -        if (off+n >= d->block.blkptr[bk].nrunes && off+n < BLOCKSIZE)
       -                Write(d->desc->fd, genbuf, (BLOCKSIZE-(off+n))*RUNESIZE);
       +    Seek(d->desc->fd, RUNESIZE*(BLOCKSIZE*d->block.blkptr[bk].bnum+off), 0);
       +    Write(d->desc->fd, loc, n*RUNESIZE);
       +        /*
       +         * sleazy hack to avoid silly SGI kernel bug
       +         *  fill partial block with garbage
       +         */
       +    if (off+n >= d->block.blkptr[bk].nrunes && off+n < BLOCKSIZE)
       +        Write(d->desc->fd, genbuf, (BLOCKSIZE-(off+n))*RUNESIZE);
        }
        
        void
        bkalloc(Disc *d, int n)
        {
       -        Discdesc *dd = d->desc;
       -        ulong bnum;
       +    Discdesc *dd = d->desc;
       +    ulong bnum;
        
       -        if(dd->free.nused)
       -                bnum = dd->free.longptr[--dd->free.nused];
       -        else
       -                bnum = dd->nbk++;
       -        if(bnum >= 1<<(8*(sizeof(((Block*)0)->bnum))))
       -                error(Etmpovfl);
       -        inslist(&d->block, n, 0L);
       -        d->block.blkptr[n].bnum = bnum;
       +    if(dd->free.nused)
       +        bnum = dd->free.longptr[--dd->free.nused];
       +    else
       +        bnum = dd->nbk++;
       +    if(bnum >= 1<<(8*(sizeof(((Block*)0)->bnum))))
       +        error(Etmpovfl);
       +    inslist(&d->block, n, 0L);
       +    d->block.blkptr[n].bnum = bnum;
        }
        
        void
        bkfree(Disc *d, int n)
        {
       -        Discdesc *dd = d->desc;
       +    Discdesc *dd = d->desc;
        
       -        inslist(&dd->free, dd->free.nused, d->block.blkptr[n].bnum);
       -        dellist(&d->block, n);
       +    inslist(&dd->free, dd->free.nused, d->block.blkptr[n].bnum);
       +    dellist(&d->block, n);
        }
 (DIR) diff --git a/sam/error.c b/sam/error.c
       @@ -2,131 +2,131 @@
        #include "sam.h"
        
        static char *emsg[]={
       -        /* error_s */
       -        "can't open",
       -        "can't create",
       -        "not in menu:",
       -        "changes to",
       -        "I/O error:",
       -        /* error_c */
       -        "unknown command",
       -        "no operand for",
       -        "bad delimiter",
       -        /* error */
       -        "can't fork",
       -        "interrupt",
       -        "address",
       -        "search",
       -        "pattern",
       -        "newline expected",
       -        "blank expected",
       -        "pattern expected",
       -        "can't nest X or Y",
       -        "unmatched `}'",
       -        "command takes no address",
       -        "addresses overlap",
       -        "substitution",
       -        "& match too long",
       -        "bad \\ in rhs",
       -        "address range",
       -        "changes not in sequence",
       -        "addresses out of order",
       -        "no file name",
       -        "unmatched `('",
       -        "unmatched `)'",
       -        "malformed `[]'",
       -        "malformed regexp",
       -        "reg. exp. list overflow",
       -        "plan 9 command",
       -        "can't pipe",
       -        "no current file",
       -        "string too long",
       -        "changed files",
       -        "empty string",
       -        "file search",
       -        "non-unique match for \"\"",
       -        "tag match too long",
       -        "too many subexpressions",
       -        "temporary file too large",
       -        "file is append-only",
       +    /* error_s */
       +    "can't open",
       +    "can't create",
       +    "not in menu:",
       +    "changes to",
       +    "I/O error:",
       +    /* error_c */
       +    "unknown command",
       +    "no operand for",
       +    "bad delimiter",
       +    /* error */
       +    "can't fork",
       +    "interrupt",
       +    "address",
       +    "search",
       +    "pattern",
       +    "newline expected",
       +    "blank expected",
       +    "pattern expected",
       +    "can't nest X or Y",
       +    "unmatched `}'",
       +    "command takes no address",
       +    "addresses overlap",
       +    "substitution",
       +    "& match too long",
       +    "bad \\ in rhs",
       +    "address range",
       +    "changes not in sequence",
       +    "addresses out of order",
       +    "no file name",
       +    "unmatched `('",
       +    "unmatched `)'",
       +    "malformed `[]'",
       +    "malformed regexp",
       +    "reg. exp. list overflow",
       +    "plan 9 command",
       +    "can't pipe",
       +    "no current file",
       +    "string too long",
       +    "changed files",
       +    "empty string",
       +    "file search",
       +    "non-unique match for \"\"",
       +    "tag match too long",
       +    "too many subexpressions",
       +    "temporary file too large",
       +    "file is append-only",
        };
        static char *wmsg[]={
       -        /* warn_s */
       -        "duplicate file name",
       -        "no such file",
       -        "write might change good version of",
       -        /* warn_S */
       -        "files might be aliased",
       -        /* warn */
       -        "null characters elided",
       -        "can't run pwd",
       -        "last char not newline",
       -        "exit status not 0",
       +    /* warn_s */
       +    "duplicate file name",
       +    "no such file",
       +    "write might change good version of",
       +    /* warn_S */
       +    "files might be aliased",
       +    /* warn */
       +    "null characters elided",
       +    "can't run pwd",
       +    "last char not newline",
       +    "exit status not 0",
        };
        
        void
        error(Err s)
        {
       -        char buf[512];
       +    char buf[512];
        
       -        sprint(buf, "?%s", emsg[s]);
       -        hiccough(buf);
       +    sprint(buf, "?%s", emsg[s]);
       +    hiccough(buf);
        }
        
        void
        error_s(Err s, char *a)
        {
       -        char buf[512];
       +    char buf[512];
        
       -        sprint(buf, "?%s \"%s\"", emsg[s], a);
       -        hiccough(buf);
       +    sprint(buf, "?%s \"%s\"", emsg[s], a);
       +    hiccough(buf);
        }
        
        void
        error_c(Err s, int c)
        {
       -        char buf[512];
       +    char buf[512];
        
       -        sprint(buf, "?%s `%c'", emsg[s], c);
       -        hiccough(buf);
       +    sprint(buf, "?%s `%c'", emsg[s], c);
       +    hiccough(buf);
        }
        
        void
        warn(Warn s)
        {
       -        dprint("?warning: %s\n", wmsg[s]);
       +    dprint("?warning: %s\n", wmsg[s]);
        }
        
        void
        warn_S(Warn s, String *a)
        {
       -        print_s(wmsg[s], a);
       +    print_s(wmsg[s], a);
        }
        
        void
        warn_SS(Warn s, String *a, String *b)
        {
       -        print_ss(wmsg[s], a, b);
       +    print_ss(wmsg[s], a, b);
        }
        
        void
        warn_s(Warn s, char *a)
        {
       -        dprint("?warning: %s `%s'\n", wmsg[s], a);
       +    dprint("?warning: %s `%s'\n", wmsg[s], a);
        }
        
        void
        termwrite(char *s)
        {
       -        String *p;
       +    String *p;
        
       -        if(downloaded){
       -                p = tmpcstr(s);
       -                if(cmd)
       -                        Finsert(cmd, p, cmdpt);
       -                else
       -                        Strinsert(&cmdstr, p, cmdstr.n);
       -                cmdptadv += p->n;
       -        }else
       -                Write(2, s, strlen(s));
       +    if(downloaded){
       +        p = tmpcstr(s);
       +        if(cmd)
       +            Finsert(cmd, p, cmdpt);
       +        else
       +            Strinsert(&cmdstr, p, cmdstr.n);
       +        cmdptadv += p->n;
       +    }else
       +        Write(2, s, strlen(s));
        }
 (DIR) diff --git a/sam/errors.h b/sam/errors.h
       @@ -1,63 +1,63 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
        typedef enum Err{
       -        /* error_s */
       -        Eopen,
       -        Ecreate,
       -        Emenu,
       -        Emodified,
       -        Eio,
       -        /* error_c */
       -        Eunk,
       -        Emissop,
       -        Edelim,
       -        /* error */
       -        Efork,
       -        Eintr,
       -        Eaddress,
       -        Esearch,
       -        Epattern,
       -        Enewline,
       -        Eblank,
       -        Enopattern,
       -        EnestXY,
       -        Enolbrace,
       -        Enoaddr,
       -        Eoverlap,
       -        Enosub,
       -        Elongrhs,
       -        Ebadrhs,
       -        Erange,
       -        Esequence,
       -        Eorder,
       -        Enoname,
       -        Eleftpar,
       -        Erightpar,
       -        Ebadclass,
       -        Ebadregexp,
       -        Eoverflow,
       -        Enocmd,
       -        Epipe,
       -        Enofile,
       -        Etoolong,
       -        Echanges,
       -        Eempty,
       -        Efsearch,
       -        Emanyfiles,
       -        Elongtag,
       -        Esubexp,
       -        Etmpovfl,
       -        Eappend
       +    /* error_s */
       +    Eopen,
       +    Ecreate,
       +    Emenu,
       +    Emodified,
       +    Eio,
       +    /* error_c */
       +    Eunk,
       +    Emissop,
       +    Edelim,
       +    /* error */
       +    Efork,
       +    Eintr,
       +    Eaddress,
       +    Esearch,
       +    Epattern,
       +    Enewline,
       +    Eblank,
       +    Enopattern,
       +    EnestXY,
       +    Enolbrace,
       +    Enoaddr,
       +    Eoverlap,
       +    Enosub,
       +    Elongrhs,
       +    Ebadrhs,
       +    Erange,
       +    Esequence,
       +    Eorder,
       +    Enoname,
       +    Eleftpar,
       +    Erightpar,
       +    Ebadclass,
       +    Ebadregexp,
       +    Eoverflow,
       +    Enocmd,
       +    Epipe,
       +    Enofile,
       +    Etoolong,
       +    Echanges,
       +    Eempty,
       +    Efsearch,
       +    Emanyfiles,
       +    Elongtag,
       +    Esubexp,
       +    Etmpovfl,
       +    Eappend
        }Err;
        typedef enum Warn{
       -        /* warn_s */
       -        Wdupname,
       -        Wfile,
       -        Wdate,
       -        /* warn_ss */
       -        Wdupfile,
       -        /* warn */
       -        Wnulls,
       -        Wpwd,
       -        Wnotnewline,
       -        Wbadstatus
       +    /* warn_s */
       +    Wdupname,
       +    Wfile,
       +    Wdate,
       +    /* warn_ss */
       +    Wdupfile,
       +    /* warn */
       +    Wnulls,
       +    Wpwd,
       +    Wnotnewline,
       +    Wbadstatus
        }Warn;
 (DIR) diff --git a/sam/file.c b/sam/file.c
       @@ -3,211 +3,211 @@
        /*
         * Files are splayed out a factor of NDISC to reduce indirect block access
         */
       -Discdesc        *files[NDISC];
       -Discdesc        *transcripts[NDISC];
       -Buffer                *undobuf;
       -static String        *ftempstr(Rune*, int);
       -int                fcount;
       -File                *lastfile;
       -
       -void        puthdr_csl(Buffer*, char, short, Posn);
       -void        puthdr_cs(Buffer*, char, short);
       -void        puthdr_M(Buffer*, Posn, Range, Range, Mod, short);
       -void        puthdr_cll(Buffer*, char, Posn, Posn);
       -void        Fflush(File*);
       +Discdesc    *files[NDISC];
       +Discdesc    *transcripts[NDISC];
       +Buffer      *undobuf;
       +static String   *ftempstr(Rune*, int);
       +int     fcount;
       +File        *lastfile;
       +
       +void    puthdr_csl(Buffer*, char, short, Posn);
       +void    puthdr_cs(Buffer*, char, short);
       +void    puthdr_M(Buffer*, Posn, Range, Range, Mod, short);
       +void    puthdr_cll(Buffer*, char, Posn, Posn);
       +void    Fflush(File*);
        
        enum{
       -        SKIP=50,                /* max dist between file changes folded together */
       -        MAXCACHE=STRSIZE        /* max length of cache. must be < 32K-BLOCKSIZE */
       +    SKIP=50,        /* max dist between file changes folded together */
       +    MAXCACHE=STRSIZE    /* max length of cache. must be < 32K-BLOCKSIZE */
        };
        
        void
        Fstart(void)
        {
       -        undobuf = Bopen(Dstart());
       -        snarfbuf = Bopen(Dstart());
       -        plan9buf = Bopen(Dstart());
       +    undobuf = Bopen(Dstart());
       +    snarfbuf = Bopen(Dstart());
       +    plan9buf = Bopen(Dstart());
        }
        
        void
        Fmark(File *f, Mod m)
        {
       -        Buffer *t = f->transcript;
       -        Posn p;
       -
       -        if(f->state == Readerr)
       -                return;
       -        if(f->state == Unread)        /* this is implicit 'e' of a file */
       -                return;
       -        p = m==0? -1 : f->markp;
       -        f->markp = t->nrunes;
       -        puthdr_M(t, p, f->dot.r, f->mark, f->mod, f->state);
       -        f->ndot = f->dot;
       -        f->marked = TRUE;
       -        f->mod = m;
       -        f->hiposn = -1;
       -        /* Safety first */
       -        f->cp1 = f->cp2 = 0;
       +    Buffer *t = f->transcript;
       +    Posn p;
       +
       +    if(f->state == Readerr)
       +        return;
       +    if(f->state == Unread)  /* this is implicit 'e' of a file */
       +        return;
       +    p = m==0? -1 : f->markp;
       +    f->markp = t->nrunes;
       +    puthdr_M(t, p, f->dot.r, f->mark, f->mod, f->state);
       +    f->ndot = f->dot;
       +    f->marked = TRUE;
       +    f->mod = m;
       +    f->hiposn = -1;
       +    /* Safety first */
       +    f->cp1 = f->cp2 = 0;
        }
        
        File *
        Fopen(void)
        {
       -        File *f;
       -
       -        f = emalloc(sizeof(File));
       -        if(files[fcount] == 0){
       -                files[fcount] = Dstart();
       -                transcripts[fcount] = Dstart();
       -        }
       -        f->buf = Bopen(files[fcount]);
       -        f->transcript = Bopen(transcripts[fcount]);
       -        if(++fcount == NDISC)
       -                fcount = 0;
       -        f->nrunes = 0;
       -        f->markp = 0;
       -        f->mod = 0;
       -        f->dot.f = f;
       -        f->ndot.f = f;
       -        f->dev = ~0;
       -        f->qid = ~0;
       -        Strinit0(&f->name);
       -        Strinit(&f->cache);
       -        f->state = Unread;
       -        Fmark(f, (Mod)0);
       -        return f;
       +    File *f;
       +
       +    f = emalloc(sizeof(File));
       +    if(files[fcount] == 0){
       +        files[fcount] = Dstart();
       +        transcripts[fcount] = Dstart();
       +    }
       +    f->buf = Bopen(files[fcount]);
       +    f->transcript = Bopen(transcripts[fcount]);
       +    if(++fcount == NDISC)
       +        fcount = 0;
       +    f->nrunes = 0;
       +    f->markp = 0;
       +    f->mod = 0;
       +    f->dot.f = f;
       +    f->ndot.f = f;
       +    f->dev = ~0;
       +    f->qid = ~0;
       +    Strinit0(&f->name);
       +    Strinit(&f->cache);
       +    f->state = Unread;
       +    Fmark(f, (Mod)0);
       +    return f;
        }
        
        void
        Fclose(File *f)
        {
       -        if(f == lastfile)
       -                lastfile = 0;
       -        Bterm(f->buf);
       -        Bterm(f->transcript);
       -        Strclose(&f->name);
       -        Strclose(&f->cache);
       -        if(f->rasp)
       -                listfree(f->rasp);
       -        free(f);
       +    if(f == lastfile)
       +        lastfile = 0;
       +    Bterm(f->buf);
       +    Bterm(f->transcript);
       +    Strclose(&f->name);
       +    Strclose(&f->cache);
       +    if(f->rasp)
       +        listfree(f->rasp);
       +    free(f);
        }
        
        void
        Finsert(File *f, String *str, Posn p1)
        {
       -        Buffer *t = f->transcript;
       -
       -        if(f->state == Readerr)
       -                return;
       -        if(str->n == 0)
       -                return;
       -        if(str->n<0 || str->n>STRSIZE)
       -                panic("Finsert");
       -        if(f->mod < modnum)
       -                Fmark(f, modnum);
       -        if(p1 < f->hiposn)
       -                error(Esequence);
       -        if(str->n >= BLOCKSIZE){        /* don't bother with the cache */
       -                Fflush(f);
       -                puthdr_csl(t, 'i', str->n, p1);
       -                Binsert(t, str, t->nrunes);
       -        }else{        /* insert into the cache instead of the transcript */
       -                if(f->cp2==0 && f->cp1==0 && f->cache.n==0)        /* empty cache */
       -                        f->cp1 = f->cp2 = p1;
       -                if(p1-f->cp2>SKIP || f->cache.n+str->n>MAXCACHE-SKIP){
       -                        Fflush(f);
       -                        f->cp1 = f->cp2 = p1;
       -                }
       -                if(f->cp2 != p1){        /* grab the piece in between */
       -                        Rune buf[SKIP];
       -                        String s;
       -                        Fchars(f, buf, f->cp2, p1);
       -                        s.s = buf;
       -                        s.n = p1-f->cp2;
       -                        Strinsert(&f->cache, &s, f->cache.n);
       -                        f->cp2 = p1;
       -                }
       -                Strinsert(&f->cache, str, f->cache.n);
       -        }
       -        if(f != cmd)
       -                quitok = FALSE;
       -        f->closeok = FALSE;
       -        if(f->state == Clean)
       -                state(f, Dirty);
       -        f->hiposn = p1;
       +    Buffer *t = f->transcript;
       +
       +    if(f->state == Readerr)
       +        return;
       +    if(str->n == 0)
       +        return;
       +    if(str->n<0 || str->n>STRSIZE)
       +        panic("Finsert");
       +    if(f->mod < modnum)
       +        Fmark(f, modnum);
       +    if(p1 < f->hiposn)
       +        error(Esequence);
       +    if(str->n >= BLOCKSIZE){    /* don't bother with the cache */
       +        Fflush(f);
       +        puthdr_csl(t, 'i', str->n, p1);
       +        Binsert(t, str, t->nrunes);
       +    }else{  /* insert into the cache instead of the transcript */
       +        if(f->cp2==0 && f->cp1==0 && f->cache.n==0) /* empty cache */
       +            f->cp1 = f->cp2 = p1;
       +        if(p1-f->cp2>SKIP || f->cache.n+str->n>MAXCACHE-SKIP){
       +            Fflush(f);
       +            f->cp1 = f->cp2 = p1;
       +        }
       +        if(f->cp2 != p1){   /* grab the piece in between */
       +            Rune buf[SKIP];
       +            String s;
       +            Fchars(f, buf, f->cp2, p1);
       +            s.s = buf;
       +            s.n = p1-f->cp2;
       +            Strinsert(&f->cache, &s, f->cache.n);
       +            f->cp2 = p1;
       +        }
       +        Strinsert(&f->cache, str, f->cache.n);
       +    }
       +    if(f != cmd)
       +        quitok = FALSE;
       +    f->closeok = FALSE;
       +    if(f->state == Clean)
       +        state(f, Dirty);
       +    f->hiposn = p1;
        }
        
        void
        Fdelete(File *f, Posn p1, Posn p2)
        {
       -        if(f->state == Readerr)
       -                return;
       -        if(p1==p2)
       -                return;
       -        if(f->mod<modnum)
       -                Fmark(f, modnum);
       -        if(p1<f->hiposn)
       -                error(Esequence);
       -        if(p1-f->cp2>SKIP)
       -                Fflush(f);
       -        if(f->cp2==0 && f->cp1==0 && f->cache.n==0)        /* empty cache */
       -                f->cp1 = f->cp2 = p1;
       -        if(f->cp2 != p1){        /* grab the piece in between */
       -                if(f->cache.n+(p1-f->cp2)>MAXCACHE){
       -                        Fflush(f);
       -                        f->cp1 = f->cp2 = p1;
       -                }else{
       -                        Rune buf[SKIP];
       -                        String s;
       -                        Fchars(f, buf, f->cp2, p1);
       -                        s.s = buf;
       -                        s.n = p1-f->cp2;
       -                        Strinsert(&f->cache, &s, f->cache.n);
       -                }
       -        }
       -        f->cp2 = p2;
       -        if(f!=cmd)
       -                quitok = FALSE;
       -        f->closeok = FALSE;
       -        if(f->state==Clean)
       -                state(f, Dirty);
       -        f->hiposn = p2;
       +    if(f->state == Readerr)
       +        return;
       +    if(p1==p2)
       +        return;
       +    if(f->mod<modnum)
       +        Fmark(f, modnum);
       +    if(p1<f->hiposn)
       +        error(Esequence);
       +    if(p1-f->cp2>SKIP)
       +        Fflush(f);
       +    if(f->cp2==0 && f->cp1==0 && f->cache.n==0) /* empty cache */
       +        f->cp1 = f->cp2 = p1;
       +    if(f->cp2 != p1){   /* grab the piece in between */
       +        if(f->cache.n+(p1-f->cp2)>MAXCACHE){
       +            Fflush(f);
       +            f->cp1 = f->cp2 = p1;
       +        }else{
       +            Rune buf[SKIP];
       +            String s;
       +            Fchars(f, buf, f->cp2, p1);
       +            s.s = buf;
       +            s.n = p1-f->cp2;
       +            Strinsert(&f->cache, &s, f->cache.n);
       +        }
       +    }
       +    f->cp2 = p2;
       +    if(f!=cmd)
       +        quitok = FALSE;
       +    f->closeok = FALSE;
       +    if(f->state==Clean)
       +        state(f, Dirty);
       +    f->hiposn = p2;
        }
        
        void
        Fflush(File *f)
        {
       -        Buffer *t = f->transcript;
       -        Posn p1 = f->cp1, p2 = f->cp2;
       -
       -        if(f->state == Readerr)
       -                return;
       -        if(p1 != p2)
       -                puthdr_cll(t, 'd', p1, p2);
       -        if(f->cache.n){
       -                puthdr_csl(t, 'i', f->cache.n, p2);
       -                Binsert(t, &f->cache, t->nrunes);
       -                Strzero(&f->cache);
       -        }
       -        f->cp1 = f->cp2 = 0;
       +    Buffer *t = f->transcript;
       +    Posn p1 = f->cp1, p2 = f->cp2;
       +
       +    if(f->state == Readerr)
       +        return;
       +    if(p1 != p2)
       +        puthdr_cll(t, 'd', p1, p2);
       +    if(f->cache.n){
       +        puthdr_csl(t, 'i', f->cache.n, p2);
       +        Binsert(t, &f->cache, t->nrunes);
       +        Strzero(&f->cache);
       +    }
       +    f->cp1 = f->cp2 = 0;
        }
        
        void
        Fsetname(File *f, String *s)
        {
       -        Buffer *t = f->transcript;
       -
       -        if(f->state == Readerr)
       -                return;
       -        if(f->state == Unread){        /* This is setting initial file name */
       -                Strduplstr(&f->name, s);
       -                sortname(f);
       -        }else{
       -                if(f->mod < modnum)
       -                        Fmark(f, modnum);
       -                puthdr_cs(t, 'f', s->n);
       -                Binsert(t, s, t->nrunes);
       -        }
       +    Buffer *t = f->transcript;
       +
       +    if(f->state == Readerr)
       +        return;
       +    if(f->state == Unread){ /* This is setting initial file name */
       +        Strduplstr(&f->name, s);
       +        sortname(f);
       +    }else{
       +        if(f->mod < modnum)
       +            Fmark(f, modnum);
       +        puthdr_cs(t, 'f', s->n);
       +        Binsert(t, s, t->nrunes);
       +    }
        }
        
        /*
       @@ -222,244 +222,244 @@ Fsetname(File *f, String *s)
        int
        Fupdate(File *f, int mktrans, int toterm)
        {
       -        Buffer *t = f->transcript;
       -        Buffer *u = undobuf;
       -        int n, ni;
       -        Posn p0, p1, p2, p, deltadot = 0, deltamark = 0, delta = 0;
       -        int changes = FALSE;
       -        union Hdr buf;
       -        Rune tmp[BLOCKSIZE+1];        /* +1 for NUL in 'f' case */
       -
       -        if(f->state == Readerr)
       -                return FALSE;
       -        if(lastfile && f!=lastfile)
       -                Bclean(lastfile->transcript);        /* save memory when multifile */
       -        lastfile = f;
       -        Fflush(f);
       -        if(f->marked)
       -                p0 = f->markp+sizeof(Mark)/RUNESIZE;
       -        else
       -                p0 = 0;
       -        f->dot = f->ndot;
       -        while((n=Bread(t, (Rune*)&buf, sizeof buf/RUNESIZE, p0)) > 0){
       -                switch(buf.cs.c){
       -                default:
       -                        panic("unknown in Fupdate");
       -                case 'd':
       -                        p1 = buf.cll.l;
       -                        p2 = buf.cll.l1;
       -                        p0 += sizeof(struct _cll)/RUNESIZE;
       -                        if(p2 <= f->dot.r.p1)
       -                                deltadot -= p2-p1;
       -                        if(p2 <= f->mark.p1)
       -                                deltamark -= p2-p1;
       -                        p1 += delta, p2+=delta;
       -                        delta -= p2-p1;
       -                        if(!mktrans)
       -                                for(p = p1; p<p2; p+=ni){
       -                                        if(p2-p>BLOCKSIZE)
       -                                                ni = BLOCKSIZE;
       -                                        else
       -                                                ni = p2-p;
       -                                        puthdr_csl(u, 'i', ni, p1);
       -                                        Bread(f->buf, tmp, ni, p);
       -                                        Binsert(u, ftempstr(tmp, ni), u->nrunes);
       -                                }
       -                        f->nrunes -= p2-p1;
       -                        Bdelete(f->buf, p1, p2);
       -                        changes = TRUE;
       -                        break;
       -
       -                case 'f':
       -                        n = buf.cs.s;
       -                        p0 += sizeof(struct _cs)/RUNESIZE;
       -                        Strinsure(&genstr, n+1);
       -                        Bread(t, tmp, n, p0);
       -                        tmp[n] = 0;
       -                        p0 += n;
       -                        Strdupl(&genstr, tmp);
       -                        if(!mktrans){
       -                                puthdr_cs(u, 'f', f->name.n);
       -                                Binsert(u, &f->name, u->nrunes);
       -                        }
       -                        Strduplstr(&f->name, &genstr);
       -                        sortname(f);
       -                        changes = TRUE;
       -                        break;
       -
       -                case 'i':
       -                        n = buf.csl.s;
       -                        p1 = buf.csl.l;
       -                        p0 += sizeof(struct _csl)/RUNESIZE;
       -                        if(p1 < f->dot.r.p1)
       -                                deltadot += n;
       -                        if(p1 < f->mark.p1)
       -                                deltamark += n;
       -                        p1 += delta;
       -                        delta += n;
       -                        if(!mktrans)
       -                                puthdr_cll(u, 'd', p1, p1+n);
       -                        changes = TRUE;
       -                        f->nrunes += n;
       -                        while(n > 0){
       -                                if(n > BLOCKSIZE)
       -                                        ni = BLOCKSIZE;
       -                                else
       -                                        ni = n;
       -                                Bread(t, tmp, ni, p0);
       -                                Binsert(f->buf, ftempstr(tmp, ni), p1);
       -                                n -= ni;
       -                                p1 += ni;
       -                                p0 += ni;
       -                        }
       -                        break;
       -                }
       -        }
       -        toterminal(f, toterm);
       -        f->dot.r.p1 += deltadot;
       -        f->dot.r.p2 += deltadot;
       -        if(f->dot.r.p1 > f->nrunes)
       -                f->dot.r.p1 = f->nrunes;
       -        if(f->dot.r.p2 > f->nrunes)
       -                f->dot.r.p2 = f->nrunes;
       -        f->mark.p1 += deltamark;
       -        f->mark.p2 += deltamark;
       -        if(f->mark.p1 > f->nrunes)
       -                f->mark.p1 = f->nrunes;
       -        if(f->mark.p2 > f->nrunes)
       -                f->mark.p2 = f->nrunes;
       -        if(n < 0)
       -                panic("Fupdate read");
       -        if(f == cmd)
       -                f->mod = 0;        /* can't undo command file */
       -        if(p0 > f->markp+sizeof(Posn)/RUNESIZE){        /* for undo, this throws away the undo transcript */
       -                if(f->mod > 0){        /* can't undo the dawn of time */
       -                        Bdelete(t, f->markp+sizeof(Mark)/RUNESIZE, t->nrunes);
       -                        /* copy the undo list back into the transcript */
       -                        for(p = 0; p<u->nrunes; p+=ni){
       -                                if(u->nrunes-p>BLOCKSIZE)
       -                                        ni = BLOCKSIZE;
       -                                else
       -                                        ni = u->nrunes-p;
       -                                Bread(u, tmp, ni, p);
       -                                Binsert(t, ftempstr(tmp, ni), t->nrunes);
       -                        }
       -                }
       -                Bdelete(u, (Posn)0, u->nrunes);
       -        }
       -        return f==cmd? FALSE : changes;
       +    Buffer *t = f->transcript;
       +    Buffer *u = undobuf;
       +    int n, ni;
       +    Posn p0, p1, p2, p, deltadot = 0, deltamark = 0, delta = 0;
       +    int changes = FALSE;
       +    union Hdr buf;
       +    Rune tmp[BLOCKSIZE+1];  /* +1 for NUL in 'f' case */
       +
       +    if(f->state == Readerr)
       +        return FALSE;
       +    if(lastfile && f!=lastfile)
       +        Bclean(lastfile->transcript);   /* save memory when multifile */
       +    lastfile = f;
       +    Fflush(f);
       +    if(f->marked)
       +        p0 = f->markp+sizeof(Mark)/RUNESIZE;
       +    else
       +        p0 = 0;
       +    f->dot = f->ndot;
       +    while((n=Bread(t, (Rune*)&buf, sizeof buf/RUNESIZE, p0)) > 0){
       +        switch(buf.cs.c){
       +        default:
       +            panic("unknown in Fupdate");
       +        case 'd':
       +            p1 = buf.cll.l;
       +            p2 = buf.cll.l1;
       +            p0 += sizeof(struct _cll)/RUNESIZE;
       +            if(p2 <= f->dot.r.p1)
       +                deltadot -= p2-p1;
       +            if(p2 <= f->mark.p1)
       +                deltamark -= p2-p1;
       +            p1 += delta, p2+=delta;
       +            delta -= p2-p1;
       +            if(!mktrans)
       +                for(p = p1; p<p2; p+=ni){
       +                    if(p2-p>BLOCKSIZE)
       +                        ni = BLOCKSIZE;
       +                    else
       +                        ni = p2-p;
       +                    puthdr_csl(u, 'i', ni, p1);
       +                    Bread(f->buf, tmp, ni, p);
       +                    Binsert(u, ftempstr(tmp, ni), u->nrunes);
       +                }
       +            f->nrunes -= p2-p1;
       +            Bdelete(f->buf, p1, p2);
       +            changes = TRUE;
       +            break;
       +
       +        case 'f':
       +            n = buf.cs.s;
       +            p0 += sizeof(struct _cs)/RUNESIZE;
       +            Strinsure(&genstr, n+1);
       +            Bread(t, tmp, n, p0);
       +            tmp[n] = 0;
       +            p0 += n;
       +            Strdupl(&genstr, tmp);
       +            if(!mktrans){
       +                puthdr_cs(u, 'f', f->name.n);
       +                Binsert(u, &f->name, u->nrunes);
       +            }
       +            Strduplstr(&f->name, &genstr);
       +            sortname(f);
       +            changes = TRUE;
       +            break;
       +
       +        case 'i':
       +            n = buf.csl.s;
       +            p1 = buf.csl.l;
       +            p0 += sizeof(struct _csl)/RUNESIZE;
       +            if(p1 < f->dot.r.p1)
       +                deltadot += n;
       +            if(p1 < f->mark.p1)
       +                deltamark += n;
       +            p1 += delta;
       +            delta += n;
       +            if(!mktrans)
       +                puthdr_cll(u, 'd', p1, p1+n);
       +            changes = TRUE;
       +            f->nrunes += n;
       +            while(n > 0){
       +                if(n > BLOCKSIZE)
       +                    ni = BLOCKSIZE;
       +                else
       +                    ni = n;
       +                Bread(t, tmp, ni, p0);
       +                Binsert(f->buf, ftempstr(tmp, ni), p1);
       +                n -= ni;
       +                p1 += ni;
       +                p0 += ni;
       +            }
       +            break;
       +        }
       +    }
       +    toterminal(f, toterm);
       +    f->dot.r.p1 += deltadot;
       +    f->dot.r.p2 += deltadot;
       +    if(f->dot.r.p1 > f->nrunes)
       +        f->dot.r.p1 = f->nrunes;
       +    if(f->dot.r.p2 > f->nrunes)
       +        f->dot.r.p2 = f->nrunes;
       +    f->mark.p1 += deltamark;
       +    f->mark.p2 += deltamark;
       +    if(f->mark.p1 > f->nrunes)
       +        f->mark.p1 = f->nrunes;
       +    if(f->mark.p2 > f->nrunes)
       +        f->mark.p2 = f->nrunes;
       +    if(n < 0)
       +        panic("Fupdate read");
       +    if(f == cmd)
       +        f->mod = 0; /* can't undo command file */
       +    if(p0 > f->markp+sizeof(Posn)/RUNESIZE){    /* for undo, this throws away the undo transcript */
       +        if(f->mod > 0){ /* can't undo the dawn of time */
       +            Bdelete(t, f->markp+sizeof(Mark)/RUNESIZE, t->nrunes);
       +            /* copy the undo list back into the transcript */
       +            for(p = 0; p<u->nrunes; p+=ni){
       +                if(u->nrunes-p>BLOCKSIZE)
       +                    ni = BLOCKSIZE;
       +                else
       +                    ni = u->nrunes-p;
       +                Bread(u, tmp, ni, p);
       +                Binsert(t, ftempstr(tmp, ni), t->nrunes);
       +            }
       +        }
       +        Bdelete(u, (Posn)0, u->nrunes);
       +    }
       +    return f==cmd? FALSE : changes;
        }
        
        void
        puthdr_csl(Buffer *b, char c, short s, Posn p)
        {
       -        struct _csl buf;
       -
       -        if(p < 0)
       -                panic("puthdr_csP");
       -        buf.c = c;
       -        buf.s = s;
       -        buf.l = p;
       -        Binsert(b, ftempstr((Rune*)&buf, sizeof buf/RUNESIZE), b->nrunes);
       +    struct _csl buf;
       +
       +    if(p < 0)
       +        panic("puthdr_csP");
       +    buf.c = c;
       +    buf.s = s;
       +    buf.l = p;
       +    Binsert(b, ftempstr((Rune*)&buf, sizeof buf/RUNESIZE), b->nrunes);
        }
        
        void
        puthdr_cs(Buffer *b, char c, short s)
        {
       -        struct _cs buf;
       +    struct _cs buf;
        
       -        buf.c = c;
       -        buf.s = s;
       -        Binsert(b, ftempstr((Rune*)&buf, sizeof buf/RUNESIZE), b->nrunes);
       +    buf.c = c;
       +    buf.s = s;
       +    Binsert(b, ftempstr((Rune*)&buf, sizeof buf/RUNESIZE), b->nrunes);
        }
        
        void
        puthdr_M(Buffer *b, Posn p, Range dot, Range mk, Mod m, short s1)
        {
       -        Mark mark;
       -        static int first = 1;
       -
       -        if(!first && p<0)
       -                panic("puthdr_M");
       -        mark.p = p;
       -        mark.dot = dot;
       -        mark.mark = mk;
       -        mark.m = m;
       -        mark.s1 = s1;
       -        Binsert(b, ftempstr((Rune *)&mark, sizeof mark/RUNESIZE), b->nrunes);
       +    Mark mark;
       +    static int first = 1;
       +
       +    if(!first && p<0)
       +        panic("puthdr_M");
       +    mark.p = p;
       +    mark.dot = dot;
       +    mark.mark = mk;
       +    mark.m = m;
       +    mark.s1 = s1;
       +    Binsert(b, ftempstr((Rune *)&mark, sizeof mark/RUNESIZE), b->nrunes);
        }
        
        void
        puthdr_cll(Buffer *b, char c, Posn p1, Posn p2)
        {
       -        struct _cll buf;
       -
       -        if(p1<0 || p2<0)
       -                panic("puthdr_cll");
       -        buf.c = c;
       -        buf.l = p1;
       -        buf.l1 = p2;
       -        Binsert(b, ftempstr((Rune*)&buf, sizeof buf/RUNESIZE), b->nrunes);
       +    struct _cll buf;
       +
       +    if(p1<0 || p2<0)
       +        panic("puthdr_cll");
       +    buf.c = c;
       +    buf.l = p1;
       +    buf.l1 = p2;
       +    Binsert(b, ftempstr((Rune*)&buf, sizeof buf/RUNESIZE), b->nrunes);
        }
        
        long
        Fchars(File *f, Rune *addr, Posn p1, Posn p2)
        {
       -        return Bread(f->buf, addr, p2-p1, p1);
       +    return Bread(f->buf, addr, p2-p1, p1);
        }
        
        int
        Fgetcset(File *f, Posn p)
        {
       -        if(p<0 || p>f->nrunes)
       -                panic("Fgetcset out of range");
       -        if((f->ngetc = Fchars(f, f->getcbuf, p, p+NGETC))<0)
       -                panic("Fgetcset Bread fail");
       -        f->getcp = p;
       -        f->getci = 0;
       -        return f->ngetc;
       +    if(p<0 || p>f->nrunes)
       +        panic("Fgetcset out of range");
       +    if((f->ngetc = Fchars(f, f->getcbuf, p, p+NGETC))<0)
       +        panic("Fgetcset Bread fail");
       +    f->getcp = p;
       +    f->getci = 0;
       +    return f->ngetc;
        }
        
        int
        Fbgetcset(File *f, Posn p)
        {
       -        if(p<0 || p>f->nrunes)
       -                panic("Fbgetcset out of range");
       -        if((f->ngetc = Fchars(f, f->getcbuf, p<NGETC? (Posn)0 : p-NGETC, p))<0)
       -                panic("Fbgetcset Bread fail");
       -        f->getcp = p;
       -        f->getci = f->ngetc;
       -        return f->ngetc;
       +    if(p<0 || p>f->nrunes)
       +        panic("Fbgetcset out of range");
       +    if((f->ngetc = Fchars(f, f->getcbuf, p<NGETC? (Posn)0 : p-NGETC, p))<0)
       +        panic("Fbgetcset Bread fail");
       +    f->getcp = p;
       +    f->getci = f->ngetc;
       +    return f->ngetc;
        }
        
        int
        Fgetcload(File *f, Posn p)
        {
       -        if(Fgetcset(f, p)){
       -                --f->ngetc;
       -                f->getcp++;
       -                return f->getcbuf[f->getci++];
       -        }
       -        return -1;
       +    if(Fgetcset(f, p)){
       +        --f->ngetc;
       +        f->getcp++;
       +        return f->getcbuf[f->getci++];
       +    }
       +    return -1;
        }
        
        int
        Fbgetcload(File *f, Posn p)
        {
       -        if(Fbgetcset(f, p)){
       -                --f->getcp;
       -                return f->getcbuf[--f->getci];
       -        }
       -        return -1;
       +    if(Fbgetcset(f, p)){
       +        --f->getcp;
       +        return f->getcbuf[--f->getci];
       +    }
       +    return -1;
        }
        
        static String*
        ftempstr(Rune *s, int n)
        {
       -        static String p;
       +    static String p;
        
       -        p.s = s;
       -        p.n = n;
       -        p.size = n;
       -        return &p;
       +    p.s = s;
       +    p.n = n;
       +    p.size = n;
       +    return &p;
        }
 (DIR) diff --git a/sam/io.c b/sam/io.c
       @@ -1,257 +1,257 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
        #include "sam.h"
        
       -#define        NSYSFILE        3
       -#define        NOFILE                128
       +#define NSYSFILE    3
       +#define NOFILE      128
        
        void
        checkqid(File *f)
        {
       -        int i, w;
       -        File *g;
       +    int i, w;
       +    File *g;
        
       -        w = whichmenu(f);
       -        for(i=1; i<file.nused; i++){
       -                g = file.filepptr[i];
       -                if(w == i)
       -                        continue;
       -                if(f->dev==g->dev && f->qid==g->qid)
       -                        warn_SS(Wdupfile, &f->name, &g->name);
       -        }
       +    w = whichmenu(f);
       +    for(i=1; i<file.nused; i++){
       +        g = file.filepptr[i];
       +        if(w == i)
       +            continue;
       +        if(f->dev==g->dev && f->qid==g->qid)
       +            warn_SS(Wdupfile, &f->name, &g->name);
       +    }
        }
        
        void
        writef(File *f)
        {
       -        Rune c;
       -        Posn n;
       -        char *name;
       -        int i, samename, newfile;
       -        ulong dev, qid;
       -        long mtime, appendonly, length;
       +    Rune c;
       +    Posn n;
       +    char *name;
       +    int i, samename, newfile;
       +    ulong dev, qid;
       +    long mtime, appendonly, length;
        
       -        newfile = 0;
       -        samename = Strcmp(&genstr, &f->name) == 0;
       -        name = Strtoc(&f->name);
       -        i = statfile(name, &dev, &qid, &mtime, 0, 0);
       -        if(i == -1)
       -                newfile++;
       -        else if(samename &&
       -                (f->dev!=dev || f->qid!=qid || f->date<mtime)){
       -                f->dev = dev;
       -                f->qid = qid;
       -                f->date = mtime;
       -                warn_S(Wdate, &genstr);
       -                return;
       -        }
       -        if(genc)
       -                free(genc);
       -        genc = Strtoc(&genstr);
       -        if((io=create(genc, 1, 0666L)) < 0)
       -                error_s(Ecreate, genc);
       -        dprint("%s: ", genc);
       -        if(statfd(io, 0, 0, 0, &length, &appendonly) > 0 && appendonly && length>0)
       -                error(Eappend);
       -        n = writeio(f);
       -        if(f->name.s[0]==0 || samename)
       -                state(f, addr.r.p1==0 && addr.r.p2==f->nrunes? Clean : Dirty);
       -        if(newfile)
       -                dprint("(new file) ");
       -        if(addr.r.p2>0 && Fchars(f, &c, addr.r.p2-1, addr.r.p2) && c!='\n')
       -                warn(Wnotnewline);
       -        closeio(n);
       -        if(f->name.s[0]==0 || samename){
       -                if(statfile(name, &dev, &qid, &mtime, 0, 0) > 0){
       -                        f->dev = dev;
       -                        f->qid = qid;
       -                        f->date = mtime;
       -                        checkqid(f);
       -                }
       -        }
       +    newfile = 0;
       +    samename = Strcmp(&genstr, &f->name) == 0;
       +    name = Strtoc(&f->name);
       +    i = statfile(name, &dev, &qid, &mtime, 0, 0);
       +    if(i == -1)
       +        newfile++;
       +    else if(samename &&
       +            (f->dev!=dev || f->qid!=qid || f->date<mtime)){
       +        f->dev = dev;
       +        f->qid = qid;
       +        f->date = mtime;
       +        warn_S(Wdate, &genstr);
       +        return;
       +    }
       +    if(genc)
       +        free(genc);
       +    genc = Strtoc(&genstr);
       +    if((io=create(genc, 1, 0666L)) < 0)
       +        error_s(Ecreate, genc);
       +    dprint("%s: ", genc);
       +    if(statfd(io, 0, 0, 0, &length, &appendonly) > 0 && appendonly && length>0)
       +        error(Eappend);
       +    n = writeio(f);
       +    if(f->name.s[0]==0 || samename)
       +        state(f, addr.r.p1==0 && addr.r.p2==f->nrunes? Clean : Dirty);
       +    if(newfile)
       +        dprint("(new file) ");
       +    if(addr.r.p2>0 && Fchars(f, &c, addr.r.p2-1, addr.r.p2) && c!='\n')
       +        warn(Wnotnewline);
       +    closeio(n);
       +    if(f->name.s[0]==0 || samename){
       +        if(statfile(name, &dev, &qid, &mtime, 0, 0) > 0){
       +            f->dev = dev;
       +            f->qid = qid;
       +            f->date = mtime;
       +            checkqid(f);
       +        }
       +    }
        }
        
        Posn
        readio(File *f, int *nulls, int setdate)
        {
       -        int n, b, w;
       -        Rune *r;
       -        Posn nt;
       -        Posn p = addr.r.p2;
       -        ulong dev, qid;
       -        long mtime;
       -        char buf[BLOCKSIZE+1], *s;
       +    int n, b, w;
       +    Rune *r;
       +    Posn nt;
       +    Posn p = addr.r.p2;
       +    ulong dev, qid;
       +    long mtime;
       +    char buf[BLOCKSIZE+1], *s;
        
       -        *nulls = FALSE;
       -        b = 0;
       -        for(nt = 0; (n = read(io, buf+b, BLOCKSIZE-b))>0; nt+=(r-genbuf)){
       -                n += b;
       -                b = 0;
       -                r = genbuf;
       -                s = buf;
       -                while(n > 0){
       -                        if((*r = *(uchar*)s) < Runeself){
       -                                if(*r)
       -                                        r++;
       -                                else
       -                                        *nulls = TRUE;
       -                                --n;
       -                                s++;
       -                                continue;
       -                        }
       -                        if(fullrune(s, n)){
       -                                w = chartorune(r, s);
       -                                if(*r)
       -                                        r++;
       -                                else
       -                                        *nulls = TRUE;
       -                                n -= w;
       -                                s += w;
       -                                continue;
       -                        }
       -                        b = n;
       -                        memmove(buf, s, b);
       -                        break;
       -                }
       -                Finsert(f, tmprstr(genbuf, r-genbuf), p);
       -        }
       -        if(b)
       -                *nulls = TRUE;
       -        if(*nulls)
       -                warn(Wnulls);
       -        if(setdate){
       -                if(statfd(io, &dev, &qid, &mtime, 0, 0) > 0){
       -                        f->dev = dev;
       -                        f->qid = qid;
       -                        f->date = mtime;
       -                        checkqid(f);
       -                }
       -        }
       -        return nt;
       +    *nulls = FALSE;
       +    b = 0;
       +    for(nt = 0; (n = read(io, buf+b, BLOCKSIZE-b))>0; nt+=(r-genbuf)){
       +        n += b;
       +        b = 0;
       +        r = genbuf;
       +        s = buf;
       +        while(n > 0){
       +            if((*r = *(uchar*)s) < Runeself){
       +                if(*r)
       +                    r++;
       +                else
       +                    *nulls = TRUE;
       +                --n;
       +                s++;
       +                continue;
       +            }
       +            if(fullrune(s, n)){
       +                w = chartorune(r, s);
       +                if(*r)
       +                    r++;
       +                else
       +                    *nulls = TRUE;
       +                n -= w;
       +                s += w;
       +                continue;
       +            }
       +            b = n;
       +            memmove(buf, s, b);
       +            break;
       +        }
       +        Finsert(f, tmprstr(genbuf, r-genbuf), p);
       +    }
       +    if(b)
       +        *nulls = TRUE;
       +    if(*nulls)
       +        warn(Wnulls);
       +    if(setdate){
       +        if(statfd(io, &dev, &qid, &mtime, 0, 0) > 0){
       +            f->dev = dev;
       +            f->qid = qid;
       +            f->date = mtime;
       +            checkqid(f);
       +        }
       +    }
       +    return nt;
        }
        
        Posn
        writeio(File *f)
        {
       -        int m, n;
       -        Posn p = addr.r.p1;
       -        char *c;
       +    int m, n;
       +    Posn p = addr.r.p1;
       +    char *c;
        
       -        while(p < addr.r.p2){
       -                if(addr.r.p2-p>BLOCKSIZE)
       -                        n = BLOCKSIZE;
       -                else
       -                        n = addr.r.p2-p;
       -                if(Fchars(f, genbuf, p, p+n)!=n)
       -                        panic("writef read");
       -                c = Strtoc(tmprstr(genbuf, n));
       -                m = strlen(c);
       -                if (m < n)
       -                        panic("corrupted file");
       -                if(Write(io, c, m) != m){
       -                        free(c);
       -                        if(p > 0)
       -                                p += n;
       -                        break;
       -                }
       -                free(c);
       -                p += n;
       -        }
       -        return p-addr.r.p1;
       +    while(p < addr.r.p2){
       +        if(addr.r.p2-p>BLOCKSIZE)
       +            n = BLOCKSIZE;
       +        else
       +            n = addr.r.p2-p;
       +        if(Fchars(f, genbuf, p, p+n)!=n)
       +            panic("writef read");
       +        c = Strtoc(tmprstr(genbuf, n));
       +        m = strlen(c);
       +        if (m < n)
       +            panic("corrupted file");
       +        if(Write(io, c, m) != m){
       +            free(c);
       +            if(p > 0)
       +                p += n;
       +            break;
       +        }
       +        free(c);
       +        p += n;
       +    }
       +    return p-addr.r.p1;
        }
        void
        closeio(Posn p)
        {
       -        close(io);
       -        io = 0;
       -        if(p >= 0)
       -                dprint("#%lu\n", p);
       +    close(io);
       +    io = 0;
       +    if(p >= 0)
       +        dprint("#%lu\n", p);
        }
        
       -int        remotefd0 = 0;
       -int        remotefd1 = 1;
       +int remotefd0 = 0;
       +int remotefd1 = 1;
        
        void
        bootterm(char *machine, char **argv, char **end)
        {
       -        int ph2t[2], pt2h[2];
       +    int ph2t[2], pt2h[2];
        
       -        if(machine){
       -                dup(remotefd0, 0);
       -                dup(remotefd1, 1);
       -                close(remotefd0);
       -                close(remotefd1);
       -                argv[0] = "samterm";
       -                *end = 0;
       -                execvp(samterm, argv);
       -                fprintf(stderr, "can't exec: ");
       -                perror(samterm);
       -                _exits("damn");
       -        }
       -        if(pipe(ph2t)==-1 || pipe(pt2h)==-1)
       -                panic("pipe");
       -        switch(fork()){
       -        case 0:
       -                dup(ph2t[0], 0);
       -                dup(pt2h[1], 1);
       -                close(ph2t[0]);
       -                close(ph2t[1]);
       -                close(pt2h[0]);
       -                close(pt2h[1]);
       -                argv[0] = "samterm";
       -                *end = 0;
       -                execvp(samterm, argv);
       -                fprintf(stderr, "can't exec: ");
       -                perror(samterm);
       -                _exits("damn");
       -        case -1:
       -                panic("can't fork samterm");
       -        }
       -        dup(pt2h[0], 0);
       -        dup(ph2t[1], 1);
       -        close(ph2t[0]);
       -        close(ph2t[1]);
       -        close(pt2h[0]);
       -        close(pt2h[1]);
       +    if(machine){
       +        dup(remotefd0, 0);
       +        dup(remotefd1, 1);
       +        close(remotefd0);
       +        close(remotefd1);
       +        argv[0] = "samterm";
       +        *end = 0;
       +        execvp(samterm, argv);
       +        fprintf(stderr, "can't exec: ");
       +        perror(samterm);
       +        _exits("damn");
       +    }
       +    if(pipe(ph2t)==-1 || pipe(pt2h)==-1)
       +        panic("pipe");
       +    switch(fork()){
       +    case 0:
       +        dup(ph2t[0], 0);
       +        dup(pt2h[1], 1);
       +        close(ph2t[0]);
       +        close(ph2t[1]);
       +        close(pt2h[0]);
       +        close(pt2h[1]);
       +        argv[0] = "samterm";
       +        *end = 0;
       +        execvp(samterm, argv);
       +        fprintf(stderr, "can't exec: ");
       +        perror(samterm);
       +        _exits("damn");
       +    case -1:
       +        panic("can't fork samterm");
       +    }
       +    dup(pt2h[0], 0);
       +    dup(ph2t[1], 1);
       +    close(ph2t[0]);
       +    close(ph2t[1]);
       +    close(pt2h[0]);
       +    close(pt2h[1]);
        }
        
        void
        connectto(char *machine)
        {
       -        int p1[2], p2[2];
       +    int p1[2], p2[2];
        
       -        if(pipe(p1)<0 || pipe(p2)<0){
       -                dprint("can't pipe\n");
       -                exits("pipe");
       -        }
       -        remotefd0 = p1[0];
       -        remotefd1 = p2[1];
       -        switch(fork()){
       -        case 0:
       -                dup(p2[0], 0);
       -                dup(p1[1], 1);
       -                close(p1[0]);
       -                close(p1[1]);
       -                close(p2[0]);
       -                close(p2[1]);
       -                execlp(getenv("RSH") ? getenv("RSH") : RXPATH, getenv("RSH") ? getenv("RSH") : RXPATH, machine, rsamname, "-R", (char*)0);
       -                dprint("can't exec %s\n", RXPATH);
       -                exits("exec");
       +    if(pipe(p1)<0 || pipe(p2)<0){
       +        dprint("can't pipe\n");
       +        exits("pipe");
       +    }
       +    remotefd0 = p1[0];
       +    remotefd1 = p2[1];
       +    switch(fork()){
       +    case 0:
       +        dup(p2[0], 0);
       +        dup(p1[1], 1);
       +        close(p1[0]);
       +        close(p1[1]);
       +        close(p2[0]);
       +        close(p2[1]);
       +        execlp(getenv("RSH") ? getenv("RSH") : RXPATH, getenv("RSH") ? getenv("RSH") : RXPATH, machine, rsamname, "-R", (char*)0);
       +        dprint("can't exec %s\n", RXPATH);
       +        exits("exec");
        
       -        case -1:
       -                dprint("can't fork\n");
       -                exits("fork");
       -        }
       -        close(p1[1]);
       -        close(p2[0]);
       +    case -1:
       +        dprint("can't fork\n");
       +        exits("fork");
       +    }
       +    close(p1[1]);
       +    close(p2[0]);
        }
        
        void
        startup(char *machine, int Rflag, char **arg, char **end)
        {
       -        if(machine)
       -                connectto(machine);
       -        if(!Rflag)
       -                bootterm(machine, arg, end);
       -        downloaded = 1;
       -        outTs(Hversion, VERSION);
       +    if(machine)
       +        connectto(machine);
       +    if(!Rflag)
       +        bootterm(machine, arg, end);
       +    downloaded = 1;
       +    outTs(Hversion, VERSION);
        }
 (DIR) diff --git a/sam/list.c b/sam/list.c
       @@ -7,15 +7,15 @@
        void
        growlist(List *l)
        {
       -        if(l->listptr==0 || l->nalloc==0){
       -                l->nalloc = INCR;
       -                l->listptr = emalloc(INCR*sizeof(long));
       -                l->nused = 0;
       -        }else if(l->nused == l->nalloc){
       -                l->listptr = erealloc(l->listptr, (l->nalloc+INCR)*sizeof(long));
       -                memset((void*)(l->longptr+l->nalloc), 0, INCR*sizeof(long));
       -                l->nalloc += INCR;
       -        }
       +    if(l->listptr==0 || l->nalloc==0){
       +        l->nalloc = INCR;
       +        l->listptr = emalloc(INCR*sizeof(long));
       +        l->nused = 0;
       +    }else if(l->nused == l->nalloc){
       +        l->listptr = erealloc(l->listptr, (l->nalloc+INCR)*sizeof(long));
       +        memset((void*)(l->longptr+l->nalloc), 0, INCR*sizeof(long));
       +        l->nalloc += INCR;
       +    }
        }
        
        /*
       @@ -24,8 +24,8 @@ growlist(List *l)
        void
        dellist(List *l, int i)
        {
       -        memmove(&l->longptr[i], &l->longptr[i+1], (l->nused-(i+1))*sizeof(long));
       -        l->nused--;
       +    memmove(&l->longptr[i], &l->longptr[i+1], (l->nused-(i+1))*sizeof(long));
       +    l->nused--;
        }
        
        /*
       @@ -34,15 +34,15 @@ dellist(List *l, int i)
        void
        inslist(List *l, int i, long val)
        {
       -        growlist(l);
       -        memmove(&l->longptr[i+1], &l->longptr[i], (l->nused-i)*sizeof(long));
       -        l->longptr[i] = val;
       -        l->nused++;
       +    growlist(l);
       +    memmove(&l->longptr[i+1], &l->longptr[i], (l->nused-i)*sizeof(long));
       +    l->longptr[i] = val;
       +    l->nused++;
        }
        
        void
        listfree(List *l)
        {
       -        free(l->listptr);
       -        free(l);
       +    free(l->listptr);
       +    free(l);
        }
 (DIR) diff --git a/sam/mesg.c b/sam/mesg.c
       @@ -2,24 +2,24 @@
        #include "sam.h"
        #include "../config.h"
        
       -Header        h;
       -uchar        indata[DATASIZE];
       -uchar        outdata[2*DATASIZE+3];        /* room for overflow message */
       -uchar        *inp;
       -uchar        *outp;
       -uchar        *outmsg = outdata;
       -Posn        cmdpt;
       -Posn        cmdptadv;
       -Buffer        *snarfbuf;
       -int        waitack;
       -int        noflush;
       -int        tversion;
       -
       -long        inlong(void);
       -long        invlong(void);
       -int        inshort(void);
       -int        inmesg(Tmesg);
       -void        setgenstr(File*, Posn, Posn);
       +Header  h;
       +uchar   indata[DATASIZE];
       +uchar   outdata[2*DATASIZE+3];  /* room for overflow message */
       +uchar   *inp;
       +uchar   *outp;
       +uchar   *outmsg = outdata;
       +Posn    cmdpt;
       +Posn    cmdptadv;
       +Buffer  *snarfbuf;
       +int waitack;
       +int noflush;
       +int tversion;
       +
       +long    inlong(void);
       +long    invlong(void);
       +int inshort(void);
       +int inmesg(Tmesg);
       +void    setgenstr(File*, Posn, Posn);
        
        #ifdef CLASSIC_SAM_COMPATIBILITY
        int oldcompat = 1;
       @@ -29,747 +29,747 @@ int oldcompat = 0;
        
        #ifdef DEBUG
        char *hname[] = {
       -        [Hversion]        "Hversion",
       -        [Hbindname]        "Hbindname",
       -        [Hcurrent]        "Hcurrent",
       -        [Hnewname]        "Hnewname",
       -        [Hmovname]        "Hmovname",
       -        [Hgrow]                "Hgrow",
       -        [Hcheck0]        "Hcheck0",
       -        [Hcheck]        "Hcheck",
       -        [Hunlock]        "Hunlock",
       -        [Hdata]                "Hdata",
       -        [Horigin]        "Horigin",
       -        [Hunlockfile]        "Hunlockfile",
       -        [Hsetdot]        "Hsetdot",
       -        [Hgrowdata]        "Hgrowdata",
       -        [Hmoveto]        "Hmoveto",
       -        [Hclean]        "Hclean",
       -        [Hdirty]        "Hdirty",
       -        [Hcut]                "Hcut",
       -        [Hsetpat]        "Hsetpat",
       -        [Hdelname]        "Hdelname",
       -        [Hclose]        "Hclose",
       -        [Hsetsnarf]        "Hsetsnarf",
       -        [Hsnarflen]        "Hsnarflen",
       -        [Hack]                "Hack",
       -        [Hextcmd]   "Hextcmd",
       -        [Hexit]                "Hexit",
       +    [Hversion]  "Hversion",
       +    [Hbindname] "Hbindname",
       +    [Hcurrent]  "Hcurrent",
       +    [Hnewname]  "Hnewname",
       +    [Hmovname]  "Hmovname",
       +    [Hgrow]     "Hgrow",
       +    [Hcheck0]   "Hcheck0",
       +    [Hcheck]    "Hcheck",
       +    [Hunlock]   "Hunlock",
       +    [Hdata]     "Hdata",
       +    [Horigin]   "Horigin",
       +    [Hunlockfile]   "Hunlockfile",
       +    [Hsetdot]   "Hsetdot",
       +    [Hgrowdata] "Hgrowdata",
       +    [Hmoveto]   "Hmoveto",
       +    [Hclean]    "Hclean",
       +    [Hdirty]    "Hdirty",
       +    [Hcut]      "Hcut",
       +    [Hsetpat]   "Hsetpat",
       +    [Hdelname]  "Hdelname",
       +    [Hclose]    "Hclose",
       +    [Hsetsnarf] "Hsetsnarf",
       +    [Hsnarflen] "Hsnarflen",
       +    [Hack]      "Hack",
       +    [Hextcmd]   "Hextcmd",
       +    [Hexit]     "Hexit",
        };
        
        char *tname[] = {
       -        [Tversion]        "Tversion",
       -        [Tstartcmdfile]        "Tstartcmdfile",
       -        [Tcheck]        "Tcheck",
       -        [Trequest]        "Trequest",
       -        [Torigin]        "Torigin",
       -        [Tstartfile]        "Tstartfile",
       -        [Tworkfile]        "Tworkfile",
       -        [Ttype]                "Ttype",
       -        [Tcut]                "Tcut",
       -        [Tpaste]        "Tpaste",
       -        [Tsnarf]        "Tsnarf",
       -        [Tstartnewfile]        "Tstartnewfile",
       -        [Twrite]        "Twrite",
       -        [Tclose]        "Tclose",
       -        [Tlook]                "Tlook",
       -        [Tsearch]        "Tsearch",
       -        [Tsend]                "Tsend",
       -        [Tdclick]        "Tdclick",
       -        [Tstartsnarf]        "Tstartsnarf",
       -        [Tsetsnarf]        "Tsetsnarf",
       -        [Tack]                "Tack",
       -        [Texit]                "Texit",
       +    [Tversion]  "Tversion",
       +    [Tstartcmdfile] "Tstartcmdfile",
       +    [Tcheck]    "Tcheck",
       +    [Trequest]  "Trequest",
       +    [Torigin]   "Torigin",
       +    [Tstartfile]    "Tstartfile",
       +    [Tworkfile] "Tworkfile",
       +    [Ttype]     "Ttype",
       +    [Tcut]      "Tcut",
       +    [Tpaste]    "Tpaste",
       +    [Tsnarf]    "Tsnarf",
       +    [Tstartnewfile] "Tstartnewfile",
       +    [Twrite]    "Twrite",
       +    [Tclose]    "Tclose",
       +    [Tlook]     "Tlook",
       +    [Tsearch]   "Tsearch",
       +    [Tsend]     "Tsend",
       +    [Tdclick]   "Tdclick",
       +    [Tstartsnarf]   "Tstartsnarf",
       +    [Tsetsnarf] "Tsetsnarf",
       +    [Tack]      "Tack",
       +    [Texit]     "Texit",
        };
        
        void
        journal(int out, char *s)
        {
       -        static int fd = 0;
       +    static int fd = 0;
        
       -        if(fd <= 0)
       -                fd = create("/tmp/sam.out", 1, 0666L);
       -        dprintf(fd, "%s%s\n", out? "out: " : "in:  ", s);
       +    if(fd <= 0)
       +        fd = create("/tmp/sam.out", 1, 0666L);
       +    dprintf(fd, "%s%s\n", out? "out: " : "in:  ", s);
        }
        
        void
        journaln(int out, long n)
        {
       -        char buf[32];
       -        sprint(buf, sizeof (long) > 4 ? "%ld" : "%d", n);
       -        journal(out, buf);
       +    char buf[32];
       +    sprint(buf, sizeof (long) > 4 ? "%ld" : "%d", n);
       +    journal(out, buf);
        }
        #else
       -#define        journal(a, b)
       +#define journal(a, b)
        #define journaln(a, b)
        #endif
        
        int
        rcvchar(void){
       -        static uchar buf[64];
       -        static int i, nleft = 0;
       -
       -        if(nleft <= 0){
       -                nleft = read(0, (char *)buf, sizeof buf);
       -                if(nleft <= 0)
       -                        return -1;
       -                i = 0;
       -        }
       -        --nleft;
       -        return buf[i++];
       +    static uchar buf[64];
       +    static int i, nleft = 0;
       +
       +    if(nleft <= 0){
       +        nleft = read(0, (char *)buf, sizeof buf);
       +        if(nleft <= 0)
       +            return -1;
       +        i = 0;
       +    }
       +    --nleft;
       +    return buf[i++];
        }
        
        int
        rcv(void){
       -        int c;
       -        static int state = 0;
       -        static int count = 0;
       -        static int i = 0;
       -
       -        while((c=rcvchar()) != -1)
       -                switch(state){
       -                case 0:
       -                        h.type = c;
       -                        state++;
       -                        break;
       -
       -                case 1:
       -                        h.count0 = c;
       -                        state++;
       -                        break;
       -
       -                case 2:
       -                        h.count1 = c;
       -                        count = h.count0|(h.count1<<8);
       -                        i = 0;
       -                        if(count > DATASIZE)
       -                                panic("count>DATASIZE");
       -                        if(count == 0)
       -                                goto zerocount;
       -                        state++;
       -                        break;
       -
       -                case 3:
       -                        indata[i++] = c;
       -                        if(i == count){
       -                zerocount:
       -                                indata[i] = 0;
       -                                state = count = 0;
       -                                return inmesg(h.type);
       -                        }
       -                        break;
       -                }
       -        return 0;
       +    int c;
       +    static int state = 0;
       +    static int count = 0;
       +    static int i = 0;
       +
       +    while((c=rcvchar()) != -1)
       +        switch(state){
       +        case 0:
       +            h.type = c;
       +            state++;
       +            break;
       +
       +        case 1:
       +            h.count0 = c;
       +            state++;
       +            break;
       +
       +        case 2:
       +            h.count1 = c;
       +            count = h.count0|(h.count1<<8);
       +            i = 0;
       +            if(count > DATASIZE)
       +                panic("count>DATASIZE");
       +            if(count == 0)
       +                goto zerocount;
       +            state++;
       +            break;
       +
       +        case 3:
       +            indata[i++] = c;
       +            if(i == count){
       +        zerocount:
       +                indata[i] = 0;
       +                state = count = 0;
       +                return inmesg(h.type);
       +            }
       +            break;
       +        }
       +    return 0;
        }
        
        File *
        whichfile(int tag)
        {
       -        int i;
       +    int i;
        
       -        for(i = 0; i<file.nused; i++)
       -                if(file.filepptr[i]->tag==tag)
       -                        return file.filepptr[i];
       -        hiccough((char *)0);
       -        return 0;
       +    for(i = 0; i<file.nused; i++)
       +        if(file.filepptr[i]->tag==tag)
       +            return file.filepptr[i];
       +    hiccough((char *)0);
       +    return 0;
        }
        
        int
        inmesg(Tmesg type)
        {
       -        Rune buf[1025];
       -        int i, m;
       -        short s;
       -        long l, l1, l2;
       -        File *f;
       -        Posn p0, p1;
       -        Range r;
       -        String *str;
       -        char *c;
       -        Rune *rp;
       -
       -        if(type > TMAX)
       -                panic("inmesg");
       -
       -        journal(0, tname[type]);
       -
       -        inp = indata;
       -        switch(type){
       -        case -1:
       -                panic("rcv error");
       -
       -        default:
       -                fprintf(stderr, "unknown type %d\n", type);
       -                panic("rcv unknown");
       -
       -        case Tversion:
       -                tversion = inshort();
       -                journaln(0, tversion);
       -                break;
       -
       -        case Tstartcmdfile:
       -                l = invlong();                /* for 64-bit pointers */
       -                journaln(0, l);
       -                Strdupl(&genstr, samname);
       -                cmd = newfile();
       -                outTsv(Hbindname, cmd->tag, l);
       -                outTs(Hcurrent, cmd->tag);
       -                Fsetname(cmd, &genstr);
       -                cmd->rasp = emalloc(sizeof(List));
       -                cmd->state = Clean;
       -                if(cmdstr.n){
       -                        Finsert(cmd, &cmdstr, 0L);
       -                        Strdelete(&cmdstr, 0L, (Posn)cmdstr.n);
       -                }
       -                Fupdate(cmd, FALSE, TRUE);
       -                outT0(Hunlock);
       -                break;
       -
       -        case Tcheck:
       -                /* go through whichfile to check the tag */
       -                outTs(Hcheck, whichfile(inshort())->tag);
       -                break;
       -
       -        case Trequest:
       -                f = whichfile(inshort());
       -                p0 = inlong();
       -                p1 = p0+inshort();
       -                journaln(0, p0);
       -                journaln(0, p1-p0);
       -                if(f->state == Unread)
       -                        panic("Trequest: unread");
       -                if(p1>f->nrunes)
       -                        p1 = f->nrunes;
       -                if(p0>f->nrunes) /* can happen e.g. scrolling during command */
       -                        p0 = f->nrunes;
       -                if(p0 == p1){
       -                        i = 0;
       -                        r.p1 = r.p2 = p0;
       -                }else{
       -                        r = rdata(f->rasp, p0, p1-p0);
       -                        i = r.p2-r.p1;
       -                        if(Fchars(f, buf, r.p1, r.p2)!=i)
       -                                panic("Trequest 2");
       -                }
       -                buf[i]=0;
       -                outTslS(Hdata, f->tag, r.p1, tmprstr(buf, i+1));
       -                break;
       -
       -        case Torigin:
       -                s = inshort(); /* tag */
       -                l = inlong(); /* position */
       -                l1 = inlong(); /* lines to seek past position */
       +    Rune buf[1025];
       +    int i, m;
       +    short s;
       +    long l, l1, l2;
       +    File *f;
       +    Posn p0, p1;
       +    Range r;
       +    String *str;
       +    char *c;
       +    Rune *rp;
       +
       +    if(type > TMAX)
       +        panic("inmesg");
       +
       +    journal(0, tname[type]);
       +
       +    inp = indata;
       +    switch(type){
       +    case -1:
       +        panic("rcv error");
       +
       +    default:
       +        fprintf(stderr, "unknown type %d\n", type);
       +        panic("rcv unknown");
       +
       +    case Tversion:
       +        tversion = inshort();
       +        journaln(0, tversion);
       +        break;
       +
       +    case Tstartcmdfile:
       +        l = invlong();      /* for 64-bit pointers */
       +        journaln(0, l);
       +        Strdupl(&genstr, samname);
       +        cmd = newfile();
       +        outTsv(Hbindname, cmd->tag, l);
       +        outTs(Hcurrent, cmd->tag);
       +        Fsetname(cmd, &genstr);
       +        cmd->rasp = emalloc(sizeof(List));
       +        cmd->state = Clean;
       +        if(cmdstr.n){
       +            Finsert(cmd, &cmdstr, 0L);
       +            Strdelete(&cmdstr, 0L, (Posn)cmdstr.n);
       +        }
       +        Fupdate(cmd, FALSE, TRUE);
       +        outT0(Hunlock);
       +        break;
       +
       +    case Tcheck:
       +        /* go through whichfile to check the tag */
       +        outTs(Hcheck, whichfile(inshort())->tag);
       +        break;
       +
       +    case Trequest:
       +        f = whichfile(inshort());
       +        p0 = inlong();
       +        p1 = p0+inshort();
       +        journaln(0, p0);
       +        journaln(0, p1-p0);
       +        if(f->state == Unread)
       +            panic("Trequest: unread");
       +        if(p1>f->nrunes)
       +            p1 = f->nrunes;
       +        if(p0>f->nrunes) /* can happen e.g. scrolling during command */
       +            p0 = f->nrunes;
       +        if(p0 == p1){
       +            i = 0;
       +            r.p1 = r.p2 = p0;
       +        }else{
       +            r = rdata(f->rasp, p0, p1-p0);
       +            i = r.p2-r.p1;
       +            if(Fchars(f, buf, r.p1, r.p2)!=i)
       +                panic("Trequest 2");
       +        }
       +        buf[i]=0;
       +        outTslS(Hdata, f->tag, r.p1, tmprstr(buf, i+1));
       +        break;
       +
       +    case Torigin:
       +        s = inshort(); /* tag */
       +        l = inlong(); /* position */
       +        l1 = inlong(); /* lines to seek past position */
                journaln(0, l1);
                if (!oldcompat){
                    l2 = inlong(); /* cookie to return (identifies layer) */
                    journaln(0, l2);
                } else
                    l2 = 0;
       -                lookorigin(whichfile(s), l, l1, l2);
       -                break;
       -
       -        case Tstartfile:
       -                termlocked++;
       -                f = whichfile(inshort());
       -                if(!f->rasp)        /* this might be a duplicate message */
       -                        f->rasp = emalloc(sizeof(List));
       -                current(f);
       -                outTsv(Hbindname, f->tag, invlong());        /* for 64-bit pointers */
       -                outTs(Hcurrent, f->tag);
       -                journaln(0, f->tag);
       -                if(f->state == Unread)
       -                        load(f);
       -                else{
       -                        if(f->nrunes>0){
       -                                rgrow(f->rasp, 0L, f->nrunes);
       -                                outTsll(Hgrow, f->tag, 0L, f->nrunes);
       -                        }
       -                        outTs(Hcheck0, f->tag);
       -                        moveto(f, f->dot.r);
       -                }
       -                break;
       -
       -        case Tworkfile:
       -                i = inshort();
       -                f = whichfile(i);
       -                current(f);
       -                f->dot.r.p1 = inlong();
       -                f->dot.r.p2 = inlong();
       -                f->tdot = f->dot.r;
       -                journaln(0, i);
       -                journaln(0, f->dot.r.p1);
       -                journaln(0, f->dot.r.p2);
       -                break;
       -
       -        case Ttype:
       -                f = whichfile(inshort());
       -                p0 = inlong();
       -                journaln(0, p0);
       -                journal(0, (char*)inp);
       -                str = tmpcstr((char*)inp);
       -                i = str->n;
       -                Finsert(f, str, p0);
       -                if(Fupdate(f, FALSE, FALSE))
       -                        modnum++;
       -                if(f==cmd && p0==f->nrunes-i && i>0 && str->s[i-1]=='\n'){
       -                        freetmpstr(str);
       -                        termlocked++;
       -                        termcommand();
       -                }else
       -                        freetmpstr(str);
       -                f->dot.r.p1 = f->dot.r.p2 = p0+i; /* terminal knows this already */
       -                f->tdot = f->dot.r;
       -                break;
       -
       -        case Tcut:
       -                f = whichfile(inshort());
       -                p0 = inlong();
       -                p1 = inlong();
       -                journaln(0, p0);
       -                journaln(0, p1);
       -                Fdelete(f, p0, p1);
       -                if(Fupdate(f, FALSE, FALSE))
       -                        modnum++;
       -                f->dot.r.p1 = f->dot.r.p2 = p0;
       -                f->tdot = f->dot.r;   /* terminal knows the value of dot already */
       -                break;
       -
       -        case Tpaste:
       -                f = whichfile(inshort());
       -                p0 = inlong();
       -                journaln(0, p0);
       -                for(l=0; l<snarfbuf->nrunes; l+=m){
       -                        m = snarfbuf->nrunes-l;
       -                        if(m>BLOCKSIZE)
       -                                m = BLOCKSIZE;
       -                        Bread(snarfbuf, genbuf, m, l);
       -                        Finsert(f, tmprstr(genbuf, m), p0);
       -                }
       -                if(Fupdate(f, FALSE, TRUE))
       -                        modnum++;
       -                f->dot.r.p1 = p0;
       -                f->dot.r.p2 = p0+snarfbuf->nrunes;
       -                f->tdot.p1 = -1; /* force telldot to tell (arguably a BUG) */
       -                telldot(f);
       -                outTs(Hunlockfile, f->tag);
       -                break;
       -
       -        case Tsnarf:
       -                i = inshort();
       -                p0 = inlong();
       -                p1 = inlong();
       -                snarf(whichfile(i), p0, p1, snarfbuf, 0);
       -                break;
       -
       -        case Tstartnewfile:
       -                l = invlong();
       -                Strdupl(&genstr, empty);
       -                f = newfile();
       -                f->rasp = emalloc(sizeof(List));
       -                outTsv(Hbindname, f->tag, l);
       -                Fsetname(f, &genstr);
       -                outTs(Hcurrent, f->tag);
       -                current(f);
       -                load(f);
       -                break;
       -
       -        case Twrite:
       -                termlocked++;
       -                i = inshort();
       -                journaln(0, i);
       -                f = whichfile(i);
       -                addr.r.p1 = 0;
       -                addr.r.p2 = f->nrunes;
       -                if(f->name.s[0] == 0)
       -                        error(Enoname);
       -                Strduplstr(&genstr, &f->name);
       -                writef(f);
       -                break;
       -
       -        case Tclose:
       -                termlocked++;
       -                i = inshort();
       -                journaln(0, i);
       -                f = whichfile(i);
       -                current(f);
       -                trytoclose(f);
       -                /* if trytoclose fails, will error out */
       -                delete(f);
       -                break;
       -
       -        case Tlook:
       -                f = whichfile(inshort());
       -                termlocked++;
       -                p0 = inlong();
       -                p1 = inlong();
       -                journaln(0, p0);
       -                journaln(0, p1);
       -                setgenstr(f, p0, p1);
       -                for(l = 0; l<genstr.n; l++){
       -                        i = genstr.s[l];
       -                        if(utfrune(".*+?(|)\\[]^$", i))
       -                                Strinsert(&genstr, tmpcstr("\\"), l++);
       -                }
       -                Straddc(&genstr, '\0');
       -                nextmatch(f, &genstr, p1, 1);
       -                moveto(f, sel.p[0]);
       -                break;
       -
       -        case Tsearch:
       -                termlocked++;
       -                if(curfile == 0)
       -                        error(Enofile);
       -                if(lastpat.s[0] == 0)
       -                        panic("Tsearch");
       -                nextmatch(curfile, &lastpat, curfile->dot.r.p2, 1);
       -                moveto(curfile, sel.p[0]);
       -                break;
       -
       -        case Tsend:
       -                termlocked++;
       -                inshort();        /* ignored */
       -                p0 = inlong();
       -                p1 = inlong();
       -                setgenstr(cmd, p0, p1);
       -                Bdelete(snarfbuf, (Posn)0, snarfbuf->nrunes);
       -                Binsert(snarfbuf, &genstr, (Posn)0);
       -                outTl(Hsnarflen, genstr.n);
       -                if(genstr.s[genstr.n-1] != '\n')
       -                        Straddc(&genstr, '\n');
       -                Finsert(cmd, &genstr, cmd->nrunes);
       -                Fupdate(cmd, FALSE, TRUE);
       -                cmd->dot.r.p1 = cmd->dot.r.p2 = cmd->nrunes;
       -                telldot(cmd);
       -                termcommand();
       -                break;
       -
       -        case Tdclick:
       -                f = whichfile(inshort());
       -                p1 = inlong();
       -                doubleclick(f, p1);
       -                f->tdot.p1 = f->tdot.p2 = p1;
       -                telldot(f);
       -                outTs(Hunlockfile, f->tag);
       -                break;
       -
       -        case Tstartsnarf:
       -                if (snarfbuf->nrunes <= 0) {        /* nothing to export */
       -                        outTs(Hsetsnarf, 0);
       -                        break;
       -                }
       -                c = 0;
       -                i = 0;
       -                m = snarfbuf->nrunes;
       -                if(m > 32000) {                /* tmprstr stores len in a short */
       -                        m = 32000;
       -                        dprint("?warning: snarf buffer truncated\n");
       -                }
       -                rp = malloc(m*sizeof(Rune));
       -                if(rp){
       -                        Bread(snarfbuf, rp, m, 0);
       -                        c = Strtoc(tmprstr(rp, m));
       -                        free(rp);
       -                        i = strlen(c);
       -                }
       -                outTs(Hsetsnarf, i);
       -                if(c){
       -                        Write(1, c, i);
       -                        free(c);
       -                } else
       -                        dprint("snarf buffer too long\n");
       -                break;
       -
       -        case Tsetsnarf:
       -                m = inshort();
       -                if(m > SNARFSIZE)
       -                        error(Etoolong);
       -                c = malloc(m+1);
       -                if(c){
       -                        for(i=0; i<m; i++)
       -                                c[i] = rcvchar();
       -                        c[m] = 0;
       -                        str = tmpcstr(c);
       -                        free(c);
       -                        Bdelete(snarfbuf, (Posn)0, snarfbuf->nrunes);
       -                        Binsert(snarfbuf, str, (Posn)0);
       -                        freetmpstr(str);
       -                        outT0(Hunlock);
       -                }
       -                break;
       -
       -        case Tack:
       -                waitack = 0;
       -                break;
       -
       -        case Texit:
       -                exits(0);
       -        }
       -        return TRUE;
       +        lookorigin(whichfile(s), l, l1, l2);
       +        break;
       +
       +    case Tstartfile:
       +        termlocked++;
       +        f = whichfile(inshort());
       +        if(!f->rasp)    /* this might be a duplicate message */
       +            f->rasp = emalloc(sizeof(List));
       +        current(f);
       +        outTsv(Hbindname, f->tag, invlong());   /* for 64-bit pointers */
       +        outTs(Hcurrent, f->tag);
       +        journaln(0, f->tag);
       +        if(f->state == Unread)
       +            load(f);
       +        else{
       +            if(f->nrunes>0){
       +                rgrow(f->rasp, 0L, f->nrunes);
       +                outTsll(Hgrow, f->tag, 0L, f->nrunes);
       +            }
       +            outTs(Hcheck0, f->tag);
       +            moveto(f, f->dot.r);
       +        }
       +        break;
       +
       +    case Tworkfile:
       +        i = inshort();
       +        f = whichfile(i);
       +        current(f);
       +        f->dot.r.p1 = inlong();
       +        f->dot.r.p2 = inlong();
       +        f->tdot = f->dot.r;
       +        journaln(0, i);
       +        journaln(0, f->dot.r.p1);
       +        journaln(0, f->dot.r.p2);
       +        break;
       +
       +    case Ttype:
       +        f = whichfile(inshort());
       +        p0 = inlong();
       +        journaln(0, p0);
       +        journal(0, (char*)inp);
       +        str = tmpcstr((char*)inp);
       +        i = str->n;
       +        Finsert(f, str, p0);
       +        if(Fupdate(f, FALSE, FALSE))
       +            modnum++;
       +        if(f==cmd && p0==f->nrunes-i && i>0 && str->s[i-1]=='\n'){
       +            freetmpstr(str);
       +            termlocked++;
       +            termcommand();
       +        }else
       +            freetmpstr(str);
       +        f->dot.r.p1 = f->dot.r.p2 = p0+i; /* terminal knows this already */
       +        f->tdot = f->dot.r;
       +        break;
       +
       +    case Tcut:
       +        f = whichfile(inshort());
       +        p0 = inlong();
       +        p1 = inlong();
       +        journaln(0, p0);
       +        journaln(0, p1);
       +        Fdelete(f, p0, p1);
       +        if(Fupdate(f, FALSE, FALSE))
       +            modnum++;
       +        f->dot.r.p1 = f->dot.r.p2 = p0;
       +        f->tdot = f->dot.r;   /* terminal knows the value of dot already */
       +        break;
       +
       +    case Tpaste:
       +        f = whichfile(inshort());
       +        p0 = inlong();
       +        journaln(0, p0);
       +        for(l=0; l<snarfbuf->nrunes; l+=m){
       +            m = snarfbuf->nrunes-l;
       +            if(m>BLOCKSIZE)
       +                m = BLOCKSIZE;
       +            Bread(snarfbuf, genbuf, m, l);
       +            Finsert(f, tmprstr(genbuf, m), p0);
       +        }
       +        if(Fupdate(f, FALSE, TRUE))
       +            modnum++;
       +        f->dot.r.p1 = p0;
       +        f->dot.r.p2 = p0+snarfbuf->nrunes;
       +        f->tdot.p1 = -1; /* force telldot to tell (arguably a BUG) */
       +        telldot(f);
       +        outTs(Hunlockfile, f->tag);
       +        break;
       +
       +    case Tsnarf:
       +        i = inshort();
       +        p0 = inlong();
       +        p1 = inlong();
       +        snarf(whichfile(i), p0, p1, snarfbuf, 0);
       +        break;
       +
       +    case Tstartnewfile:
       +        l = invlong();
       +        Strdupl(&genstr, empty);
       +        f = newfile();
       +        f->rasp = emalloc(sizeof(List));
       +        outTsv(Hbindname, f->tag, l);
       +        Fsetname(f, &genstr);
       +        outTs(Hcurrent, f->tag);
       +        current(f);
       +        load(f);
       +        break;
       +
       +    case Twrite:
       +        termlocked++;
       +        i = inshort();
       +        journaln(0, i);
       +        f = whichfile(i);
       +        addr.r.p1 = 0;
       +        addr.r.p2 = f->nrunes;
       +        if(f->name.s[0] == 0)
       +            error(Enoname);
       +        Strduplstr(&genstr, &f->name);
       +        writef(f);
       +        break;
       +
       +    case Tclose:
       +        termlocked++;
       +        i = inshort();
       +        journaln(0, i);
       +        f = whichfile(i);
       +        current(f);
       +        trytoclose(f);
       +        /* if trytoclose fails, will error out */
       +        delete(f);
       +        break;
       +
       +    case Tlook:
       +        f = whichfile(inshort());
       +        termlocked++;
       +        p0 = inlong();
       +        p1 = inlong();
       +        journaln(0, p0);
       +        journaln(0, p1);
       +        setgenstr(f, p0, p1);
       +        for(l = 0; l<genstr.n; l++){
       +            i = genstr.s[l];
       +            if(utfrune(".*+?(|)\\[]^$", i))
       +                Strinsert(&genstr, tmpcstr("\\"), l++);
       +        }
       +        Straddc(&genstr, '\0');
       +        nextmatch(f, &genstr, p1, 1);
       +        moveto(f, sel.p[0]);
       +        break;
       +
       +    case Tsearch:
       +        termlocked++;
       +        if(curfile == 0)
       +            error(Enofile);
       +        if(lastpat.s[0] == 0)
       +            panic("Tsearch");
       +        nextmatch(curfile, &lastpat, curfile->dot.r.p2, 1);
       +        moveto(curfile, sel.p[0]);
       +        break;
       +
       +    case Tsend:
       +        termlocked++;
       +        inshort();  /* ignored */
       +        p0 = inlong();
       +        p1 = inlong();
       +        setgenstr(cmd, p0, p1);
       +        Bdelete(snarfbuf, (Posn)0, snarfbuf->nrunes);
       +        Binsert(snarfbuf, &genstr, (Posn)0);
       +        outTl(Hsnarflen, genstr.n);
       +        if(genstr.s[genstr.n-1] != '\n')
       +            Straddc(&genstr, '\n');
       +        Finsert(cmd, &genstr, cmd->nrunes);
       +        Fupdate(cmd, FALSE, TRUE);
       +        cmd->dot.r.p1 = cmd->dot.r.p2 = cmd->nrunes;
       +        telldot(cmd);
       +        termcommand();
       +        break;
       +
       +    case Tdclick:
       +        f = whichfile(inshort());
       +        p1 = inlong();
       +        doubleclick(f, p1);
       +        f->tdot.p1 = f->tdot.p2 = p1;
       +        telldot(f);
       +        outTs(Hunlockfile, f->tag);
       +        break;
       +
       +    case Tstartsnarf:
       +        if (snarfbuf->nrunes <= 0) {    /* nothing to export */
       +            outTs(Hsetsnarf, 0);
       +            break;
       +        }
       +        c = 0;
       +        i = 0;
       +        m = snarfbuf->nrunes;
       +        if(m > 32000) {     /* tmprstr stores len in a short */
       +            m = 32000;
       +            dprint("?warning: snarf buffer truncated\n");
       +        }
       +        rp = malloc(m*sizeof(Rune));
       +        if(rp){
       +            Bread(snarfbuf, rp, m, 0);
       +            c = Strtoc(tmprstr(rp, m));
       +            free(rp);
       +            i = strlen(c);
       +        }
       +        outTs(Hsetsnarf, i);
       +        if(c){
       +            Write(1, c, i);
       +            free(c);
       +        } else
       +            dprint("snarf buffer too long\n");
       +        break;
       +
       +    case Tsetsnarf:
       +        m = inshort();
       +        if(m > SNARFSIZE)
       +            error(Etoolong);
       +        c = malloc(m+1);
       +        if(c){
       +            for(i=0; i<m; i++)
       +                c[i] = rcvchar();
       +            c[m] = 0;
       +            str = tmpcstr(c);
       +            free(c);
       +            Bdelete(snarfbuf, (Posn)0, snarfbuf->nrunes);
       +            Binsert(snarfbuf, str, (Posn)0);
       +            freetmpstr(str);
       +            outT0(Hunlock);
       +        }
       +        break;
       +
       +    case Tack:
       +        waitack = 0;
       +        break;
       +
       +    case Texit:
       +        exits(0);
       +    }
       +    return TRUE;
        }
        
        void
        snarf(File *f, Posn p1, Posn p2, Buffer *buf, int emptyok)
        {
       -        Posn l;
       -        int i;
       -
       -        if(!emptyok && p1==p2)
       -                return;
       -        Bdelete(buf, (Posn)0, buf->nrunes);
       -        /* Stage through genbuf to avoid compaction problems (vestigial) */
       -        for(l=p1; l<p2; l+=i){
       -                i = p2-l>BLOCKSIZE? BLOCKSIZE : p2-l;
       -                Fchars(f, genbuf, l, l+i);
       -                Binsert(buf, tmprstr(genbuf, i), buf->nrunes);
       -        }
       +    Posn l;
       +    int i;
       +
       +    if(!emptyok && p1==p2)
       +        return;
       +    Bdelete(buf, (Posn)0, buf->nrunes);
       +    /* Stage through genbuf to avoid compaction problems (vestigial) */
       +    for(l=p1; l<p2; l+=i){
       +        i = p2-l>BLOCKSIZE? BLOCKSIZE : p2-l;
       +        Fchars(f, genbuf, l, l+i);
       +        Binsert(buf, tmprstr(genbuf, i), buf->nrunes);
       +    }
        }
        
        int
        inshort(void)
        {
       -        ushort n;
       +    ushort n;
        
       -        n = inp[0] | (inp[1]<<8);
       -        inp += 2;
       -        return n;
       +    n = inp[0] | (inp[1]<<8);
       +    inp += 2;
       +    return n;
        }
        
        long
        inlong(void)
        {
       -        ulong n;
       +    ulong n;
        
       -        n = inp[0] | (inp[1]<<8) | (inp[2]<<16) | (inp[3]<<24);
       -        inp += 4;
       -        return n;
       +    n = inp[0] | (inp[1]<<8) | (inp[2]<<16) | (inp[3]<<24);
       +    inp += 4;
       +    return n;
        }
        
        long
        invlong(void)
        {
       -        ulong n;
       -        
       -        n = (inp[7]<<24) | (inp[6]<<16) | (inp[5]<<8) | inp[4];
       -        n = (n<<16) | (inp[3]<<8) | inp[2];
       -        n = (n<<16) | (inp[1]<<8) | inp[0];
       -        inp += 8;
       -        return n;
       +    ulong n;
       +    
       +    n = (inp[7]<<24) | (inp[6]<<16) | (inp[5]<<8) | inp[4];
       +    n = (n<<16) | (inp[3]<<8) | inp[2];
       +    n = (n<<16) | (inp[1]<<8) | inp[0];
       +    inp += 8;
       +    return n;
        }
        
        void
        setgenstr(File *f, Posn p0, Posn p1)
        {
       -        if(p0 != p1){
       -                if(p1-p0 >= TBLOCKSIZE)
       -                        error(Etoolong);
       -                Strinsure(&genstr, p1-p0);
       -                Fchars(f, genbuf, p0, p1);
       -                memmove(genstr.s, genbuf, RUNESIZE*(p1-p0));
       -                genstr.n = p1-p0;
       -        }else{
       -                if(snarfbuf->nrunes == 0)
       -                        error(Eempty);
       -                if(snarfbuf->nrunes > TBLOCKSIZE)
       -                        error(Etoolong);
       -                Bread(snarfbuf, genbuf, snarfbuf->nrunes, (Posn)0);
       -                Strinsure(&genstr, snarfbuf->nrunes);
       -                memmove(genstr.s, genbuf, RUNESIZE*snarfbuf->nrunes);
       -                genstr.n = snarfbuf->nrunes;
       -        }
       +    if(p0 != p1){
       +        if(p1-p0 >= TBLOCKSIZE)
       +            error(Etoolong);
       +        Strinsure(&genstr, p1-p0);
       +        Fchars(f, genbuf, p0, p1);
       +        memmove(genstr.s, genbuf, RUNESIZE*(p1-p0));
       +        genstr.n = p1-p0;
       +    }else{
       +        if(snarfbuf->nrunes == 0)
       +            error(Eempty);
       +        if(snarfbuf->nrunes > TBLOCKSIZE)
       +            error(Etoolong);
       +        Bread(snarfbuf, genbuf, snarfbuf->nrunes, (Posn)0);
       +        Strinsure(&genstr, snarfbuf->nrunes);
       +        memmove(genstr.s, genbuf, RUNESIZE*snarfbuf->nrunes);
       +        genstr.n = snarfbuf->nrunes;
       +    }
        }
        
        void
        outT0(Hmesg type)
        {
       -        outstart(type);
       -        outsend();
       +    outstart(type);
       +    outsend();
        }
        
        void
        outTl(Hmesg type, long l)
        {
       -        outstart(type);
       -        outlong(l);
       -        outsend();
       +    outstart(type);
       +    outlong(l);
       +    outsend();
        }
        
        void
        outTs(Hmesg type, int s)
        {
       -        outstart(type);
       -        journaln(1, s);
       -        outshort(s);
       -        outsend();
       +    outstart(type);
       +    journaln(1, s);
       +    outshort(s);
       +    outsend();
        }
        
        void
        outS(String *s)
        {
       -        char *c;
       -        int i;
       -
       -        c = Strtoc(s);
       -        i = strlen(c);
       -        outcopy(i, c);
       -        if(i > 99)
       -                c[99] = 0;
       -        journaln(1, i);
       -        journal(1, c);
       -        free(c);
       +    char *c;
       +    int i;
       +
       +    c = Strtoc(s);
       +    i = strlen(c);
       +    outcopy(i, c);
       +    if(i > 99)
       +        c[99] = 0;
       +    journaln(1, i);
       +    journal(1, c);
       +    free(c);
        }
        
        void
        outTsS(Hmesg type, int s1, String *s)
        {
       -        outstart(type);
       -        outshort(s1);
       -        outS(s);
       -        outsend();
       +    outstart(type);
       +    outshort(s1);
       +    outS(s);
       +    outsend();
        }
        
        void
        outTslS(Hmesg type, int s1, Posn l1, String *s)
        {
       -        outstart(type);
       -        outshort(s1);
       -        journaln(1, s1);
       -        outlong(l1);
       -        journaln(1, l1);
       -        outS(s);
       -        outsend();
       +    outstart(type);
       +    outshort(s1);
       +    journaln(1, s1);
       +    outlong(l1);
       +    journaln(1, l1);
       +    outS(s);
       +    outsend();
        }
        
        void
        outTS(Hmesg type, String *s)
        {
       -        outstart(type);
       -        outS(s);
       -        outsend();
       +    outstart(type);
       +    outS(s);
       +    outsend();
        }
        
        void
        outTsllS(Hmesg type, int s1, Posn l1, Posn l2, String *s)
        {
       -        outstart(type);
       -        outshort(s1);
       -        outlong(l1);
       -        outlong(l2);
       -        journaln(1, l1);
       -        journaln(1, l2);
       -        outS(s);
       -        outsend();
       +    outstart(type);
       +    outshort(s1);
       +    outlong(l1);
       +    outlong(l2);
       +    journaln(1, l1);
       +    journaln(1, l2);
       +    outS(s);
       +    outsend();
        }
        
        void
        outTsll(Hmesg type, int s, Posn l1, Posn l2)
        {
       -        outstart(type);
       -        outshort(s);
       -        outlong(l1);
       -        outlong(l2);
       -        journaln(1, l1);
       -        journaln(1, l2);
       -        outsend();
       +    outstart(type);
       +    outshort(s);
       +    outlong(l1);
       +    outlong(l2);
       +    journaln(1, l1);
       +    journaln(1, l2);
       +    outsend();
        }
        
        void
        outTsl(Hmesg type, int s, Posn l)
        {
       -        outstart(type);
       -        outshort(s);
       -        outlong(l);
       -        journaln(1, l);
       -        outsend();
       +    outstart(type);
       +    outshort(s);
       +    outlong(l);
       +    journaln(1, l);
       +    outsend();
        }
        
        void
        outTsv(Hmesg type, int s, Posn l)
        {
       -        outstart(type);
       -        outshort(s);
       -        outvlong((void*)l);
       -        journaln(1, l);
       -        outsend();
       +    outstart(type);
       +    outshort(s);
       +    outvlong((void*)l);
       +    journaln(1, l);
       +    outsend();
        }
        
        void
        outstart(Hmesg type)
        {
       -        journal(1, hname[type]);
       -        outmsg[0] = type;
       -        outp = outmsg+3;
       +    journal(1, hname[type]);
       +    outmsg[0] = type;
       +    outp = outmsg+3;
        }
        
        void
        outcopy(int count, void *data)
        {
       -        memmove(outp, data, count);
       -        outp += count;
       +    memmove(outp, data, count);
       +    outp += count;
        }
        
        void
        outshort(int s)
        {
       -        *outp++ = s;
       -        *outp++ = s>>8; 
       +    *outp++ = s;
       +    *outp++ = s>>8; 
        }
        
        void
        outlong(long l)
        {
       -        *outp++ = l;
       -        *outp++ = l>>8;
       -        *outp++ = l>>16;
       -        *outp++ = l>>24;
       +    *outp++ = l;
       +    *outp++ = l>>8;
       +    *outp++ = l>>16;
       +    *outp++ = l>>24;
        }
        
        void
        outvlong(void *v)
        {
       -        int i;
       -        uintptr_t l;
       +    int i;
       +    uintptr_t l;
        
       -        l = (uintptr_t)v;
       -        for(i = 0; i < 8; i++, l >>= 8)
       -                *outp++ = l;
       +    l = (uintptr_t)v;
       +    for(i = 0; i < 8; i++, l >>= 8)
       +        *outp++ = l;
        }
        
        void
        outsend(void)
        {
       -        int outcount;
       -
       -        outcount = outp-outmsg;
       -        outcount -= 3;
       -        outmsg[1] = outcount;
       -        outmsg[2] = outcount>>8;
       -        outmsg = outp;
       -        if(!noflush){
       -                outcount = outmsg-outdata;
       -                if (write(1, (char*) outdata, outcount) != outcount)
       -                        rescue();
       -                outmsg = outdata;
       -                return;
       -        }
       -        if(outmsg < outdata+DATASIZE)
       -                return;
       -        outflush();
       +    int outcount;
       +
       +    outcount = outp-outmsg;
       +    outcount -= 3;
       +    outmsg[1] = outcount;
       +    outmsg[2] = outcount>>8;
       +    outmsg = outp;
       +    if(!noflush){
       +        outcount = outmsg-outdata;
       +        if (write(1, (char*) outdata, outcount) != outcount)
       +            rescue();
       +        outmsg = outdata;
       +        return;
       +    }
       +    if(outmsg < outdata+DATASIZE)
       +        return;
       +    outflush();
        }
        
        void
        outflush(void)
        {
       -        if(outmsg == outdata)
       -                return;
       -        noflush = 0;
       -        outT0(Hack);
       -        waitack = 1;
       -        do
       -                if(rcv() == 0){
       -                        rescue();
       -                        exits("eof");
       -                }
       -        while(waitack);
       -        outmsg = outdata;
       -        noflush = 1;
       +    if(outmsg == outdata)
       +        return;
       +    noflush = 0;
       +    outT0(Hack);
       +    waitack = 1;
       +    do
       +        if(rcv() == 0){
       +            rescue();
       +            exits("eof");
       +        }
       +    while(waitack);
       +    outmsg = outdata;
       +    noflush = 1;
        }
 (DIR) diff --git a/sam/mesg.h b/sam/mesg.h
       @@ -2,107 +2,107 @@
        #ifdef CLASSIC_SAM_COMPATIBILITY
        #define VERSION 0
        #else
       -#define        VERSION        16091
       +#define VERSION 16091
        #endif
        
        extern int oldcompat;
        
       -#define        TBLOCKSIZE 512                  /* largest piece of text sent to terminal */
       -#define        DATASIZE  (UTFmax*TBLOCKSIZE+30) /* ... including protocol header stuff */
       -#define        SNARFSIZE 16384                /* maximum length of exchanged snarf buffer */
       +#define TBLOCKSIZE 512        /* largest piece of text sent to terminal */
       +#define DATASIZE  (UTFmax*TBLOCKSIZE+30) /* ... including protocol header stuff */
       +#define SNARFSIZE 16384     /* maximum length of exchanged snarf buffer */
        /*
         * Messages originating at the terminal
         */
        typedef enum Tmesg
        {
       -        Tversion,        /* version */
       -        Tstartcmdfile,        /* terminal just opened command frame */
       -        Tcheck,                /* ask host to poke with Hcheck */
       -        Trequest,        /* request data to fill a hole */
       -        Torigin,        /* gimme an Horigin near here */
       -        Tstartfile,        /* terminal just opened a file's frame */
       -        Tworkfile,        /* set file to which commands apply */
       -        Ttype,                /* add some characters, but terminal already knows */
       -        Tcut,
       -        Tpaste,
       -        Tsnarf,
       -        Tstartnewfile,        /* terminal just opened a new frame */
       -        Twrite,                /* write file */
       -        Tclose,                /* terminal requests file close; check mod. status */
       -        Tlook,                /* search for literal current text */
       -        Tsearch,        /* search for last regular expression */
       -        Tsend,                /* pretend he typed stuff */
       -        Tdclick,        /* double click */
       -        Tstartsnarf,        /* initiate snarf buffer exchange */
       -        Tsetsnarf,        /* remember string in snarf buffer */
       -        Tack,                /* acknowledge Hack */
       -        Texit,                /* exit */
       -        TMAX
       +    Tversion,   /* version */
       +    Tstartcmdfile,  /* terminal just opened command frame */
       +    Tcheck,     /* ask host to poke with Hcheck */
       +    Trequest,   /* request data to fill a hole */
       +    Torigin,    /* gimme an Horigin near here */
       +    Tstartfile, /* terminal just opened a file's frame */
       +    Tworkfile,  /* set file to which commands apply */
       +    Ttype,      /* add some characters, but terminal already knows */
       +    Tcut,
       +    Tpaste,
       +    Tsnarf,
       +    Tstartnewfile,  /* terminal just opened a new frame */
       +    Twrite,     /* write file */
       +    Tclose,     /* terminal requests file close; check mod. status */
       +    Tlook,      /* search for literal current text */
       +    Tsearch,    /* search for last regular expression */
       +    Tsend,      /* pretend he typed stuff */
       +    Tdclick,    /* double click */
       +    Tstartsnarf,    /* initiate snarf buffer exchange */
       +    Tsetsnarf,  /* remember string in snarf buffer */
       +    Tack,       /* acknowledge Hack */
       +    Texit,      /* exit */
       +    TMAX
        }Tmesg;
        /*
         * Messages originating at the host
         */
        typedef enum Hmesg
        {
       -        Hversion,        /* version */
       -        Hbindname,        /* attach name[0] to text in terminal */
       -        Hcurrent,        /* make named file the typing file */
       -        Hnewname,        /* create "" name in menu */
       -        Hmovname,        /* move file name in menu */
       -        Hgrow,                /* insert space in rasp */
       -        Hcheck0,        /* see below */
       -        Hcheck,                /* ask terminal to check whether it needs more data */
       -        Hunlock,        /* command is finished; user can do things */
       -        Hdata,                /* store this data in previously allocated space */
       -        Horigin,        /* set origin of file/frame in terminal */
       -        Hunlockfile,        /* unlock file in terminal */
       -        Hsetdot,        /* set dot in terminal */
       -        Hgrowdata,        /* Hgrow + Hdata folded together */
       -        Hmoveto,        /* scrolling, context search, etc. */
       -        Hclean,                /* named file is now 'clean' */
       -        Hdirty,                /* named file is now 'dirty' */
       -        Hcut,                /* remove space from rasp */
       -        Hsetpat,        /* set remembered regular expression */
       -        Hdelname,        /* delete file name from menu */
       -        Hclose,                /* close file and remove from menu */
       -        Hsetsnarf,        /* remember string in snarf buffer */
       -        Hsnarflen,        /* report length of implicit snarf */
       -        Hack,                /* request acknowledgement */
       -        Hexit,
       +    Hversion,   /* version */
       +    Hbindname,  /* attach name[0] to text in terminal */
       +    Hcurrent,   /* make named file the typing file */
       +    Hnewname,   /* create "" name in menu */
       +    Hmovname,   /* move file name in menu */
       +    Hgrow,      /* insert space in rasp */
       +    Hcheck0,    /* see below */
       +    Hcheck,     /* ask terminal to check whether it needs more data */
       +    Hunlock,    /* command is finished; user can do things */
       +    Hdata,      /* store this data in previously allocated space */
       +    Horigin,    /* set origin of file/frame in terminal */
       +    Hunlockfile,    /* unlock file in terminal */
       +    Hsetdot,    /* set dot in terminal */
       +    Hgrowdata,  /* Hgrow + Hdata folded together */
       +    Hmoveto,    /* scrolling, context search, etc. */
       +    Hclean,     /* named file is now 'clean' */
       +    Hdirty,     /* named file is now 'dirty' */
       +    Hcut,       /* remove space from rasp */
       +    Hsetpat,    /* set remembered regular expression */
       +    Hdelname,   /* delete file name from menu */
       +    Hclose,     /* close file and remove from menu */
       +    Hsetsnarf,  /* remember string in snarf buffer */
       +    Hsnarflen,  /* report length of implicit snarf */
       +    Hack,       /* request acknowledgement */
       +    Hexit,
            Hextcmd,   /* execute a external command */
       -        HMAX
       +    HMAX
        }Hmesg;
        typedef struct Header{
       -        uchar        type;                /* one of the above */
       -        uchar        count0;                /* low bits of data size */
       -        uchar        count1;                /* high bits of data size */
       -        uchar        data[1];        /* variable size */
       +    uchar   type;       /* one of the above */
       +    uchar   count0;     /* low bits of data size */
       +    uchar   count1;     /* high bits of data size */
       +    uchar   data[1];    /* variable size */
        }Header;
        /*
         * File transfer protocol schematic, a la Holzmann
       - *        
       - *        proc h
       - *        {        pvar n = 0;
       - *                queue h[4];
       - *        
       - *                do
       - *                :: (n <  N)  -> n++; t!Hgrow
       - *                :: (n == N)  -> n++; t!Hcheck0
       - *                :: h?Trequest -> t!Hdata
       - *                :: h?Tcheck  -> t!Hcheck
       - *                od
       - *        }
       - *        proc t
       - *        {        queue t[4];
       - *                do
       - *                :: t?Hgrow -> h!Trequest
       - *                :: t?Hdata -> skip
       - *                :: t?Hcheck0 -> h!Tcheck
       - *                :: t?Hcheck ->
       - *                        if
       - *                        :: break
       - *                        :: h!Trequest; h!Tcheck
       - *                        fi
       - *                od
       - *        }
       + *  
       + *  proc h
       + *  {   pvar n = 0;
       + *      queue h[4];
       + *  
       + *      do
       + *      :: (n <  N)  -> n++; t!Hgrow
       + *      :: (n == N)  -> n++; t!Hcheck0
       + *      :: h?Trequest -> t!Hdata
       + *      :: h?Tcheck  -> t!Hcheck
       + *      od
       + *  }
       + *  proc t
       + *  {   queue t[4];
       + *      do
       + *      :: t?Hgrow -> h!Trequest
       + *      :: t?Hdata -> skip
       + *      :: t?Hcheck0 -> h!Tcheck
       + *      :: t?Hcheck ->
       + *          if
       + *          :: break
       + *          :: h!Trequest; h!Tcheck
       + *          fi
       + *      od
       + *  }
         */
 (DIR) diff --git a/sam/moveto.c b/sam/moveto.c
       @@ -4,168 +4,168 @@
        void
        moveto(File *f, Range r)
        {
       -        Posn p1 = r.p1, p2 = r.p2;
       -
       -        f->dot.r.p1 = p1;
       -        f->dot.r.p2 = p2;
       -        if(f->rasp){
       -                telldot(f);
       -                outTsl(Hmoveto, f->tag, f->dot.r.p1);
       -        }
       +    Posn p1 = r.p1, p2 = r.p2;
       +
       +    f->dot.r.p1 = p1;
       +    f->dot.r.p2 = p2;
       +    if(f->rasp){
       +        telldot(f);
       +        outTsl(Hmoveto, f->tag, f->dot.r.p1);
       +    }
        }
        
        void
        telldot(File *f)
        {
       -        if(f->rasp == 0)
       -                panic("telldot");
       -        if(f->dot.r.p1==f->tdot.p1 && f->dot.r.p2==f->tdot.p2)
       -                return;
       -        outTsll(Hsetdot, f->tag, f->dot.r.p1, f->dot.r.p2);
       -        f->tdot = f->dot.r;
       +    if(f->rasp == 0)
       +        panic("telldot");
       +    if(f->dot.r.p1==f->tdot.p1 && f->dot.r.p2==f->tdot.p2)
       +        return;
       +    outTsll(Hsetdot, f->tag, f->dot.r.p1, f->dot.r.p2);
       +    f->tdot = f->dot.r;
        }
        
        void
        tellpat(void)
        {
       -        outTS(Hsetpat, &lastpat);
       -        patset = FALSE;
       +    outTS(Hsetpat, &lastpat);
       +    patset = FALSE;
        }
        
       -#define        CHARSHIFT        128
       +#define CHARSHIFT   128
        
        void
        lookorigin(File *f, Posn p0, Posn ls, long rl)
        {
       -        int nl, nc, c;
       -        Posn oldp0;
       -
       -        if(p0 > f->nrunes)
       -                p0 = f->nrunes;
       -        oldp0 = p0;
       -        Fgetcset(f, p0);
       -        for(nl=nc=c=0; c!=-1 && nl<ls && nc<ls*CHARSHIFT; nc++)
       -                if((c=Fbgetc(f)) == '\n'){
       -                        nl++;
       -                        oldp0 = p0-nc;
       -                }
       -        if(c == -1)
       -                p0 = 0;
       -        else if(nl==0){
       -                if(p0>=CHARSHIFT/2)
       -                        p0-=CHARSHIFT/2;
       -                else
       -                        p0 = 0;
       -        }else
       -                p0 = oldp0;
       +    int nl, nc, c;
       +    Posn oldp0;
       +
       +    if(p0 > f->nrunes)
       +        p0 = f->nrunes;
       +    oldp0 = p0;
       +    Fgetcset(f, p0);
       +    for(nl=nc=c=0; c!=-1 && nl<ls && nc<ls*CHARSHIFT; nc++)
       +        if((c=Fbgetc(f)) == '\n'){
       +            nl++;
       +            oldp0 = p0-nc;
       +        }
       +    if(c == -1)
       +        p0 = 0;
       +    else if(nl==0){
       +        if(p0>=CHARSHIFT/2)
       +            p0-=CHARSHIFT/2;
       +        else
       +            p0 = 0;
       +    }else
       +        p0 = oldp0;
            if (oldcompat)
       -            outTsl(Horigin, f->tag, p0);
       +        outTsl(Horigin, f->tag, p0);
            else
       -            outTsll(Horigin, f->tag, p0, rl);
       +        outTsll(Horigin, f->tag, p0, rl);
        }
        
        int
        alnum(int c)
        {
       -        /*
       -         * Hard to get absolutely right.  Use what we know about ASCII
       -         * and assume anything above the Latin control characters is
       -         * potentially an alphanumeric.
       -         */
       -        if(c<=' ')
       -                return 0;
       -        if(0x7F<=c && c<=0xA0)
       -                return 0;
       -        if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
       -                return 0;
       -        return 1;
       +    /*
       +     * Hard to get absolutely right.  Use what we know about ASCII
       +     * and assume anything above the Latin control characters is
       +     * potentially an alphanumeric.
       +     */
       +    if(c<=' ')
       +        return 0;
       +    if(0x7F<=c && c<=0xA0)
       +        return 0;
       +    if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
       +        return 0;
       +    return 1;
        }
        
        int
        clickmatch(File *f, int cl, int cr, int dir)
        {
       -        int c;
       -        int nest = 1;
       -
       -        while((c=(dir>0? Fgetc(f) : Fbgetc(f))) > 0)
       -                if(c == cr){
       -                        if(--nest==0)
       -                                return 1;
       -                }else if(c == cl)
       -                        nest++;
       -        return cl=='\n' && nest==1;
       +    int c;
       +    int nest = 1;
       +
       +    while((c=(dir>0? Fgetc(f) : Fbgetc(f))) > 0)
       +        if(c == cr){
       +            if(--nest==0)
       +                return 1;
       +        }else if(c == cl)
       +            nest++;
       +    return cl=='\n' && nest==1;
        }
        
        Rune*
        strrune(Rune *s, Rune c)
        {
       -        Rune c1;
       -
       -        if(c == 0) {
       -                while(*s++)
       -                        ;
       -                return s-1;
       -        }
       -
       -        while(c1 = *s++)
       -                if(c1 == c)
       -                        return s-1;
       -        return 0;
       +    Rune c1;
       +
       +    if(c == 0) {
       +        while(*s++)
       +            ;
       +        return s-1;
       +    }
       +
       +    while(c1 = *s++)
       +        if(c1 == c)
       +            return s-1;
       +    return 0;
        }
        
        void
        doubleclick(File *f, Posn p1)
        {
       -        int c, i;
       -        Rune *r, *l;
       -
       -        if(p1 > f->nrunes)
       -                return;
       -        f->dot.r.p1 = f->dot.r.p2 = p1;
       -        for(i=0; left[i]; i++){
       -                l = left[i];
       -                r = right[i];
       -                /* try left match */
       -                if(p1 == 0){
       -                        Fgetcset(f, p1);
       -                        c = '\n';
       -                }else{
       -                        Fgetcset(f, p1-1);
       -                        c = Fgetc(f);
       -                }
       -                if(c!=-1 && strrune(l, c)){
       -                        if(clickmatch(f, c, r[strrune(l, c)-l], 1)){
       -                                f->dot.r.p1 = p1;
       -                                f->dot.r.p2 = f->getcp-(c!='\n');
       -                        }
       -                        return;
       -                }
       -                /* try right match */
       -                if(p1 == f->nrunes){
       -                        Fbgetcset(f, p1);
       -                        c = '\n';
       -                }else{
       -                        Fbgetcset(f, p1+1);
       -                        c = Fbgetc(f);
       -                }
       -                if(c!=-1 && strrune(r, c)){
       -                        if(clickmatch(f, c, l[strrune(r, c)-r], -1)){
       -                                f->dot.r.p1 = f->getcp;
       -                                if(c!='\n' || f->getcp!=0 ||
       -                                   (Fgetcset(f, (Posn)0),Fgetc(f))=='\n')
       -                                        f->dot.r.p1++;
       -                                f->dot.r.p2 = p1+(p1<f->nrunes && c=='\n');
       -                        }
       -                        return;
       -                }
       -        }
       -        /* try filling out word to right */
       -        Fgetcset(f, p1);
       -        while((c=Fgetc(f))!=-1 && alnum(c))
       -                f->dot.r.p2++;
       -        /* try filling out word to left */
       -        Fbgetcset(f, p1);
       -        while((c=Fbgetc(f))!=-1 && alnum(c))
       -                f->dot.r.p1--;
       +    int c, i;
       +    Rune *r, *l;
       +
       +    if(p1 > f->nrunes)
       +        return;
       +    f->dot.r.p1 = f->dot.r.p2 = p1;
       +    for(i=0; left[i]; i++){
       +        l = left[i];
       +        r = right[i];
       +        /* try left match */
       +        if(p1 == 0){
       +            Fgetcset(f, p1);
       +            c = '\n';
       +        }else{
       +            Fgetcset(f, p1-1);
       +            c = Fgetc(f);
       +        }
       +        if(c!=-1 && strrune(l, c)){
       +            if(clickmatch(f, c, r[strrune(l, c)-l], 1)){
       +                f->dot.r.p1 = p1;
       +                f->dot.r.p2 = f->getcp-(c!='\n');
       +            }
       +            return;
       +        }
       +        /* try right match */
       +        if(p1 == f->nrunes){
       +            Fbgetcset(f, p1);
       +            c = '\n';
       +        }else{
       +            Fbgetcset(f, p1+1);
       +            c = Fbgetc(f);
       +        }
       +        if(c!=-1 && strrune(r, c)){
       +            if(clickmatch(f, c, l[strrune(r, c)-r], -1)){
       +                f->dot.r.p1 = f->getcp;
       +                if(c!='\n' || f->getcp!=0 ||
       +                   (Fgetcset(f, (Posn)0),Fgetc(f))=='\n')
       +                    f->dot.r.p1++;
       +                f->dot.r.p2 = p1+(p1<f->nrunes && c=='\n');
       +            }
       +            return;
       +        }
       +    }
       +    /* try filling out word to right */
       +    Fgetcset(f, p1);
       +    while((c=Fgetc(f))!=-1 && alnum(c))
       +        f->dot.r.p2++;
       +    /* try filling out word to left */
       +    Fbgetcset(f, p1);
       +    while((c=Fbgetc(f))!=-1 && alnum(c))
       +        f->dot.r.p1--;
        }
        
 (DIR) diff --git a/sam/multi.c b/sam/multi.c
       @@ -3,96 +3,96 @@
        
        #include <libgen.h>
        
       -List        file;
       -ushort        tag;
       +List    file;
       +ushort  tag;
        
        File *
        newfile(void)
        {
       -        File *f;
       -
       -        inslist(&file, 0, (long)(f = Fopen()));
       -        f->tag = tag++;
       -        if(downloaded)
       -                outTs(Hnewname, f->tag);
       -        /* already sorted; file name is "" */
       -        return f;
       +    File *f;
       +
       +    inslist(&file, 0, (long)(f = Fopen()));
       +    f->tag = tag++;
       +    if(downloaded)
       +        outTs(Hnewname, f->tag);
       +    /* already sorted; file name is "" */
       +    return f;
        }
        
        int
        whichmenu(File *f)
        {
       -        int i;
       +    int i;
        
       -        for(i=0; i<file.nused; i++)
       -                if(file.filepptr[i]==f)
       -                        return i;
       -        return -1;
       +    for(i=0; i<file.nused; i++)
       +        if(file.filepptr[i]==f)
       +            return i;
       +    return -1;
        }
        
        void
        delfile(File *f)
        {
       -        int w = whichmenu(f);
       -
       -        if(w < 0)        /* e.g. x/./D */
       -                return;
       -        if(downloaded)
       -                outTs(Hdelname, f->tag);
       -        dellist(&file, w);
       -        Fclose(f);
       +    int w = whichmenu(f);
       +
       +    if(w < 0)   /* e.g. x/./D */
       +        return;
       +    if(downloaded)
       +        outTs(Hdelname, f->tag);
       +    dellist(&file, w);
       +    Fclose(f);
        }
        
        void
        sortname(File *f)
        {
       -        int i, cmp, w;
       -        int dupwarned;
       -
       -        w = whichmenu(f);
       -        dupwarned = FALSE;
       -        dellist(&file, w);
       -        if(f == cmd)
       -                i = 0;
       -        else for(i=0; i<file.nused; i++){
       -                cmp = Strcmp(&f->name, &file.filepptr[i]->name);
       -                if(cmp==0 && !dupwarned){
       -                        dupwarned = TRUE;
       -                        warn_S(Wdupname, &f->name);
       -                }else if(cmp<0 && (i>0 || cmd==0))
       -                        break;
       -        }
       -        inslist(&file, i, (long)f);
       -        if(downloaded)
       -                outTsS(Hmovname, f->tag, &f->name);
       +    int i, cmp, w;
       +    int dupwarned;
       +
       +    w = whichmenu(f);
       +    dupwarned = FALSE;
       +    dellist(&file, w);
       +    if(f == cmd)
       +        i = 0;
       +    else for(i=0; i<file.nused; i++){
       +        cmp = Strcmp(&f->name, &file.filepptr[i]->name);
       +        if(cmp==0 && !dupwarned){
       +            dupwarned = TRUE;
       +            warn_S(Wdupname, &f->name);
       +        }else if(cmp<0 && (i>0 || cmd==0))
       +            break;
       +    }
       +    inslist(&file, i, (long)f);
       +    if(downloaded)
       +        outTsS(Hmovname, f->tag, &f->name);
        }
        
        void
        state(File *f, int cleandirty)
        {
       -        if(f == cmd)
       -                return;
       -        if(downloaded && whichmenu(f)>=0){        /* else flist or menu */
       -                if(f->state==Dirty && cleandirty!=Dirty)
       -                        outTs(Hclean, f->tag);
       -                else if(f->state!=Dirty && cleandirty==Dirty)
       -                        outTs(Hdirty, f->tag);
       -        }
       -        f->state = cleandirty;
       +    if(f == cmd)
       +        return;
       +    if(downloaded && whichmenu(f)>=0){  /* else flist or menu */
       +        if(f->state==Dirty && cleandirty!=Dirty)
       +            outTs(Hclean, f->tag);
       +        else if(f->state!=Dirty && cleandirty==Dirty)
       +            outTs(Hdirty, f->tag);
       +    }
       +    f->state = cleandirty;
        }
        
        File *
        lookfile(String *s, int fuzzy)
        {
       -        int i;
       +    int i;
            File *b = NULL;
            char *sc = Strtoc(s);
            size_t sl = strlen(sc);
        
       -        for(i=0; i<file.nused; i++){
       +    for(i=0; i<file.nused; i++){
                int l2;
       -                if(Strcmp(&file.filepptr[i]->name, s) == 0)
       -                        return file.filepptr[i];
       +        if(Strcmp(&file.filepptr[i]->name, s) == 0)
       +            return file.filepptr[i];
        
                if (fuzzy){
                    char *ac = Strtoc(&file.filepptr[i]->name);
       @@ -105,5 +105,5 @@ lookfile(String *s, int fuzzy)
                }
            }
        
       -        return b;
       +    return b;
        }
 (DIR) diff --git a/sam/parse.h b/sam/parse.h
       @@ -3,67 +3,67 @@ typedef struct Addr Addr;
        typedef struct Cmd Cmd;
        struct Addr
        {
       -        char        type;        /* # (char addr), l (line addr), / ? . $ + - , ; */
       -        union{
       -                String        *re;
       -                Addr        *aleft;                /* left side of , and ; */
       -        } g;
       -        Posn        num;
       -        Addr        *next;                        /* or right side of , and ; */
       +    char    type;   /* # (char addr), l (line addr), / ? . $ + - , ; */
       +    union{
       +        String  *re;
       +        Addr    *aleft;     /* left side of , and ; */
       +    } g;
       +    Posn    num;
       +    Addr    *next;          /* or right side of , and ; */
        };
        
       -#define        are        g.re
       -#define        left        g.aleft
       +#define are g.re
       +#define left    g.aleft
        
        struct Cmd
        {
       -        Addr        *addr;                        /* address (range of text) */
       -        String        *re;                        /* regular expression for e.g. 'x' */
       -        union{
       -                Cmd        *cmd;                /* target of x, g, {, etc. */
       -                String        *text;                /* text of a, c, i; rhs of s */
       -                Addr        *addr;                /* address for m, t */
       -        } g;
       -        Cmd        *next;                        /* pointer to next element in {} */
       -        short        num;
       -        ushort        flag;                        /* whatever */
       -        ushort        cmdc;                        /* command character; 'x' etc. */
       +    Addr    *addr;          /* address (range of text) */
       +    String  *re;            /* regular expression for e.g. 'x' */
       +    union{
       +        Cmd *cmd;       /* target of x, g, {, etc. */
       +        String  *text;      /* text of a, c, i; rhs of s */
       +        Addr    *addr;      /* address for m, t */
       +    } g;
       +    Cmd *next;          /* pointer to next element in {} */
       +    short   num;
       +    ushort  flag;           /* whatever */
       +    ushort  cmdc;           /* command character; 'x' etc. */
        };
        
       -#define        ccmd        g.cmd
       -#define        ctext        g.text
       -#define        caddr        g.addr
       +#define ccmd    g.cmd
       +#define ctext   g.text
       +#define caddr   g.addr
        
        extern struct cmdtab{
       -        ushort        cmdc;                /* command character */
       -        uchar        text;                /* takes a textual argument? */
       -        uchar        regexp;                /* takes a regular expression? */
       -        uchar        addr;                /* takes an address (m or t)? */
       -        uchar        defcmd;                /* default command; 0==>none */
       -        uchar        defaddr;        /* default address */
       -        uchar        count;                /* takes a count e.g. s2/// */
       -        char        *token;                /* takes text terminated by one of these */
       -        int        (*fn)(File*, Cmd*);        /* function to call with parse tree */
       +    ushort  cmdc;       /* command character */
       +    uchar   text;       /* takes a textual argument? */
       +    uchar   regexp;     /* takes a regular expression? */
       +    uchar   addr;       /* takes an address (m or t)? */
       +    uchar   defcmd;     /* default command; 0==>none */
       +    uchar   defaddr;    /* default address */
       +    uchar   count;      /* takes a count e.g. s2/// */
       +    char    *token;     /* takes text terminated by one of these */
       +    int (*fn)(File*, Cmd*); /* function to call with parse tree */
        }cmdtab[];
        
       -enum Defaddr{        /* default addresses */
       -        aNo,
       -        aDot,
       -        aAll
       +enum Defaddr{   /* default addresses */
       +    aNo,
       +    aDot,
       +    aAll
        };
        
       -int        nl_cmd(File*, Cmd*), a_cmd(File*, Cmd*), b_cmd(File*, Cmd*);
       -int        c_cmd(File*, Cmd*), cd_cmd(File*, Cmd*), d_cmd(File*, Cmd*);
       -int        D_cmd(File*, Cmd*), e_cmd(File*, Cmd*);
       -int        f_cmd(File*, Cmd*), g_cmd(File*, Cmd*), i_cmd(File*, Cmd*);
       -int        k_cmd(File*, Cmd*), m_cmd(File*, Cmd*), n_cmd(File*, Cmd*);
       -int        p_cmd(File*, Cmd*), q_cmd(File*, Cmd*);
       -int        s_cmd(File*, Cmd*), u_cmd(File*, Cmd*), w_cmd(File*, Cmd*);
       -int        x_cmd(File*, Cmd*), X_cmd(File*, Cmd*), plan9_cmd(File*, Cmd*);
       -int        eq_cmd(File*, Cmd*);
       +int nl_cmd(File*, Cmd*), a_cmd(File*, Cmd*), b_cmd(File*, Cmd*);
       +int c_cmd(File*, Cmd*), cd_cmd(File*, Cmd*), d_cmd(File*, Cmd*);
       +int D_cmd(File*, Cmd*), e_cmd(File*, Cmd*);
       +int f_cmd(File*, Cmd*), g_cmd(File*, Cmd*), i_cmd(File*, Cmd*);
       +int k_cmd(File*, Cmd*), m_cmd(File*, Cmd*), n_cmd(File*, Cmd*);
       +int p_cmd(File*, Cmd*), q_cmd(File*, Cmd*);
       +int s_cmd(File*, Cmd*), u_cmd(File*, Cmd*), w_cmd(File*, Cmd*);
       +int x_cmd(File*, Cmd*), X_cmd(File*, Cmd*), plan9_cmd(File*, Cmd*);
       +int eq_cmd(File*, Cmd*);
        
        
       -String        *getregexp(int);
       -Addr        *newaddr(void);
       -Address        address(Addr*, Address, int);
       -int        cmdexec(File*, Cmd*);
       +String  *getregexp(int);
       +Addr    *newaddr(void);
       +Address address(Addr*, Address, int);
       +int cmdexec(File*, Cmd*);
 (DIR) diff --git a/sam/rasp.c b/sam/rasp.c
       @@ -4,273 +4,273 @@
         * GROWDATASIZE must be big enough that all errors go out as Hgrowdata's,
         * so they will be scrolled into visibility in the ~~sam~~ window (yuck!).
         */
       -#define        GROWDATASIZE        50        /* if size is > this, send data with grow */
       +#define GROWDATASIZE    50  /* if size is > this, send data with grow */
        
       -void        rcut(List*, Posn, Posn);
       -int        rterm(List*, Posn);
       -void        rgrow(List*, Posn, Posn);
       +void    rcut(List*, Posn, Posn);
       +int rterm(List*, Posn);
       +void    rgrow(List*, Posn, Posn);
        
        void
        toterminal(File *f, int toterm)
        {
       -        Buffer *t = f->transcript;
       -        Posn n, p0, p1, p2, delta = 0, deltacmd = 0;
       -        Range r;
       -        union{
       -                union        Hdr g;
       -                Rune        buf[8+GROWDATASIZE];
       -        }hdr;
       -        Posn growpos, grown;
       +    Buffer *t = f->transcript;
       +    Posn n, p0, p1, p2, delta = 0, deltacmd = 0;
       +    Range r;
       +    union{
       +        union   Hdr g;
       +        Rune    buf[8+GROWDATASIZE];
       +    }hdr;
       +    Posn growpos, grown;
        
       -        if(f->rasp == 0)
       -                return;
       -        if(f->marked)
       -                p0 = f->markp+sizeof(Mark)/RUNESIZE;
       -        else
       -                p0 = 0;
       -        grown = 0;
       -        noflush = 1;
       -        SET(growpos);
       -        while(Bread(t, (Rune*)&hdr, sizeof(hdr)/RUNESIZE, p0) > 0){
       -                switch(hdr.g.cs.c){
       -                default:
       -                        fprintf(stderr, "char %c %.2x\n", hdr.g.cs.c, hdr.g.cs.c);
       -                        panic("unknown in toterminal");
       +    if(f->rasp == 0)
       +        return;
       +    if(f->marked)
       +        p0 = f->markp+sizeof(Mark)/RUNESIZE;
       +    else
       +        p0 = 0;
       +    grown = 0;
       +    noflush = 1;
       +    SET(growpos);
       +    while(Bread(t, (Rune*)&hdr, sizeof(hdr)/RUNESIZE, p0) > 0){
       +        switch(hdr.g.cs.c){
       +        default:
       +            fprintf(stderr, "char %c %.2x\n", hdr.g.cs.c, hdr.g.cs.c);
       +            panic("unknown in toterminal");
        
       -                case 'd':
       -                        if(grown){
       -                                outTsll(Hgrow, f->tag, growpos, grown);
       -                                grown = 0;
       -                        }
       -                        p1 = hdr.g.cll.l;
       -                        p2 = hdr.g.cll.l1;
       -                        if(p2 <= p1)
       -                                panic("toterminal delete 0");
       -                        if(f==cmd && p1<cmdpt){
       -                                if(p2 <= cmdpt)
       -                                        deltacmd -= (p2-p1);
       -                                else
       -                                        deltacmd -= cmdpt-p1;
       -                        }
       -                        p1 += delta;
       -                        p2 += delta;
       -                        p0 += sizeof(struct _cll)/RUNESIZE;
       -                        if(toterm)
       -                                outTsll(Hcut, f->tag, p1, p2-p1);
       -                        rcut(f->rasp, p1, p2);
       -                        delta -= p2-p1;
       -                        break;
       +        case 'd':
       +            if(grown){
       +                outTsll(Hgrow, f->tag, growpos, grown);
       +                grown = 0;
       +            }
       +            p1 = hdr.g.cll.l;
       +            p2 = hdr.g.cll.l1;
       +            if(p2 <= p1)
       +                panic("toterminal delete 0");
       +            if(f==cmd && p1<cmdpt){
       +                if(p2 <= cmdpt)
       +                    deltacmd -= (p2-p1);
       +                else
       +                    deltacmd -= cmdpt-p1;
       +            }
       +            p1 += delta;
       +            p2 += delta;
       +            p0 += sizeof(struct _cll)/RUNESIZE;
       +            if(toterm)
       +                outTsll(Hcut, f->tag, p1, p2-p1);
       +            rcut(f->rasp, p1, p2);
       +            delta -= p2-p1;
       +            break;
        
       -                case 'f':
       -                        if(grown){
       -                                outTsll(Hgrow, f->tag, growpos, grown);
       -                                grown = 0;
       -                        }
       -                        n = hdr.g.cs.s;
       -                        p0 += sizeof(struct _cs)/RUNESIZE + n;
       -                        break;
       +        case 'f':
       +            if(grown){
       +                outTsll(Hgrow, f->tag, growpos, grown);
       +                grown = 0;
       +            }
       +            n = hdr.g.cs.s;
       +            p0 += sizeof(struct _cs)/RUNESIZE + n;
       +            break;
        
       -                case 'i':
       -                        n = hdr.g.csl.s;
       -                        p1 = hdr.g.csl.l;
       -                        p0 += sizeof(struct _csl)/RUNESIZE + n;
       -                        if(n <= 0)
       -                                panic("toterminal insert 0");
       -                        if(f==cmd && p1<cmdpt)
       -                                deltacmd += n;
       -                        p1 += delta;
       -                        if(toterm){
       -                                if(n>GROWDATASIZE || !rterm(f->rasp, p1)){
       -                                        rgrow(f->rasp, p1, n);
       -                                        if(grown && growpos+grown!=p1){
       -                                                outTsll(Hgrow, f->tag, growpos, grown);
       -                                                grown = 0;
       -                                        }
       -                                        if(grown)
       -                                                grown += n;
       -                                        else{
       -                                                growpos = p1;
       -                                                grown = n;
       -                                        }
       -                                }else{
       -                                        Rune *rp;
       -                                        if(grown){
       -                                                outTsll(Hgrow, f->tag, growpos, grown);
       -                                                grown = 0;
       -                                        }
       -                                        rp = hdr.buf+sizeof(hdr.g.csl)/RUNESIZE;
       -                                        rgrow(f->rasp, p1, n);
       -                                        r = rdata(f->rasp, p1, n);
       -                                        if(r.p1!=p1 || r.p2!=p1+n)
       -                                                panic("rdata in toterminal");
       -                                        outTsllS(Hgrowdata, f->tag, p1, n, tmprstr(rp, n));
       -                                }
       -                        }else{
       -                                rgrow(f->rasp, p1, n);
       -                                r = rdata(f->rasp, p1, n);
       -                                if(r.p1!=p1 || r.p2!=p1+n)
       -                                        panic("rdata in toterminal");
       -                        }
       -                        delta += n;
       -                        break;
       -                }
       -        }
       -        if(grown)
       -                outTsll(Hgrow, f->tag, growpos, grown);
       -        if(toterm)
       -                outTs(Hcheck0, f->tag);
       -        outflush();
       -        noflush = 0;
       -        if(f == cmd){
       -                cmdpt += deltacmd+cmdptadv;
       -                cmdptadv = 0;
       -        }
       +        case 'i':
       +            n = hdr.g.csl.s;
       +            p1 = hdr.g.csl.l;
       +            p0 += sizeof(struct _csl)/RUNESIZE + n;
       +            if(n <= 0)
       +                panic("toterminal insert 0");
       +            if(f==cmd && p1<cmdpt)
       +                deltacmd += n;
       +            p1 += delta;
       +            if(toterm){
       +                if(n>GROWDATASIZE || !rterm(f->rasp, p1)){
       +                    rgrow(f->rasp, p1, n);
       +                    if(grown && growpos+grown!=p1){
       +                        outTsll(Hgrow, f->tag, growpos, grown);
       +                        grown = 0;
       +                    }
       +                    if(grown)
       +                        grown += n;
       +                    else{
       +                        growpos = p1;
       +                        grown = n;
       +                    }
       +                }else{
       +                    Rune *rp;
       +                    if(grown){
       +                        outTsll(Hgrow, f->tag, growpos, grown);
       +                        grown = 0;
       +                    }
       +                    rp = hdr.buf+sizeof(hdr.g.csl)/RUNESIZE;
       +                    rgrow(f->rasp, p1, n);
       +                    r = rdata(f->rasp, p1, n);
       +                    if(r.p1!=p1 || r.p2!=p1+n)
       +                        panic("rdata in toterminal");
       +                    outTsllS(Hgrowdata, f->tag, p1, n, tmprstr(rp, n));
       +                }
       +            }else{
       +                rgrow(f->rasp, p1, n);
       +                r = rdata(f->rasp, p1, n);
       +                if(r.p1!=p1 || r.p2!=p1+n)
       +                    panic("rdata in toterminal");
       +            }
       +            delta += n;
       +            break;
       +        }
       +    }
       +    if(grown)
       +        outTsll(Hgrow, f->tag, growpos, grown);
       +    if(toterm)
       +        outTs(Hcheck0, f->tag);
       +    outflush();
       +    noflush = 0;
       +    if(f == cmd){
       +        cmdpt += deltacmd+cmdptadv;
       +        cmdptadv = 0;
       +    }
        }
        
       -#define        M        0x80000000L
       -#define        P(i)        r->longptr[i]
       -#define        T(i)        (P(i)&M)        /* in terminal */
       -#define        L(i)        (P(i)&~M)        /* length of this piece */
       +#define M   0x80000000L
       +#define P(i)    r->longptr[i]
       +#define T(i)    (P(i)&M)    /* in terminal */
       +#define L(i)    (P(i)&~M)   /* length of this piece */
        
        void
        rcut(List *r, Posn p1, Posn p2)
        {
       -        Posn p, x;
       -        int i;
       +    Posn p, x;
       +    int i;
        
       -        if(p1 == p2)
       -                panic("rcut 0");
       -        for(p=0,i=0; i<r->nused && p+L(i)<=p1; p+=L(i++))
       -                ;
       -        if(i == r->nused)
       -                panic("rcut 1");
       -        if(p<p1){        /* chop this piece */
       -                if(p+L(i) < p2){
       -                        x = p1-p;
       -                        p += L(i);
       -                }else{
       -                        x = L(i)-(p2-p1);
       -                        p = p2;
       -                }
       -                if(T(i))
       -                        P(i) = x|M;
       -                else
       -                        P(i) = x;
       -                i++;
       -        }
       -        while(i<r->nused && p+L(i)<=p2){
       -                p += L(i);
       -                dellist(r, i);
       -        }
       -        if(p<p2){
       -                if(i == r->nused)
       -                        panic("rcut 2");
       -                x = L(i)-(p2-p);
       -                if(T(i))
       -                        P(i) = x|M;
       -                else
       -                        P(i) = x;
       -        }
       -        /* can we merge i and i-1 ? */
       -        if(i>0 && i<r->nused && T(i-1)==T(i)){
       -                x = L(i-1)+L(i);
       -                dellist(r, i--);
       -                if(T(i))
       -                        P(i)=x|M;
       -                else
       -                        P(i)=x;
       -        }
       +    if(p1 == p2)
       +        panic("rcut 0");
       +    for(p=0,i=0; i<r->nused && p+L(i)<=p1; p+=L(i++))
       +        ;
       +    if(i == r->nused)
       +        panic("rcut 1");
       +    if(p<p1){   /* chop this piece */
       +        if(p+L(i) < p2){
       +            x = p1-p;
       +            p += L(i);
       +        }else{
       +            x = L(i)-(p2-p1);
       +            p = p2;
       +        }
       +        if(T(i))
       +            P(i) = x|M;
       +        else
       +            P(i) = x;
       +        i++;
       +    }
       +    while(i<r->nused && p+L(i)<=p2){
       +        p += L(i);
       +        dellist(r, i);
       +    }
       +    if(p<p2){
       +        if(i == r->nused)
       +            panic("rcut 2");
       +        x = L(i)-(p2-p);
       +        if(T(i))
       +            P(i) = x|M;
       +        else
       +            P(i) = x;
       +    }
       +    /* can we merge i and i-1 ? */
       +    if(i>0 && i<r->nused && T(i-1)==T(i)){
       +        x = L(i-1)+L(i);
       +        dellist(r, i--);
       +        if(T(i))
       +            P(i)=x|M;
       +        else
       +            P(i)=x;
       +    }
        }
        
        void
        rgrow(List *r, Posn p1, Posn n)
        {
       -        Posn p;
       -        int i;
       +    Posn p;
       +    int i;
        
       -        if(n == 0)
       -                panic("rgrow 0");
       -        for(p=0,i=0; i<r->nused && p+L(i)<=p1; p+=L(i++))
       -                ;
       -        if(i == r->nused){        /* stick on end of file */
       -                if(p!=p1)
       -                        panic("rgrow 1");
       -                if(i>0 && !T(i-1))
       -                        P(i-1)+=n;
       -                else
       -                        inslist(r, i, n);
       -        }else if(!T(i))                /* goes in this empty piece */
       -                P(i)+=n;
       -        else if(p==p1 && i>0 && !T(i-1))        /* special case; simplifies life */
       -                P(i-1)+=n;
       -        else if(p==p1)
       -                inslist(r, i, n);
       -        else{                        /* must break piece in terminal */
       -                inslist(r, i+1, (L(i)-(p1-p))|M);
       -                inslist(r, i+1, n);
       -                P(i) = (p1-p)|M;
       -        }
       +    if(n == 0)
       +        panic("rgrow 0");
       +    for(p=0,i=0; i<r->nused && p+L(i)<=p1; p+=L(i++))
       +        ;
       +    if(i == r->nused){  /* stick on end of file */
       +        if(p!=p1)
       +            panic("rgrow 1");
       +        if(i>0 && !T(i-1))
       +            P(i-1)+=n;
       +        else
       +            inslist(r, i, n);
       +    }else if(!T(i))     /* goes in this empty piece */
       +        P(i)+=n;
       +    else if(p==p1 && i>0 && !T(i-1))    /* special case; simplifies life */
       +        P(i-1)+=n;
       +    else if(p==p1)
       +        inslist(r, i, n);
       +    else{           /* must break piece in terminal */
       +        inslist(r, i+1, (L(i)-(p1-p))|M);
       +        inslist(r, i+1, n);
       +        P(i) = (p1-p)|M;
       +    }
        }
        
        int
        rterm(List *r, Posn p1)
        {
       -        Posn p;
       -        int i;
       +    Posn p;
       +    int i;
        
       -        for(p = 0,i = 0; i<r->nused && p+L(i)<=p1; p+=L(i++))
       -                ;
       -        if(i==r->nused && (i==0 || !T(i-1)))
       -                return 0;
       -        return T(i);
       +    for(p = 0,i = 0; i<r->nused && p+L(i)<=p1; p+=L(i++))
       +        ;
       +    if(i==r->nused && (i==0 || !T(i-1)))
       +        return 0;
       +    return T(i);
        }
        
        Range
        rdata(List *r, Posn p1, Posn n)
        {
       -        Posn p;
       -        int i;
       -        Range rg;
       +    Posn p;
       +    int i;
       +    Range rg;
        
       -        if(n==0)
       -                panic("rdata 0");
       -        for(p = 0,i = 0; i<r->nused && p+L(i)<=p1; p+=L(i++))
       -                ;
       -        if(i==r->nused)
       -                panic("rdata 1");
       -        if(T(i)){
       -                n-=L(i)-(p1-p);
       -                if(n<=0){
       -                        rg.p1 = rg.p2 = p1;
       -                        return rg;
       -                }
       -                p+=L(i++);
       -                p1 = p;
       -        }
       -        if(T(i) || i==r->nused)
       -                panic("rdata 2");
       -        if(p+L(i)<p1+n)
       -                n = L(i)-(p1-p);
       -        rg.p1 = p1;
       -        rg.p2 = p1+n;
       -        if(p!=p1){
       -                inslist(r, i+1, L(i)-(p1-p));
       -                P(i)=p1-p;
       -                i++;
       -        }
       -        if(L(i)!=n){
       -                inslist(r, i+1, L(i)-n);
       -                P(i)=n;
       -        }
       -        P(i)|=M;
       -        /* now i is set; can we merge? */
       -        if(i<r->nused-1 && T(i+1)){
       -                P(i)=(n+=L(i+1))|M;
       -                dellist(r, i+1);
       -        }
       -        if(i>0 && T(i-1)){
       -                P(i)=(n+L(i-1))|M;
       -                dellist(r, i-1);
       -        }
       -        return rg;
       +    if(n==0)
       +        panic("rdata 0");
       +    for(p = 0,i = 0; i<r->nused && p+L(i)<=p1; p+=L(i++))
       +        ;
       +    if(i==r->nused)
       +        panic("rdata 1");
       +    if(T(i)){
       +        n-=L(i)-(p1-p);
       +        if(n<=0){
       +            rg.p1 = rg.p2 = p1;
       +            return rg;
       +        }
       +        p+=L(i++);
       +        p1 = p;
       +    }
       +    if(T(i) || i==r->nused)
       +        panic("rdata 2");
       +    if(p+L(i)<p1+n)
       +        n = L(i)-(p1-p);
       +    rg.p1 = p1;
       +    rg.p2 = p1+n;
       +    if(p!=p1){
       +        inslist(r, i+1, L(i)-(p1-p));
       +        P(i)=p1-p;
       +        i++;
       +    }
       +    if(L(i)!=n){
       +        inslist(r, i+1, L(i)-n);
       +        P(i)=n;
       +    }
       +    P(i)|=M;
       +    /* now i is set; can we merge? */
       +    if(i<r->nused-1 && T(i+1)){
       +        P(i)=(n+=L(i+1))|M;
       +        dellist(r, i+1);
       +    }
       +    if(i>0 && T(i-1)){
       +        P(i)=(n+L(i-1))|M;
       +        dellist(r, i-1);
       +    }
       +    return rg;
        }
 (DIR) diff --git a/sam/regexp.c b/sam/regexp.c
       @@ -1,8 +1,8 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
        #include "sam.h"
        
       -Rangeset        sel;
       -String                lastregexp;
       +Rangeset    sel;
       +String      lastregexp;
        /*
         * Machine Information
         */
       @@ -10,72 +10,72 @@ typedef struct Inst Inst;
        
        struct Inst
        {
       -        long        type;        /* < 0x10000 ==> literal, otherwise action */
       -        union {
       -                int rsid;
       -                int rsubid;
       -                int class;
       -                struct Inst *rother;
       -                struct Inst *rright;
       -        } r;
       -        union{
       -                struct Inst *lleft;
       -                struct Inst *lnext;
       -        } l;
       +    long    type;   /* < 0x10000 ==> literal, otherwise action */
       +    union {
       +        int rsid;
       +        int rsubid;
       +        int class;
       +        struct Inst *rother;
       +        struct Inst *rright;
       +    } r;
       +    union{
       +        struct Inst *lleft;
       +        struct Inst *lnext;
       +    } l;
        };
       -#define        sid        r.rsid
       -#define        subid        r.rsubid
       -#define        rclass        r.class
       -#define        other        r.rother
       -#define        right        r.rright
       -#define        left        l.lleft
       -#define        next        l.lnext
       -
       -#define        NPROG        1024
       -Inst        program[NPROG];
       -Inst        *progp;
       -Inst        *startinst;        /* First inst. of program; might not be program[0] */
       -Inst        *bstartinst;        /* same for backwards machine */
       +#define sid r.rsid
       +#define subid   r.rsubid
       +#define rclass  r.class
       +#define other   r.rother
       +#define right   r.rright
       +#define left    l.lleft
       +#define next    l.lnext
       +
       +#define NPROG   1024
       +Inst    program[NPROG];
       +Inst    *progp;
       +Inst    *startinst; /* First inst. of program; might not be program[0] */
       +Inst    *bstartinst;    /* same for backwards machine */
        
        typedef struct Ilist Ilist;
        struct Ilist
        {
       -        Inst        *inst;                /* Instruction of the thread */
       -        Rangeset se;
       -        Posn        startp;                /* first char of match */
       +    Inst    *inst;      /* Instruction of the thread */
       +    Rangeset se;
       +    Posn    startp;     /* first char of match */
        };
        
       -#define        NLIST        128
       +#define NLIST   128
        
       -Ilist        *tl, *nl;        /* This list, next list */
       -Ilist        list[2][NLIST];
       -static        Rangeset sempty;
       +Ilist   *tl, *nl;   /* This list, next list */
       +Ilist   list[2][NLIST];
       +static  Rangeset sempty;
        
        /*
         * Actions and Tokens
         *
       - *        0x100xx are operators, value == precedence
       - *        0x200xx are tokens, i.e. operands for operators
       + *  0x100xx are operators, value == precedence
       + *  0x200xx are tokens, i.e. operands for operators
         */
       -#define        OPERATOR        0x10000        /* Bitmask of all operators */
       -#define        START                0x10000        /* Start, used for marker on stack */
       -#define        RBRA                0x10001        /* Right bracket, ) */
       -#define        LBRA                0x10002        /* Left bracket, ( */
       -#define        OR                0x10003        /* Alternation, | */
       -#define        CAT                0x10004        /* Concatentation, implicit operator */
       -#define        STAR                0x10005        /* Closure, * */
       -#define        PLUS                0x10006        /* a+ == aa* */
       -#define        QUEST                0x10007        /* a? == a|nothing, i.e. 0 or 1 a's */
       -#define        ANY                0x20000        /* Any character but newline, . */
       -#define        NOP                0x20001        /* No operation, internal use only */
       -#define        BOL                0x20002        /* Beginning of line, ^ */
       -#define        EOL                0x20003        /* End of line, $ */
       -#define        CCLASS                0x20004        /* Character class, [] */
       -#define        NCCLASS                0x20005        /* Negated character class, [^] */
       -#define        END                0x20077        /* Terminate: match found */
       -
       -#define        ISATOR                0x10000
       -#define        ISAND                0x20000
       +#define OPERATOR    0x10000 /* Bitmask of all operators */
       +#define START       0x10000 /* Start, used for marker on stack */
       +#define RBRA        0x10001 /* Right bracket, ) */
       +#define LBRA        0x10002 /* Left bracket, ( */
       +#define OR      0x10003 /* Alternation, | */
       +#define CAT     0x10004 /* Concatentation, implicit operator */
       +#define STAR        0x10005 /* Closure, * */
       +#define PLUS        0x10006 /* a+ == aa* */
       +#define QUEST       0x10007 /* a? == a|nothing, i.e. 0 or 1 a's */
       +#define ANY     0x20000 /* Any character but newline, . */
       +#define NOP     0x20001 /* No operation, internal use only */
       +#define BOL     0x20002 /* Beginning of line, ^ */
       +#define EOL     0x20003 /* End of line, $ */
       +#define CCLASS      0x20004 /* Character class, [] */
       +#define NCCLASS     0x20005 /* Negated character class, [^] */
       +#define END     0x20077 /* Terminate: match found */
       +
       +#define ISATOR      0x10000
       +#define ISAND       0x20000
        
        /*
         * Parser Information
       @@ -83,729 +83,729 @@ static        Rangeset sempty;
        typedef struct Node Node;
        struct Node
        {
       -        Inst        *first;
       -        Inst        *last;
       +    Inst    *first;
       +    Inst    *last;
        };
        
       -#define        NSTACK        20
       -Node        andstack[NSTACK];
       -Node        *andp;
       -int        atorstack[NSTACK];
       -int        *atorp;
       -int        lastwasand;        /* Last token was operand */
       -int        cursubid;
       -int        subidstack[NSTACK];
       -int        *subidp;
       -int        backwards;
       -int        nbra;
       -Rune        *exprp;                /* pointer to next character in source expression */
       -#define        DCLASS        10        /* allocation increment */
       -int        nclass;                /* number active */
       -int        Nclass;                /* high water mark */
       -Rune        **class;
       -int        negateclass;
       -
       -void        addinst(Ilist *l, Inst *inst, Rangeset *sep);
       -void        newmatch(Rangeset*);
       -void        bnewmatch(Rangeset*);
       -void        pushand(Inst*, Inst*);
       -void        pushator(int);
       -Node        *popand(int);
       -int        popator(void);
       -void        startlex(Rune*);
       -int        lex(void);
       -void        operator(int);
       -void        operand(int);
       -void        evaluntil(int);
       -void        optimize(Inst*);
       -void        bldcclass(void);
       +#define NSTACK  20
       +Node    andstack[NSTACK];
       +Node    *andp;
       +int atorstack[NSTACK];
       +int *atorp;
       +int lastwasand; /* Last token was operand */
       +int cursubid;
       +int subidstack[NSTACK];
       +int *subidp;
       +int backwards;
       +int nbra;
       +Rune    *exprp;     /* pointer to next character in source expression */
       +#define DCLASS  10  /* allocation increment */
       +int nclass;     /* number active */
       +int Nclass;     /* high water mark */
       +Rune    **class;
       +int negateclass;
       +
       +void    addinst(Ilist *l, Inst *inst, Rangeset *sep);
       +void    newmatch(Rangeset*);
       +void    bnewmatch(Rangeset*);
       +void    pushand(Inst*, Inst*);
       +void    pushator(int);
       +Node    *popand(int);
       +int popator(void);
       +void    startlex(Rune*);
       +int lex(void);
       +void    operator(int);
       +void    operand(int);
       +void    evaluntil(int);
       +void    optimize(Inst*);
       +void    bldcclass(void);
        
        void
        regerror(Err e)
        {
       -        Strzero(&lastregexp);
       -        error(e);
       +    Strzero(&lastregexp);
       +    error(e);
        }
        
        void
        regerror_c(Err e, int c)
        {
       -        Strzero(&lastregexp);
       -        error_c(e, c);
       +    Strzero(&lastregexp);
       +    error_c(e, c);
        }
        
        Inst *
        newinst(int t)
        {
       -        if(progp >= &program[NPROG])
       -                regerror(Etoolong);
       -        progp->type = t;
       -        progp->left = 0;
       -        progp->right = 0;
       -        return progp++;
       +    if(progp >= &program[NPROG])
       +        regerror(Etoolong);
       +    progp->type = t;
       +    progp->left = 0;
       +    progp->right = 0;
       +    return progp++;
        }
        
        Inst *
        realcompile(Rune *s)
        {
       -        int token;
       -
       -        startlex(s);
       -        atorp = atorstack;
       -        andp = andstack;
       -        subidp = subidstack;
       -        cursubid = 0;
       -        lastwasand = FALSE;
       -        /* Start with a low priority operator to prime parser */
       -        pushator(START-1);
       -        while((token=lex()) != END){
       -                if((token&ISATOR) == OPERATOR)
       -                        operator(token);
       -                else
       -                        operand(token);
       -        }
       -        /* Close with a low priority operator */
       -        evaluntil(START);
       -        /* Force END */
       -        operand(END);
       -        evaluntil(START);
       -        if(nbra)
       -                regerror(Eleftpar);
       -        --andp;        /* points to first and only operand */
       -        return andp->first;
       +    int token;
       +
       +    startlex(s);
       +    atorp = atorstack;
       +    andp = andstack;
       +    subidp = subidstack;
       +    cursubid = 0;
       +    lastwasand = FALSE;
       +    /* Start with a low priority operator to prime parser */
       +    pushator(START-1);
       +    while((token=lex()) != END){
       +        if((token&ISATOR) == OPERATOR)
       +            operator(token);
       +        else
       +            operand(token);
       +    }
       +    /* Close with a low priority operator */
       +    evaluntil(START);
       +    /* Force END */
       +    operand(END);
       +    evaluntil(START);
       +    if(nbra)
       +        regerror(Eleftpar);
       +    --andp; /* points to first and only operand */
       +    return andp->first;
        }
        
        void
        compile(String *s)
        {
       -        int i;
       -        Inst *oprogp;
       -
       -        if(Strcmp(s, &lastregexp)==0)
       -                return;
       -        for(i=0; i<nclass; i++)
       -                free(class[i]);
       -        nclass = 0;
       -        progp = program;
       -        backwards = FALSE;
       -        startinst = realcompile(s->s);
       -        optimize(program);
       -        oprogp = progp;
       -        backwards = TRUE;
       -        bstartinst = realcompile(s->s);
       -        optimize(oprogp);
       -        Strduplstr(&lastregexp, s);
       +    int i;
       +    Inst *oprogp;
       +
       +    if(Strcmp(s, &lastregexp)==0)
       +        return;
       +    for(i=0; i<nclass; i++)
       +        free(class[i]);
       +    nclass = 0;
       +    progp = program;
       +    backwards = FALSE;
       +    startinst = realcompile(s->s);
       +    optimize(program);
       +    oprogp = progp;
       +    backwards = TRUE;
       +    bstartinst = realcompile(s->s);
       +    optimize(oprogp);
       +    Strduplstr(&lastregexp, s);
        }
        
        void
        operand(int t)
        {
       -        Inst *i;
       -        if(lastwasand)
       -                operator(CAT);        /* catenate is implicit */
       -        i = newinst(t);
       -        if(t == CCLASS){
       -                if(negateclass)
       -                        i->type = NCCLASS;        /* UGH */
       -                i->rclass = nclass-1;                /* UGH */
       -        }
       -        pushand(i, i);
       -        lastwasand = TRUE;
       +    Inst *i;
       +    if(lastwasand)
       +        operator(CAT);  /* catenate is implicit */
       +    i = newinst(t);
       +    if(t == CCLASS){
       +        if(negateclass)
       +            i->type = NCCLASS;  /* UGH */
       +        i->rclass = nclass-1;       /* UGH */
       +    }
       +    pushand(i, i);
       +    lastwasand = TRUE;
        }
        
        void
        operator(int t)
        {
       -        if(t==RBRA && --nbra<0)
       -                regerror(Erightpar);
       -        if(t==LBRA){
       +    if(t==RBRA && --nbra<0)
       +        regerror(Erightpar);
       +    if(t==LBRA){
        /*
       - *                if(++cursubid >= NSUBEXP)
       - *                        regerror(Esubexp);
       + *      if(++cursubid >= NSUBEXP)
       + *          regerror(Esubexp);
         */
       -                cursubid++;        /* silently ignored */
       -                nbra++;
       -                if(lastwasand)
       -                        operator(CAT);
       -        }else
       -                evaluntil(t);
       -        if(t!=RBRA)
       -                pushator(t);
       -        lastwasand = FALSE;
       -        if(t==STAR || t==QUEST || t==PLUS || t==RBRA)
       -                lastwasand = TRUE;        /* these look like operands */
       +        cursubid++; /* silently ignored */
       +        nbra++;
       +        if(lastwasand)
       +            operator(CAT);
       +    }else
       +        evaluntil(t);
       +    if(t!=RBRA)
       +        pushator(t);
       +    lastwasand = FALSE;
       +    if(t==STAR || t==QUEST || t==PLUS || t==RBRA)
       +        lastwasand = TRUE;  /* these look like operands */
        }
        
        void
        cant(char *s)
        {
       -        char buf[100];
       +    char buf[100];
        
       -        sprint(buf, "regexp: can't happen: %s", s);
       -        panic(buf);
       +    sprint(buf, "regexp: can't happen: %s", s);
       +    panic(buf);
        }
        
        void
        pushand(Inst *f, Inst *l)
        {
       -        if(andp >= &andstack[NSTACK])
       -                cant("operand stack overflow");
       -        andp->first = f;
       -        andp->last = l;
       -        andp++;
       +    if(andp >= &andstack[NSTACK])
       +        cant("operand stack overflow");
       +    andp->first = f;
       +    andp->last = l;
       +    andp++;
        }
        
        void
        pushator(int t)
        {
       -        if(atorp >= &atorstack[NSTACK])
       -                cant("operator stack overflow");
       -        *atorp++=t;
       -        if(cursubid >= NSUBEXP)
       -                *subidp++= -1;
       -        else
       -                *subidp++=cursubid;
       +    if(atorp >= &atorstack[NSTACK])
       +        cant("operator stack overflow");
       +    *atorp++=t;
       +    if(cursubid >= NSUBEXP)
       +        *subidp++= -1;
       +    else
       +        *subidp++=cursubid;
        }
        
        Node *
        popand(int op)
        {
       -        if(andp <= &andstack[0])
       -                if(op)
       -                        regerror_c(Emissop, op);
       -                else
       -                        regerror(Ebadregexp);
       -        return --andp;
       +    if(andp <= &andstack[0])
       +        if(op)
       +            regerror_c(Emissop, op);
       +        else
       +            regerror(Ebadregexp);
       +    return --andp;
        }
        
        int
        popator(void)
        {
       -        if(atorp <= &atorstack[0])
       -                cant("operator stack underflow");
       -        --subidp;
       -        return *--atorp;
       +    if(atorp <= &atorstack[0])
       +        cant("operator stack underflow");
       +    --subidp;
       +    return *--atorp;
        }
        
        void
        evaluntil(int pri)
        {
       -        Node *op1, *op2, *t;
       -        Inst *inst1, *inst2;
       -
       -        while(pri==RBRA || atorp[-1]>=pri){
       -                switch(popator()){
       -                case LBRA:
       -                        op1 = popand('(');
       -                        inst2 = newinst(RBRA);
       -                        inst2->subid = *subidp;
       -                        op1->last->next = inst2;
       -                        inst1 = newinst(LBRA);
       -                        inst1->subid = *subidp;
       -                        inst1->next = op1->first;
       -                        pushand(inst1, inst2);
       -                        return;                /* must have been RBRA */
       -                default:
       -                        panic("unknown regexp operator");
       -                        break;
       -                case OR:
       -                        op2 = popand('|');
       -                        op1 = popand('|');
       -                        inst2 = newinst(NOP);
       -                        op2->last->next = inst2;
       -                        op1->last->next = inst2;
       -                        inst1 = newinst(OR);
       -                        inst1->right = op1->first;
       -                        inst1->left = op2->first;
       -                        pushand(inst1, inst2);
       -                        break;
       -                case CAT:
       -                        op2 = popand(0);
       -                        op1 = popand(0);
       -                        if(backwards && op2->first->type!=END)
       -                                t = op1, op1 = op2, op2 = t;
       -                        op1->last->next = op2->first;
       -                        pushand(op1->first, op2->last);
       -                        break;
       -                case STAR:
       -                        op2 = popand('*');
       -                        inst1 = newinst(OR);
       -                        op2->last->next = inst1;
       -                        inst1->right = op2->first;
       -                        pushand(inst1, inst1);
       -                        break;
       -                case PLUS:
       -                        op2 = popand('+');
       -                        inst1 = newinst(OR);
       -                        op2->last->next = inst1;
       -                        inst1->right = op2->first;
       -                        pushand(op2->first, inst1);
       -                        break;
       -                case QUEST:
       -                        op2 = popand('?');
       -                        inst1 = newinst(OR);
       -                        inst2 = newinst(NOP);
       -                        inst1->left = inst2;
       -                        inst1->right = op2->first;
       -                        op2->last->next = inst2;
       -                        pushand(inst1, inst2);
       -                        break;
       -                }
       -        }
       +    Node *op1, *op2, *t;
       +    Inst *inst1, *inst2;
       +
       +    while(pri==RBRA || atorp[-1]>=pri){
       +        switch(popator()){
       +        case LBRA:
       +            op1 = popand('(');
       +            inst2 = newinst(RBRA);
       +            inst2->subid = *subidp;
       +            op1->last->next = inst2;
       +            inst1 = newinst(LBRA);
       +            inst1->subid = *subidp;
       +            inst1->next = op1->first;
       +            pushand(inst1, inst2);
       +            return;     /* must have been RBRA */
       +        default:
       +            panic("unknown regexp operator");
       +            break;
       +        case OR:
       +            op2 = popand('|');
       +            op1 = popand('|');
       +            inst2 = newinst(NOP);
       +            op2->last->next = inst2;
       +            op1->last->next = inst2;
       +            inst1 = newinst(OR);
       +            inst1->right = op1->first;
       +            inst1->left = op2->first;
       +            pushand(inst1, inst2);
       +            break;
       +        case CAT:
       +            op2 = popand(0);
       +            op1 = popand(0);
       +            if(backwards && op2->first->type!=END)
       +                t = op1, op1 = op2, op2 = t;
       +            op1->last->next = op2->first;
       +            pushand(op1->first, op2->last);
       +            break;
       +        case STAR:
       +            op2 = popand('*');
       +            inst1 = newinst(OR);
       +            op2->last->next = inst1;
       +            inst1->right = op2->first;
       +            pushand(inst1, inst1);
       +            break;
       +        case PLUS:
       +            op2 = popand('+');
       +            inst1 = newinst(OR);
       +            op2->last->next = inst1;
       +            inst1->right = op2->first;
       +            pushand(op2->first, inst1);
       +            break;
       +        case QUEST:
       +            op2 = popand('?');
       +            inst1 = newinst(OR);
       +            inst2 = newinst(NOP);
       +            inst1->left = inst2;
       +            inst1->right = op2->first;
       +            op2->last->next = inst2;
       +            pushand(inst1, inst2);
       +            break;
       +        }
       +    }
        }
        
        
        void
        optimize(Inst *start)
        {
       -        Inst *inst, *target;
       -
       -        for(inst=start; inst->type!=END; inst++){
       -                target = inst->next;
       -                while(target->type == NOP)
       -                        target = target->next;
       -                inst->next = target;
       -        }
       +    Inst *inst, *target;
       +
       +    for(inst=start; inst->type!=END; inst++){
       +        target = inst->next;
       +        while(target->type == NOP)
       +            target = target->next;
       +        inst->next = target;
       +    }
        }
        
       -#ifdef        DEBUG
       +#ifdef  DEBUG
        void
        dumpstack(void){
       -        Node *stk;
       -        int *ip;
       -
       -        dprint("operators\n");
       -        for(ip = atorstack; ip<atorp; ip++)
       -                dprint("0%o\n", *ip);
       -        dprint("operands\n");
       -        for(stk = andstack; stk<andp; stk++)
       -                dprint("0%o\t0%o\n", stk->first->type, stk->last->type);
       +    Node *stk;
       +    int *ip;
       +
       +    dprint("operators\n");
       +    for(ip = atorstack; ip<atorp; ip++)
       +        dprint("0%o\n", *ip);
       +    dprint("operands\n");
       +    for(stk = andstack; stk<andp; stk++)
       +        dprint("0%o\t0%o\n", stk->first->type, stk->last->type);
        }
        void
        dump(void){
       -        Inst *l;
       +    Inst *l;
        
       -        l = program;
       -        do{
       -                dprint("%d:\t0%o\t%d\t%d\n", l-program, l->type,
       -                        l->left-program, l->right-program);
       -        }while(l++->type);
       +    l = program;
       +    do{
       +        dprint("%d:\t0%o\t%d\t%d\n", l-program, l->type,
       +            l->left-program, l->right-program);
       +    }while(l++->type);
        }
        #endif
        
        void
        startlex(Rune *s)
        {
       -        exprp = s;
       -        nbra = 0;
       +    exprp = s;
       +    nbra = 0;
        }
        
        
        int
        lex(void){
       -        int c= *exprp++;
       -
       -        switch(c){
       -        case '\\':
       -                if(*exprp)
       -                        if((c= *exprp++)=='n')
       -                                c='\n';
       -                break;
       -        case 0:
       -                c = END;
       -                --exprp;        /* In case we come here again */
       -                break;
       -        case '*':
       -                c = STAR;
       -                break;
       -        case '?':
       -                c = QUEST;
       -                break;
       -        case '+':
       -                c = PLUS;
       -                break;
       -        case '|':
       -                c = OR;
       -                break;
       -        case '.':
       -                c = ANY;
       -                break;
       -        case '(':
       -                c = LBRA;
       -                break;
       -        case ')':
       -                c = RBRA;
       -                break;
       -        case '^':
       -                c = BOL;
       -                break;
       -        case '$':
       -                c = EOL;
       -                break;
       -        case '[':
       -                c = CCLASS;
       -                bldcclass();
       -                break;
       -        }
       -        return c;
       +    int c= *exprp++;
       +
       +    switch(c){
       +    case '\\':
       +        if(*exprp)
       +            if((c= *exprp++)=='n')
       +                c='\n';
       +        break;
       +    case 0:
       +        c = END;
       +        --exprp;    /* In case we come here again */
       +        break;
       +    case '*':
       +        c = STAR;
       +        break;
       +    case '?':
       +        c = QUEST;
       +        break;
       +    case '+':
       +        c = PLUS;
       +        break;
       +    case '|':
       +        c = OR;
       +        break;
       +    case '.':
       +        c = ANY;
       +        break;
       +    case '(':
       +        c = LBRA;
       +        break;
       +    case ')':
       +        c = RBRA;
       +        break;
       +    case '^':
       +        c = BOL;
       +        break;
       +    case '$':
       +        c = EOL;
       +        break;
       +    case '[':
       +        c = CCLASS;
       +        bldcclass();
       +        break;
       +    }
       +    return c;
        }
        
        long
        nextrec(void){
       -        if(exprp[0]==0 || (exprp[0]=='\\' && exprp[1]==0))
       -                regerror(Ebadclass);
       -        if(exprp[0] == '\\'){
       -                exprp++;
       -                if(*exprp=='n'){
       -                        exprp++;
       -                        return '\n';
       -                }
       -                return *exprp++|0x10000;
       -        }
       -        return *exprp++;
       +    if(exprp[0]==0 || (exprp[0]=='\\' && exprp[1]==0))
       +        regerror(Ebadclass);
       +    if(exprp[0] == '\\'){
       +        exprp++;
       +        if(*exprp=='n'){
       +            exprp++;
       +            return '\n';
       +        }
       +        return *exprp++|0x10000;
       +    }
       +    return *exprp++;
        }
        
        void
        bldcclass(void)
        {
       -        long c1, c2, n, na;
       -        Rune *classp;
       -
       -        classp = emalloc(DCLASS*RUNESIZE);
       -        n = 0;
       -        na = DCLASS;
       -        /* we have already seen the '[' */
       -        if(*exprp == '^'){
       -                classp[n++] = '\n';        /* don't match newline in negate case */
       -                negateclass = TRUE;
       -                exprp++;
       -        }else
       -                negateclass = FALSE;
       -        while((c1 = nextrec()) != ']'){
       -                if(c1 == '-'){
       +    long c1, c2, n, na;
       +    Rune *classp;
       +
       +    classp = emalloc(DCLASS*RUNESIZE);
       +    n = 0;
       +    na = DCLASS;
       +    /* we have already seen the '[' */
       +    if(*exprp == '^'){
       +        classp[n++] = '\n'; /* don't match newline in negate case */
       +        negateclass = TRUE;
       +        exprp++;
       +    }else
       +        negateclass = FALSE;
       +    while((c1 = nextrec()) != ']'){
       +        if(c1 == '-'){
            Error:
       -                        free(classp);
       -                        regerror(Ebadclass);
       -                }
       -                if(n+4 >= na){                /* 3 runes plus NUL */
       -                        na += DCLASS;
       -                        classp = erealloc(classp, na*RUNESIZE);
       -                }
       -                if(*exprp == '-'){
       -                        exprp++;        /* eat '-' */
       -                        if((c2 = nextrec()) == ']')
       -                                goto Error;
       -                        classp[n+0] = 0xFFFF;
       -                        classp[n+1] = c1;
       -                        classp[n+2] = c2;
       -                        n += 3;
       -                }else
       -                        classp[n++] = c1;
       -        }
       -        classp[n] = 0;
       -        if(nclass == Nclass){
       -                Nclass += DCLASS;
       -                class = erealloc(class, Nclass*sizeof(Rune*));
       -        }
       -        class[nclass++] = classp;
       +            free(classp);
       +            regerror(Ebadclass);
       +        }
       +        if(n+4 >= na){      /* 3 runes plus NUL */
       +            na += DCLASS;
       +            classp = erealloc(classp, na*RUNESIZE);
       +        }
       +        if(*exprp == '-'){
       +            exprp++;    /* eat '-' */
       +            if((c2 = nextrec()) == ']')
       +                goto Error;
       +            classp[n+0] = 0xFFFF;
       +            classp[n+1] = c1;
       +            classp[n+2] = c2;
       +            n += 3;
       +        }else
       +            classp[n++] = c1;
       +    }
       +    classp[n] = 0;
       +    if(nclass == Nclass){
       +        Nclass += DCLASS;
       +        class = erealloc(class, Nclass*sizeof(Rune*));
       +    }
       +    class[nclass++] = classp;
        }
        
        int
        classmatch(int classno, int c, int negate)
        {
       -        Rune *p;
       -
       -        p = class[classno];
       -        while(*p){
       -                if(*p == 0xFFFF){
       -                        if(p[1]<=c && c<=p[2])
       -                                return !negate;
       -                        p += 3;
       -                }else if(*p++ == c)
       -                        return !negate;
       -        }
       -        return negate;
       +    Rune *p;
       +
       +    p = class[classno];
       +    while(*p){
       +        if(*p == 0xFFFF){
       +            if(p[1]<=c && c<=p[2])
       +                return !negate;
       +            p += 3;
       +        }else if(*p++ == c)
       +            return !negate;
       +    }
       +    return negate;
        }
        
        /*
         * Note optimization in addinst:
       - *         *l must be pending when addinst called; if *l has been looked
       - *                at already, the optimization is a bug.
       + *  *l must be pending when addinst called; if *l has been looked
       + *      at already, the optimization is a bug.
         */
        void
        addinst(Ilist *l, Inst *inst, Rangeset *sep)
        {
       -        Ilist *p;
       -
       -        for(p = l; p->inst; p++){
       -                if(p->inst==inst){
       -                        if((sep)->p[0].p1 < p->se.p[0].p1)
       -                                p->se= *sep;        /* this would be bug */
       -                        return;        /* It's already there */
       -                }
       -        }
       -        p->inst = inst;
       -        p->se= *sep;
       -        (p+1)->inst = 0;
       +    Ilist *p;
       +
       +    for(p = l; p->inst; p++){
       +        if(p->inst==inst){
       +            if((sep)->p[0].p1 < p->se.p[0].p1)
       +                p->se= *sep;    /* this would be bug */
       +            return; /* It's already there */
       +        }
       +    }
       +    p->inst = inst;
       +    p->se= *sep;
       +    (p+1)->inst = 0;
        }
        
        int
        execute(File *f, Posn startp, Posn eof)
        {
       -        int flag = 0;
       -        Inst *inst;
       -        Ilist *tlp;
       -        Posn p = startp;
       -        int nnl = 0, ntl;
       -        int c;
       -        int wrapped = 0;
       -        int startchar = startinst->type<OPERATOR? startinst->type : 0;
       -
       -        list[0][0].inst = list[1][0].inst = 0;
       -        sel.p[0].p1 = -1;
       -        Fgetcset(f, startp);
       -        /* Execute machine once for each character */
       -        for(;;p++){
       -        doloop:
       -                c = Fgetc(f);
       -                if(p>=eof || c<0){
       -                        switch(wrapped++){
       -                        case 0:                /* let loop run one more click */
       -                        case 2:
       -                                break;
       -                        case 1:                /* expired; wrap to beginning */
       -                                if(sel.p[0].p1>=0 || eof!=INFINITY)
       -                                        goto Return;
       -                                list[0][0].inst = list[1][0].inst = 0;
       -                                Fgetcset(f, (Posn)0);
       -                                p = 0;
       -                                goto doloop;
       -                        default:
       -                                goto Return;
       -                        }
       -                }else if(((wrapped && p>=startp) || sel.p[0].p1>0) && nnl==0)
       -                        break;
       -                /* fast check for first char */
       -                if(startchar && nnl==0 && c!=startchar)
       -                        continue;
       -                tl = list[flag];
       -                nl = list[flag^=1];
       -                nl->inst = 0;
       -                ntl = nnl;
       -                nnl = 0;
       -                if(sel.p[0].p1<0 && (!wrapped || p<startp || startp==eof)){
       -                        /* Add first instruction to this list */
       -                        if(++ntl >= NLIST)
       -        Overflow:
       -                                error(Eoverflow);
       -                        sempty.p[0].p1 = p;
       -                        addinst(tl, startinst, &sempty);
       -                }
       -                /* Execute machine until this list is empty */
       -                for(tlp = tl; inst = tlp->inst; tlp++){        /* assignment = */
       -        Switchstmt:
       -                        switch(inst->type){
       -                        default:        /* regular character */
       -                                if(inst->type==c){
       -        Addinst:
       -                                        if(++nnl >= NLIST)
       -                                                goto Overflow;
       -                                        addinst(nl, inst->next, &tlp->se);
       -                                }
       -                                break;
       -                        case LBRA:
       -                                if(inst->subid>=0)
       -                                        tlp->se.p[inst->subid].p1 = p;
       -                                inst = inst->next;
       -                                goto Switchstmt;
       -                        case RBRA:
       -                                if(inst->subid>=0)
       -                                        tlp->se.p[inst->subid].p2 = p;
       -                                inst = inst->next;
       -                                goto Switchstmt;
       -                        case ANY:
       -                                if(c!='\n')
       -                                        goto Addinst;
       -                                break;
       -                        case BOL:
       -                                if(p == 0){
       -        Step:
       -                                        inst = inst->next;
       -                                        goto Switchstmt;
       -                                }
       -                                if(f->getci > 1){
       -                                        if(f->getcbuf[f->getci-2]=='\n')
       -                                                goto Step;
       -                                }else{
       -                                        Rune c;
       -                                        if(Fchars(f, &c, p-1, p)==1 && c=='\n')
       -                                                goto Step;
       -                                }
       -                                break;
       -                        case EOL:
       -                                if(c == '\n')
       -                                        goto Step;
       -                                break;
       -                        case CCLASS:
       -                                if(c>=0 && classmatch(inst->rclass, c, 0))
       -                                        goto Addinst;
       -                                break;
       -                        case NCCLASS:
       -                                if(c>=0 && classmatch(inst->rclass, c, 1))
       -                                        goto Addinst;
       -                                break;
       -                        case OR:
       -                                /* evaluate right choice later */
       -                                if(++ntl >= NLIST)
       -                                        goto Overflow;
       -                                addinst(tlp, inst->right, &tlp->se);
       -                                /* efficiency: advance and re-evaluate */
       -                                inst = inst->left;
       -                                goto Switchstmt;
       -                        case END:        /* Match! */
       -                                tlp->se.p[0].p2 = p;
       -                                newmatch(&tlp->se);
       -                                break;
       -                        }
       -                }
       -        }
       +    int flag = 0;
       +    Inst *inst;
       +    Ilist *tlp;
       +    Posn p = startp;
       +    int nnl = 0, ntl;
       +    int c;
       +    int wrapped = 0;
       +    int startchar = startinst->type<OPERATOR? startinst->type : 0;
       +
       +    list[0][0].inst = list[1][0].inst = 0;
       +    sel.p[0].p1 = -1;
       +    Fgetcset(f, startp);
       +    /* Execute machine once for each character */
       +    for(;;p++){
       +    doloop:
       +        c = Fgetc(f);
       +        if(p>=eof || c<0){
       +            switch(wrapped++){
       +            case 0:     /* let loop run one more click */
       +            case 2:
       +                break;
       +            case 1:     /* expired; wrap to beginning */
       +                if(sel.p[0].p1>=0 || eof!=INFINITY)
       +                    goto Return;
       +                list[0][0].inst = list[1][0].inst = 0;
       +                Fgetcset(f, (Posn)0);
       +                p = 0;
       +                goto doloop;
       +            default:
       +                goto Return;
       +            }
       +        }else if(((wrapped && p>=startp) || sel.p[0].p1>0) && nnl==0)
       +            break;
       +        /* fast check for first char */
       +        if(startchar && nnl==0 && c!=startchar)
       +            continue;
       +        tl = list[flag];
       +        nl = list[flag^=1];
       +        nl->inst = 0;
       +        ntl = nnl;
       +        nnl = 0;
       +        if(sel.p[0].p1<0 && (!wrapped || p<startp || startp==eof)){
       +            /* Add first instruction to this list */
       +            if(++ntl >= NLIST)
       +    Overflow:
       +                error(Eoverflow);
       +            sempty.p[0].p1 = p;
       +            addinst(tl, startinst, &sempty);
       +        }
       +        /* Execute machine until this list is empty */
       +        for(tlp = tl; inst = tlp->inst; tlp++){ /* assignment = */
       +    Switchstmt:
       +            switch(inst->type){
       +            default:    /* regular character */
       +                if(inst->type==c){
       +    Addinst:
       +                    if(++nnl >= NLIST)
       +                        goto Overflow;
       +                    addinst(nl, inst->next, &tlp->se);
       +                }
       +                break;
       +            case LBRA:
       +                if(inst->subid>=0)
       +                    tlp->se.p[inst->subid].p1 = p;
       +                inst = inst->next;
       +                goto Switchstmt;
       +            case RBRA:
       +                if(inst->subid>=0)
       +                    tlp->se.p[inst->subid].p2 = p;
       +                inst = inst->next;
       +                goto Switchstmt;
       +            case ANY:
       +                if(c!='\n')
       +                    goto Addinst;
       +                break;
       +            case BOL:
       +                if(p == 0){
       +    Step:
       +                    inst = inst->next;
       +                    goto Switchstmt;
       +                }
       +                if(f->getci > 1){
       +                    if(f->getcbuf[f->getci-2]=='\n')
       +                        goto Step;
       +                }else{
       +                    Rune c;
       +                    if(Fchars(f, &c, p-1, p)==1 && c=='\n')
       +                        goto Step;
       +                }
       +                break;
       +            case EOL:
       +                if(c == '\n')
       +                    goto Step;
       +                break;
       +            case CCLASS:
       +                if(c>=0 && classmatch(inst->rclass, c, 0))
       +                    goto Addinst;
       +                break;
       +            case NCCLASS:
       +                if(c>=0 && classmatch(inst->rclass, c, 1))
       +                    goto Addinst;
       +                break;
       +            case OR:
       +                /* evaluate right choice later */
       +                if(++ntl >= NLIST)
       +                    goto Overflow;
       +                addinst(tlp, inst->right, &tlp->se);
       +                /* efficiency: advance and re-evaluate */
       +                inst = inst->left;
       +                goto Switchstmt;
       +            case END:   /* Match! */
       +                tlp->se.p[0].p2 = p;
       +                newmatch(&tlp->se);
       +                break;
       +            }
       +        }
       +    }
            Return:
       -        return sel.p[0].p1>=0;
       +    return sel.p[0].p1>=0;
        }
        
        void
        newmatch(Rangeset *sp)
        {
       -        int i;
       +    int i;
        
       -        if(sel.p[0].p1<0 || sp->p[0].p1<sel.p[0].p1 ||
       -           (sp->p[0].p1==sel.p[0].p1 && sp->p[0].p2>sel.p[0].p2))
       -                for(i = 0; i<NSUBEXP; i++)
       -                        sel.p[i] = sp->p[i];
       +    if(sel.p[0].p1<0 || sp->p[0].p1<sel.p[0].p1 ||
       +       (sp->p[0].p1==sel.p[0].p1 && sp->p[0].p2>sel.p[0].p2))
       +        for(i = 0; i<NSUBEXP; i++)
       +            sel.p[i] = sp->p[i];
        }
        
        int
        bexecute(File *f, Posn startp)
        {
       -        int flag = 0;
       -        Inst *inst;
       -        Ilist *tlp;
       -        Posn p = startp;
       -        int nnl = 0, ntl;
       -        int c;
       -        int wrapped = 0;
       -        int startchar = bstartinst->type<OPERATOR? bstartinst->type : 0;
       -
       -        list[0][0].inst = list[1][0].inst = 0;
       -        sel.p[0].p1= -1;
       -        Fgetcset(f, startp);
       -        /* Execute machine once for each character, including terminal NUL */
       -        for(;;--p){
       -        doloop:
       -                if((c = Fbgetc(f))==-1){
       -                        switch(wrapped++){
       -                        case 0:                /* let loop run one more click */
       -                        case 2:
       -                                break;
       -                        case 1:                /* expired; wrap to end */
       -                                if(sel.p[0].p1>=0)
       -                        case 3:
       -                                        goto Return;
       -                                list[0][0].inst = list[1][0].inst = 0;
       -                                Fgetcset(f, f->nrunes);
       -                                p = f->nrunes;
       -                                goto doloop;
       -                        default:
       -                                goto Return;
       -                        }
       -                }else if(((wrapped && p<=startp) || sel.p[0].p1>0) && nnl==0)
       -                        break;
       -                /* fast check for first char */
       -                if(startchar && nnl==0 && c!=startchar)
       -                        continue;
       -                tl = list[flag];
       -                nl = list[flag^=1];
       -                nl->inst = 0;
       -                ntl = nnl;
       -                nnl = 0;
       -                if(sel.p[0].p1<0 && (!wrapped || p>startp)){
       -                        /* Add first instruction to this list */
       -                        if(++ntl >= NLIST)
       -        Overflow:
       -                                error(Eoverflow);
       -                        /* the minus is so the optimizations in addinst work */
       -                        sempty.p[0].p1 = -p;
       -                        addinst(tl, bstartinst, &sempty);
       -                }
       -                /* Execute machine until this list is empty */
       -                for(tlp = tl; inst = tlp->inst; tlp++){        /* assignment = */
       -        Switchstmt:
       -                        switch(inst->type){
       -                        default:        /* regular character */
       -                                if(inst->type == c){
       -        Addinst:
       -                                        if(++nnl >= NLIST)
       -                                                goto Overflow;
       -                                        addinst(nl, inst->next, &tlp->se);
       -                                }
       -                                break;
       -                        case LBRA:
       -                                if(inst->subid>=0)
       -                                        tlp->se.p[inst->subid].p1 = p;
       -                                inst = inst->next;
       -                                goto Switchstmt;
       -                        case RBRA:
       -                                if(inst->subid >= 0)
       -                                        tlp->se.p[inst->subid].p2 = p;
       -                                inst = inst->next;
       -                                goto Switchstmt;
       -                        case ANY:
       -                                if(c != '\n')
       -                                        goto Addinst;
       -                                break;
       -                        case BOL:
       -                                if(c=='\n' || p==0){
       -        Step:
       -                                        inst = inst->next;
       -                                        goto Switchstmt;
       -                                }
       -                                break;
       -                        case EOL:
       -                                if(f->getci<f->ngetc-1){
       -                                        if(f->getcbuf[f->getci+1]=='\n')
       -                                                goto Step;
       -                                }else if(p<f->nrunes-1){
       -                                        Rune c;
       -                                        if(Fchars(f, &c, p, p+1)==1 && c=='\n')
       -                                                goto Step;
       -                                }
       -                                break;
       -                        case CCLASS:
       -                                if(c>=0 && classmatch(inst->rclass, c, 0))
       -                                        goto Addinst;
       -                                break;
       -                        case NCCLASS:
       -                                if(c>=0 && classmatch(inst->rclass, c, 1))
       -                                        goto Addinst;
       -                                break;
       -                        case OR:
       -                                /* evaluate right choice later */
       -                                if(++ntl >= NLIST)
       -                                        goto Overflow;
       -                                addinst(tlp, inst->right, &tlp->se);
       -                                /* efficiency: advance and re-evaluate */
       -                                inst = inst->left;
       -                                goto Switchstmt;
       -                        case END:        /* Match! */
       -                                tlp->se.p[0].p1 = -tlp->se.p[0].p1; /* minus sign */
       -                                tlp->se.p[0].p2 = p;
       -                                bnewmatch(&tlp->se);
       -                                break;
       -                        }
       -                }
       -        }
       +    int flag = 0;
       +    Inst *inst;
       +    Ilist *tlp;
       +    Posn p = startp;
       +    int nnl = 0, ntl;
       +    int c;
       +    int wrapped = 0;
       +    int startchar = bstartinst->type<OPERATOR? bstartinst->type : 0;
       +
       +    list[0][0].inst = list[1][0].inst = 0;
       +    sel.p[0].p1= -1;
       +    Fgetcset(f, startp);
       +    /* Execute machine once for each character, including terminal NUL */
       +    for(;;--p){
       +    doloop:
       +        if((c = Fbgetc(f))==-1){
       +            switch(wrapped++){
       +            case 0:     /* let loop run one more click */
       +            case 2:
       +                break;
       +            case 1:     /* expired; wrap to end */
       +                if(sel.p[0].p1>=0)
       +            case 3:
       +                    goto Return;
       +                list[0][0].inst = list[1][0].inst = 0;
       +                Fgetcset(f, f->nrunes);
       +                p = f->nrunes;
       +                goto doloop;
       +            default:
       +                goto Return;
       +            }
       +        }else if(((wrapped && p<=startp) || sel.p[0].p1>0) && nnl==0)
       +            break;
       +        /* fast check for first char */
       +        if(startchar && nnl==0 && c!=startchar)
       +            continue;
       +        tl = list[flag];
       +        nl = list[flag^=1];
       +        nl->inst = 0;
       +        ntl = nnl;
       +        nnl = 0;
       +        if(sel.p[0].p1<0 && (!wrapped || p>startp)){
       +            /* Add first instruction to this list */
       +            if(++ntl >= NLIST)
       +    Overflow:
       +                error(Eoverflow);
       +            /* the minus is so the optimizations in addinst work */
       +            sempty.p[0].p1 = -p;
       +            addinst(tl, bstartinst, &sempty);
       +        }
       +        /* Execute machine until this list is empty */
       +        for(tlp = tl; inst = tlp->inst; tlp++){ /* assignment = */
       +    Switchstmt:
       +            switch(inst->type){
       +            default:    /* regular character */
       +                if(inst->type == c){
       +    Addinst:
       +                    if(++nnl >= NLIST)
       +                        goto Overflow;
       +                    addinst(nl, inst->next, &tlp->se);
       +                }
       +                break;
       +            case LBRA:
       +                if(inst->subid>=0)
       +                    tlp->se.p[inst->subid].p1 = p;
       +                inst = inst->next;
       +                goto Switchstmt;
       +            case RBRA:
       +                if(inst->subid >= 0)
       +                    tlp->se.p[inst->subid].p2 = p;
       +                inst = inst->next;
       +                goto Switchstmt;
       +            case ANY:
       +                if(c != '\n')
       +                    goto Addinst;
       +                break;
       +            case BOL:
       +                if(c=='\n' || p==0){
       +    Step:
       +                    inst = inst->next;
       +                    goto Switchstmt;
       +                }
       +                break;
       +            case EOL:
       +                if(f->getci<f->ngetc-1){
       +                    if(f->getcbuf[f->getci+1]=='\n')
       +                        goto Step;
       +                }else if(p<f->nrunes-1){
       +                    Rune c;
       +                    if(Fchars(f, &c, p, p+1)==1 && c=='\n')
       +                        goto Step;
       +                }
       +                break;
       +            case CCLASS:
       +                if(c>=0 && classmatch(inst->rclass, c, 0))
       +                    goto Addinst;
       +                break;
       +            case NCCLASS:
       +                if(c>=0 && classmatch(inst->rclass, c, 1))
       +                    goto Addinst;
       +                break;
       +            case OR:
       +                /* evaluate right choice later */
       +                if(++ntl >= NLIST)
       +                    goto Overflow;
       +                addinst(tlp, inst->right, &tlp->se);
       +                /* efficiency: advance and re-evaluate */
       +                inst = inst->left;
       +                goto Switchstmt;
       +            case END:   /* Match! */
       +                tlp->se.p[0].p1 = -tlp->se.p[0].p1; /* minus sign */
       +                tlp->se.p[0].p2 = p;
       +                bnewmatch(&tlp->se);
       +                break;
       +            }
       +        }
       +    }
            Return:
       -        return sel.p[0].p1>=0;
       +    return sel.p[0].p1>=0;
        }
        
        void
 (DIR) diff --git a/sam/sam.c b/sam/sam.c
       @@ -3,44 +3,44 @@
        
        #include <unistd.h>
        
       -Rune        genbuf[BLOCKSIZE];
       -int        io;
       -int        panicking;
       -int        rescuing;
       -Mod        modnum;
       -String        genstr;
       -String        rhs;
       -String        wd;
       -String        cmdstr;
       -Rune        empty[] = { 0 };
       -char        *genc;
       -File        *curfile;
       -File        *flist;
       -File        *cmd;
       -jmp_buf        mainloop;
       +Rune    genbuf[BLOCKSIZE];
       +int io;
       +int panicking;
       +int rescuing;
       +Mod modnum;
       +String  genstr;
       +String  rhs;
       +String  wd;
       +String  cmdstr;
       +Rune    empty[] = { 0 };
       +char    *genc;
       +File    *curfile;
       +File    *flist;
       +File    *cmd;
       +jmp_buf mainloop;
        List tempfile;
       -int        quitok = TRUE;
       -int        downloaded;
       +int quitok = TRUE;
       +int downloaded;
        int expandtabs;
       -int        dflag;
       -int        Rflag;
       -char        *machine;
       -char        *home;
       -int        bpipeok;
       -int        termlocked;
       -char        *samterm = "samterm";
       -char        *rsamname = "rsam";
       +int dflag;
       +int Rflag;
       +char    *machine;
       +char    *home;
       +int bpipeok;
       +int termlocked;
       +char    *samterm = "samterm";
       +char    *rsamname = "rsam";
        
       -Rune        baddir[] = { '<', 'b', 'a', 'd', 'd', 'i', 'r', '>', '\n'};
       +Rune    baddir[] = { '<', 'b', 'a', 'd', 'd', 'i', 'r', '>', '\n'};
        
       -void        usage(void);
       +void    usage(void);
        
        int
        main(int argc, char *argv[])
        {
       -        int i, o;
       -        String *t;
       -        char *arg[argc + 1], **ap;
       +    int i, o;
       +    String *t;
       +    char *arg[argc + 1], **ap;
            int targc = 1;
        
            ap = &arg[argc];
       @@ -87,68 +87,68 @@ main(int argc, char *argv[])
            argc -= optind;
            arg[targc] = NULL;
        
       -        Strinit(&cmdstr);
       -        Strinit0(&lastpat);
       -        Strinit0(&lastregexp);
       -        Strinit0(&genstr);
       -        Strinit0(&rhs);
       -        Strinit0(&wd);
       -        tempfile.listptr = emalloc(0);
       -        Strinit0(&plan9cmd);
       -        home = getenv("HOME");
       -        if(home == 0)
       -                home = "/";
       -        if(!dflag)
       -                startup(machine, Rflag, arg, ap);
       -        Fstart();
       -        notify(notifyf);
       -        if(argc > 0){
       -                for(i=0; i<argc; i++)
       -                        if(!setjmp(mainloop)){
       -                                t = tmpcstr(argv[i]);
       -                                Straddc(t, '\0');
       -                                Strduplstr(&genstr, t);
       -                                freetmpstr(t);
       -                                Fsetname(newfile(), &genstr);
       -                        }
       -        }else if(!downloaded)
       -                newfile()->state = Clean;
       -        modnum++;
       -        if(file.nused)
       -                current(file.filepptr[0]);
       -        setjmp(mainloop);
       -        cmdloop();
       -        trytoquit();        /* if we already q'ed, quitok will be TRUE */
       -        exits(0);
       +    Strinit(&cmdstr);
       +    Strinit0(&lastpat);
       +    Strinit0(&lastregexp);
       +    Strinit0(&genstr);
       +    Strinit0(&rhs);
       +    Strinit0(&wd);
       +    tempfile.listptr = emalloc(0);
       +    Strinit0(&plan9cmd);
       +    home = getenv("HOME");
       +    if(home == 0)
       +        home = "/";
       +    if(!dflag)
       +        startup(machine, Rflag, arg, ap);
       +    Fstart();
       +    notify(notifyf);
       +    if(argc > 0){
       +        for(i=0; i<argc; i++)
       +            if(!setjmp(mainloop)){
       +                t = tmpcstr(argv[i]);
       +                Straddc(t, '\0');
       +                Strduplstr(&genstr, t);
       +                freetmpstr(t);
       +                Fsetname(newfile(), &genstr);
       +            }
       +    }else if(!downloaded)
       +        newfile()->state = Clean;
       +    modnum++;
       +    if(file.nused)
       +        current(file.filepptr[0]);
       +    setjmp(mainloop);
       +    cmdloop();
       +    trytoquit();    /* if we already q'ed, quitok will be TRUE */
       +    exits(0);
        }
        
        void
        usage(void)
        {
       -        dprint("usage: sam [-r machine] [-d] [-f] [-e] [-t samterm] [-s samname] FILE...\n");
       -        exits("usage");
       +    dprint("usage: sam [-r machine] [-d] [-f] [-e] [-t samterm] [-s samname] FILE...\n");
       +    exits("usage");
        }
        
        void
        rescue(void)
        {
       -        int i, nblank = 0;
       -        File *f;
       -        char *c;
       -        char buf[256];
       -
       -        if(rescuing++)
       -                return;
       -        io = -1;
       -        for(i=0; i<file.nused; i++){
       -                f = file.filepptr[i];
       -                if(f==cmd || f->nrunes==0 || f->state!=Dirty)
       -                        continue;
       -                if(io == -1){
       -                        sprint(buf, "%s/sam.save", home);
       -                        io = create(buf, 1, 0700);
       -                        if(io<0)
       -                                return;
       +    int i, nblank = 0;
       +    File *f;
       +    char *c;
       +    char buf[256];
       +
       +    if(rescuing++)
       +        return;
       +    io = -1;
       +    for(i=0; i<file.nused; i++){
       +        f = file.filepptr[i];
       +        if(f==cmd || f->nrunes==0 || f->state!=Dirty)
       +            continue;
       +        if(io == -1){
       +            sprint(buf, "%s/sam.save", home);
       +            io = create(buf, 1, 0700);
       +            if(io<0)
       +                return;
                    dprintf(io, "samsave() {\n"
                               "    echo \"${1}?\"\n"
                               "    read yn < /dev/tty\n"
       @@ -156,432 +156,432 @@ rescue(void)
                               "        [Yy]*) cat > \"${1}\"\n"
                               "    esac\n"
                               "}\n");
       -                }
       -                if(f->name.s[0]){
       -                        c = Strtoc(&f->name);
       -                        strncpy(buf, c, sizeof buf-1);
       -                        buf[sizeof buf-1] = 0;
       -                        free(c);
       -                }else
       -                        sprint(buf, "nameless.%d", nblank++);
       +        }
       +        if(f->name.s[0]){
       +            c = Strtoc(&f->name);
       +            strncpy(buf, c, sizeof buf-1);
       +            buf[sizeof buf-1] = 0;
       +            free(c);
       +        }else
       +            sprint(buf, "nameless.%d", nblank++);
                dprintf(io, "samsave %s <<'---%s'\n", buf, buf);
       -                addr.r.p1 = 0, addr.r.p2 = f->nrunes;
       -                writeio(f);
       -                dprintf(io, "\n---%s\n", (char *)buf);
       -        }
       +        addr.r.p1 = 0, addr.r.p2 = f->nrunes;
       +        writeio(f);
       +        dprintf(io, "\n---%s\n", (char *)buf);
       +    }
        }
        
        void
        panic(char *s)
        {
       -        int wasd;
       -
       -        if(!panicking++ && !setjmp(mainloop)){
       -                wasd = downloaded;
       -                downloaded = 0;
       -                dprint("sam: panic: %s\n", s);
       -                if(wasd)
       -                        fprintf(stderr, "sam: panic: %s\n", s);
       -                rescue();
       -                abort();
       -        }
       +    int wasd;
       +
       +    if(!panicking++ && !setjmp(mainloop)){
       +        wasd = downloaded;
       +        downloaded = 0;
       +        dprint("sam: panic: %s\n", s);
       +        if(wasd)
       +            fprintf(stderr, "sam: panic: %s\n", s);
       +        rescue();
       +        abort();
       +    }
        }
        
        void
        hiccough(char *s)
        {
       -        if(rescuing)
       -                exits("rescue");
       -        if(s)
       -                dprint("%s\n", s);
       -        resetcmd();
       -        resetxec();
       -        resetsys();
       -        if(io > 0)
       -                close(io);
       -        if(undobuf->nrunes)
       -                Bdelete(undobuf, (Posn)0, undobuf->nrunes);
       -        update();
       -        if (curfile) {
       -                if (curfile->state==Unread)
       -                        curfile->state = Clean;
       -                else if (downloaded)
       -                        outTs(Hcurrent, curfile->tag);
       -        }
       -        longjmp(mainloop, 1);
       +    if(rescuing)
       +        exits("rescue");
       +    if(s)
       +        dprint("%s\n", s);
       +    resetcmd();
       +    resetxec();
       +    resetsys();
       +    if(io > 0)
       +        close(io);
       +    if(undobuf->nrunes)
       +        Bdelete(undobuf, (Posn)0, undobuf->nrunes);
       +    update();
       +    if (curfile) {
       +        if (curfile->state==Unread)
       +            curfile->state = Clean;
       +        else if (downloaded)
       +            outTs(Hcurrent, curfile->tag);
       +    }
       +    longjmp(mainloop, 1);
        }
        
        void
        intr(void)
        {
       -        error(Eintr);
       +    error(Eintr);
        }
        
        void
        trytoclose(File *f)
        {
       -        char *t;
       -        char buf[256];
       -
       -        if(f == cmd)        /* possible? */
       -                return;
       -        if(f->deleted)
       -                return;
       -        if(f->state==Dirty && !f->closeok){
       -                f->closeok = TRUE;
       -                if(f->name.s[0]){
       -                        t = Strtoc(&f->name);
       -                        strncpy(buf, t, sizeof buf-1);
       -                        free(t);
       -                }else
       -                        strcpy(buf, "nameless file");
       -                error_s(Emodified, buf);
       -        }
       -        f->deleted = TRUE;
       +    char *t;
       +    char buf[256];
       +
       +    if(f == cmd)    /* possible? */
       +        return;
       +    if(f->deleted)
       +        return;
       +    if(f->state==Dirty && !f->closeok){
       +        f->closeok = TRUE;
       +        if(f->name.s[0]){
       +            t = Strtoc(&f->name);
       +            strncpy(buf, t, sizeof buf-1);
       +            free(t);
       +        }else
       +            strcpy(buf, "nameless file");
       +        error_s(Emodified, buf);
       +    }
       +    f->deleted = TRUE;
        }
        
        void
        trytoquit(void)
        {
       -        int c;
       -        File *f;
       +    int c;
       +    File *f;
        
       -        if(!quitok)
       +    if(!quitok)
        {
       -                for(c = 0; c<file.nused; c++){
       -                        f = file.filepptr[c];
       -                        if(f!=cmd && f->state==Dirty){
       -                                quitok = TRUE;
       -                                eof = FALSE;
       -                                error(Echanges);
       -                        }
       -                }
       +        for(c = 0; c<file.nused; c++){
       +            f = file.filepptr[c];
       +            if(f!=cmd && f->state==Dirty){
       +                quitok = TRUE;
       +                eof = FALSE;
       +                error(Echanges);
       +            }
       +        }
        }
        }
        
        void
        load(File *f)
        {
       -        Address saveaddr;
       -
       -        Strduplstr(&genstr, &f->name);
       -        filename(f);
       -        if(f->name.s[0]){
       -                saveaddr = addr;
       -                edit(f, 'I');
       -                addr = saveaddr;
       -        }else
       -                f->state = Clean;
       -        Fupdate(f, TRUE, TRUE);
       +    Address saveaddr;
       +
       +    Strduplstr(&genstr, &f->name);
       +    filename(f);
       +    if(f->name.s[0]){
       +        saveaddr = addr;
       +        edit(f, 'I');
       +        addr = saveaddr;
       +    }else
       +        f->state = Clean;
       +    Fupdate(f, TRUE, TRUE);
        }
        
        void
        cmdupdate(void)
        {
       -        if(cmd && cmd->mod!=0){
       -                Fupdate(cmd, FALSE, downloaded);
       -                cmd->dot.r.p1 = cmd->dot.r.p2 = cmd->nrunes;
       -                telldot(cmd);
       -        }
       +    if(cmd && cmd->mod!=0){
       +        Fupdate(cmd, FALSE, downloaded);
       +        cmd->dot.r.p1 = cmd->dot.r.p2 = cmd->nrunes;
       +        telldot(cmd);
       +    }
        }
        
        void
        delete(File *f)
        {
       -        if(downloaded && f->rasp)
       -                outTs(Hclose, f->tag);
       -        delfile(f);
       -        if(f == curfile)
       -                current(0);
       +    if(downloaded && f->rasp)
       +        outTs(Hclose, f->tag);
       +    delfile(f);
       +    if(f == curfile)
       +        current(0);
        }
        
        void
        update(void)
        {
       -        int i, anymod;
       -        File *f;
       -
       -        settempfile();
       -        for(anymod = i=0; i<tempfile.nused; i++){
       -                f = tempfile.filepptr[i];
       -                if(f==cmd)        /* cmd gets done in main() */
       -                        continue;
       -                if(f->deleted) {
       -                        delete(f);
       -                        continue;
       -                }
       -                if(f->mod==modnum && Fupdate(f, FALSE, downloaded))
       -                        anymod++;
       -                if(f->rasp)
       -                        telldot(f);
       -        }
       -        if(anymod)
       -                modnum++;
       +    int i, anymod;
       +    File *f;
       +
       +    settempfile();
       +    for(anymod = i=0; i<tempfile.nused; i++){
       +        f = tempfile.filepptr[i];
       +        if(f==cmd)  /* cmd gets done in main() */
       +            continue;
       +        if(f->deleted) {
       +            delete(f);
       +            continue;
       +        }
       +        if(f->mod==modnum && Fupdate(f, FALSE, downloaded))
       +            anymod++;
       +        if(f->rasp)
       +            telldot(f);
       +    }
       +    if(anymod)
       +        modnum++;
        }
        
        File *
        current(File *f)
        {
       -        return curfile = f;
       +    return curfile = f;
        }
        
        void
        edit(File *f, int cmd)
        {
       -        int empty = TRUE;
       -        Posn p;
       -        int nulls;
       -
       -        if(cmd == 'r')
       -                Fdelete(f, addr.r.p1, addr.r.p2);
       -        if(cmd=='e' || cmd=='I'){
       -                Fdelete(f, (Posn)0, f->nrunes);
       -                addr.r.p2 = f->nrunes;
       -        }else if(f->nrunes!=0 || (f->name.s[0] && Strcmp(&genstr, &f->name)!=0))
       -                empty = FALSE;
       -        if((io = open(genc, OREAD))<0) {
       -                if (curfile && curfile->state == Unread)
       -                        curfile->state = Clean;
       -                error_s(Eopen, genc);
       -        }
       -        p = readio(f, &nulls, empty);
       -        closeio((cmd=='e' || cmd=='I')? -1 : p);
       -        if(cmd == 'r')
       -                f->ndot.r.p1 = addr.r.p2, f->ndot.r.p2 = addr.r.p2+p;
       -        else
       -                f->ndot.r.p1 = f->ndot.r.p2 = 0;
       -        f->closeok = empty;
       -        if (quitok)
       -                quitok = empty;
       -        else
       -                quitok = FALSE;
       -        state(f, empty && !nulls? Clean : Dirty);
       -        if(cmd == 'e')
       -                filename(f);
       +    int empty = TRUE;
       +    Posn p;
       +    int nulls;
       +
       +    if(cmd == 'r')
       +        Fdelete(f, addr.r.p1, addr.r.p2);
       +    if(cmd=='e' || cmd=='I'){
       +        Fdelete(f, (Posn)0, f->nrunes);
       +        addr.r.p2 = f->nrunes;
       +    }else if(f->nrunes!=0 || (f->name.s[0] && Strcmp(&genstr, &f->name)!=0))
       +        empty = FALSE;
       +    if((io = open(genc, OREAD))<0) {
       +        if (curfile && curfile->state == Unread)
       +            curfile->state = Clean;
       +        error_s(Eopen, genc);
       +    }
       +    p = readio(f, &nulls, empty);
       +    closeio((cmd=='e' || cmd=='I')? -1 : p);
       +    if(cmd == 'r')
       +        f->ndot.r.p1 = addr.r.p2, f->ndot.r.p2 = addr.r.p2+p;
       +    else
       +        f->ndot.r.p1 = f->ndot.r.p2 = 0;
       +    f->closeok = empty;
       +    if (quitok)
       +        quitok = empty;
       +    else
       +        quitok = FALSE;
       +    state(f, empty && !nulls? Clean : Dirty);
       +    if(cmd == 'e')
       +        filename(f);
        }
        
        int
        getname(File *f, String *s, int save)
        {
       -        int c, i;
       -
       -        Strzero(&genstr);
       -        if(genc){
       -                free(genc);
       -                genc = 0;
       -        }
       -        if(s==0 || (c = s->s[0])==0){                /* no name provided */
       -                if(f)
       -                        Strduplstr(&genstr, &f->name);
       -                else
       -                        Straddc(&genstr, '\0');
       -                goto Return;
       -        }
       -        if(c!=' ' && c!='\t')
       -                error(Eblank);
       -        for(i=0; (c=s->s[i])==' ' || c=='\t'; i++)
       -                ;
       -        while(s->s[i] > ' ')
       -                Straddc(&genstr, s->s[i++]);
       -        if(s->s[i])
       -                error(Enewline);
       -        Straddc(&genstr, '\0');
       -        if(f && (save || f->name.s[0]==0)){
       -                Fsetname(f, &genstr);
       -                if(Strcmp(&f->name, &genstr)){
       -                        quitok = f->closeok = FALSE;
       -                        f->qid = 0;
       -                        f->date = 0;
       -                        state(f, Dirty); /* if it's 'e', fix later */
       -                }
       -        }
       +    int c, i;
       +
       +    Strzero(&genstr);
       +    if(genc){
       +        free(genc);
       +        genc = 0;
       +    }
       +    if(s==0 || (c = s->s[0])==0){       /* no name provided */
       +        if(f)
       +            Strduplstr(&genstr, &f->name);
       +        else
       +            Straddc(&genstr, '\0');
       +        goto Return;
       +    }
       +    if(c!=' ' && c!='\t')
       +        error(Eblank);
       +    for(i=0; (c=s->s[i])==' ' || c=='\t'; i++)
       +        ;
       +    while(s->s[i] > ' ')
       +        Straddc(&genstr, s->s[i++]);
       +    if(s->s[i])
       +        error(Enewline);
       +    Straddc(&genstr, '\0');
       +    if(f && (save || f->name.s[0]==0)){
       +        Fsetname(f, &genstr);
       +        if(Strcmp(&f->name, &genstr)){
       +            quitok = f->closeok = FALSE;
       +            f->qid = 0;
       +            f->date = 0;
       +            state(f, Dirty); /* if it's 'e', fix later */
       +        }
       +    }
            Return:
       -        genc = Strtoc(&genstr);
       -        return genstr.n-1;        /* strlen(name) */
       +    genc = Strtoc(&genstr);
       +    return genstr.n-1;  /* strlen(name) */
        }
        
        void
        filename(File *f)
        {
       -        if(genc)
       -                free(genc);
       -        genc = Strtoc(&f->name);
       -        dprint("%c%c%c %s\n", " '"[f->state==Dirty],
       -                "-+"[f->rasp!=0], " ."[f==curfile], genc);
       +    if(genc)
       +        free(genc);
       +    genc = Strtoc(&f->name);
       +    dprint("%c%c%c %s\n", " '"[f->state==Dirty],
       +        "-+"[f->rasp!=0], " ."[f==curfile], genc);
        }
        
        void
        undostep(File *f)
        {
       -        Buffer *t;
       -        int changes;
       -        Mark mark;
       -
       -        t = f->transcript;
       -        changes = Fupdate(f, TRUE, TRUE);
       -        Bread(t, (Rune*)&mark, (sizeof mark)/RUNESIZE, f->markp);
       -        Bdelete(t, f->markp, t->nrunes);
       -        f->markp = mark.p;
       -        f->dot.r = mark.dot;
       -        f->ndot.r = mark.dot;
       -        f->mark = mark.mark;
       -        f->mod = mark.m;
       -        f->closeok = mark.s1!=Dirty;
       -        if(mark.s1==Dirty)
       -                quitok = FALSE;
       -        if(f->state==Clean && mark.s1==Clean && changes)
       -                state(f, Dirty);
       -        else
       -                state(f, mark.s1);
       +    Buffer *t;
       +    int changes;
       +    Mark mark;
       +
       +    t = f->transcript;
       +    changes = Fupdate(f, TRUE, TRUE);
       +    Bread(t, (Rune*)&mark, (sizeof mark)/RUNESIZE, f->markp);
       +    Bdelete(t, f->markp, t->nrunes);
       +    f->markp = mark.p;
       +    f->dot.r = mark.dot;
       +    f->ndot.r = mark.dot;
       +    f->mark = mark.mark;
       +    f->mod = mark.m;
       +    f->closeok = mark.s1!=Dirty;
       +    if(mark.s1==Dirty)
       +        quitok = FALSE;
       +    if(f->state==Clean && mark.s1==Clean && changes)
       +        state(f, Dirty);
       +    else
       +        state(f, mark.s1);
        }
        
        int
        undo(void)
        {
       -        File *f;
       -        int i;
       -        Mod max;
       -        if((max = curfile->mod)==0)
       -                return 0;
       -        settempfile();
       -        for(i = 0; i<tempfile.nused; i++){
       -                f = tempfile.filepptr[i];
       -                if(f!=cmd && f->mod==max)
       -                        undostep(f);
       -        }
       -        return 1;
       +    File *f;
       +    int i;
       +    Mod max;
       +    if((max = curfile->mod)==0)
       +        return 0;
       +    settempfile();
       +    for(i = 0; i<tempfile.nused; i++){
       +        f = tempfile.filepptr[i];
       +        if(f!=cmd && f->mod==max)
       +            undostep(f);
       +    }
       +    return 1;
        }
        
        int
        readcmd(String *s)
        {
       -        int retcode;
       -
       -        if(flist == 0)
       -                (flist = Fopen())->state = Clean;
       -        addr.r.p1 = 0, addr.r.p2 = flist->nrunes;
       -        retcode = plan9(flist, '<', s, FALSE);
       -        Fupdate(flist, FALSE, FALSE);
       -        flist->mod = 0;
       -        if (flist->nrunes > BLOCKSIZE)
       -                error(Etoolong);
       -        Strzero(&genstr);
       -        Strinsure(&genstr, flist->nrunes);
       -        Fchars(flist, genbuf, (Posn)0, flist->nrunes);
       -        memmove(genstr.s, genbuf, flist->nrunes*RUNESIZE);
       -        genstr.n = flist->nrunes;
       -        Straddc(&genstr, '\0');
       -        return retcode;
       +    int retcode;
       +
       +    if(flist == 0)
       +        (flist = Fopen())->state = Clean;
       +    addr.r.p1 = 0, addr.r.p2 = flist->nrunes;
       +    retcode = plan9(flist, '<', s, FALSE);
       +    Fupdate(flist, FALSE, FALSE);
       +    flist->mod = 0;
       +    if (flist->nrunes > BLOCKSIZE)
       +        error(Etoolong);
       +    Strzero(&genstr);
       +    Strinsure(&genstr, flist->nrunes);
       +    Fchars(flist, genbuf, (Posn)0, flist->nrunes);
       +    memmove(genstr.s, genbuf, flist->nrunes*RUNESIZE);
       +    genstr.n = flist->nrunes;
       +    Straddc(&genstr, '\0');
       +    return retcode;
        }
        
        void
        cd(String *str)
        {
       -        int i;
       -        File *f;
       -        String *t;
       -
       -        t = tmpcstr("/bin/pwd");
       -        Straddc(t, '\0');
       -        if (flist) {
       -                Fclose(flist);
       -                flist = 0;
       -        }
       -        if (readcmd(t) != 0) {
       -                Strduplstr(&genstr, tmprstr(baddir, sizeof(baddir)/sizeof(Rune)));
       -                Straddc(&genstr, '\0');
       -        }
       -        freetmpstr(t);
       -        Strduplstr(&wd, &genstr);
       -        if(wd.s[0] == 0){
       -                wd.n = 0;
       -                warn(Wpwd);
       -        }else if(wd.s[wd.n-2] == '\n'){
       -                --wd.n;
       -                wd.s[wd.n-1]='/';
       -        }
       -        if(chdir(getname((File *)0, str, FALSE)? genc : home))
       -                syserror("chdir");
       -        settempfile();
       -        for(i=0; i<tempfile.nused; i++){
       -                f = tempfile.filepptr[i];
       -                if(f!=cmd && f->name.s[0]!='/' && f->name.s[0]!=0){
       -                        Strinsert(&f->name, &wd, (Posn)0);
       -                        sortname(f);
       -                }
       -        }
       +    int i;
       +    File *f;
       +    String *t;
       +
       +    t = tmpcstr("/bin/pwd");
       +    Straddc(t, '\0');
       +    if (flist) {
       +        Fclose(flist);
       +        flist = 0;
       +    }
       +    if (readcmd(t) != 0) {
       +        Strduplstr(&genstr, tmprstr(baddir, sizeof(baddir)/sizeof(Rune)));
       +        Straddc(&genstr, '\0');
       +    }
       +    freetmpstr(t);
       +    Strduplstr(&wd, &genstr);
       +    if(wd.s[0] == 0){
       +        wd.n = 0;
       +        warn(Wpwd);
       +    }else if(wd.s[wd.n-2] == '\n'){
       +        --wd.n;
       +        wd.s[wd.n-1]='/';
       +    }
       +    if(chdir(getname((File *)0, str, FALSE)? genc : home))
       +        syserror("chdir");
       +    settempfile();
       +    for(i=0; i<tempfile.nused; i++){
       +        f = tempfile.filepptr[i];
       +        if(f!=cmd && f->name.s[0]!='/' && f->name.s[0]!=0){
       +            Strinsert(&f->name, &wd, (Posn)0);
       +            sortname(f);
       +        }
       +    }
        }
        
        int
        loadflist(String *s)
        {
       -        int c, i;
       -
       -        c = s->s[0];
       -        for(i = 0; s->s[i]==' ' || s->s[i]=='\t'; i++)
       -                ;
       -        if((c==' ' || c=='\t') && s->s[i]!='\n'){
       -                if(s->s[i]=='<'){
       -                        Strdelete(s, 0L, (long)i+1);
       -                        readcmd(s);
       -                }else{
       -                        Strzero(&genstr);
       -                        while((c = s->s[i++]) && c!='\n')
       -                                Straddc(&genstr, c);
       -                        Straddc(&genstr, '\0');
       -                }
       -        }else{
       -                if(c != '\n')
       -                        error(Eblank);
       -                Strdupl(&genstr, empty);
       -        }
       -        if(genc)
       -                free(genc);
       -        genc = Strtoc(&genstr);
       -        return genstr.s[0];
       +    int c, i;
       +
       +    c = s->s[0];
       +    for(i = 0; s->s[i]==' ' || s->s[i]=='\t'; i++)
       +        ;
       +    if((c==' ' || c=='\t') && s->s[i]!='\n'){
       +        if(s->s[i]=='<'){
       +            Strdelete(s, 0L, (long)i+1);
       +            readcmd(s);
       +        }else{
       +            Strzero(&genstr);
       +            while((c = s->s[i++]) && c!='\n')
       +                Straddc(&genstr, c);
       +            Straddc(&genstr, '\0');
       +        }
       +    }else{
       +        if(c != '\n')
       +            error(Eblank);
       +        Strdupl(&genstr, empty);
       +    }
       +    if(genc)
       +        free(genc);
       +    genc = Strtoc(&genstr);
       +    return genstr.s[0];
        }
        
        File *
        readflist(int readall, int delete)
        {
       -        Posn i;
       -        int c;
       -        File *f;
       -        String *t;
       -
       -        for(i=0,f=0; f==0 || readall || delete; i++){        /* ++ skips blank */
       -                Strdelete(&genstr, (Posn)0, i);
       -                for(i=0; (c = genstr.s[i])==' ' || c=='\t' || c=='\n'; i++)
       -                        ;
       -                if(i >= genstr.n)
       -                        break;
       -                Strdelete(&genstr, (Posn)0, i);
       -                for(i=0; (c=genstr.s[i]) && c!=' ' && c!='\t' && c!='\n'; i++)
       -                        ;
       -
       -                if(i == 0)
       -                        break;
       -                genstr.s[i] = 0;
       -                t = tmprstr(genstr.s, i+1);
       -                f = lookfile(t, 0);
       -                if(delete){
       -                        if(f == 0)
       -                                warn_S(Wfile, t);
       -                        else
       -                                trytoclose(f);
       -                }else if(f==0 && readall)
       -                        Fsetname(f = newfile(), t);
       -        }
       -        return f;
       +    Posn i;
       +    int c;
       +    File *f;
       +    String *t;
       +
       +    for(i=0,f=0; f==0 || readall || delete; i++){   /* ++ skips blank */
       +        Strdelete(&genstr, (Posn)0, i);
       +        for(i=0; (c = genstr.s[i])==' ' || c=='\t' || c=='\n'; i++)
       +            ;
       +        if(i >= genstr.n)
       +            break;
       +        Strdelete(&genstr, (Posn)0, i);
       +        for(i=0; (c=genstr.s[i]) && c!=' ' && c!='\t' && c!='\n'; i++)
       +            ;
       +
       +        if(i == 0)
       +            break;
       +        genstr.s[i] = 0;
       +        t = tmprstr(genstr.s, i+1);
       +        f = lookfile(t, 0);
       +        if(delete){
       +            if(f == 0)
       +                warn_S(Wfile, t);
       +            else
       +                trytoclose(f);
       +        }else if(f==0 && readall)
       +            Fsetname(f = newfile(), t);
       +    }
       +    return f;
        }
        
        File *
        tofile(String *s)
        {
       -        File *f = NULL;
       +    File *f = NULL;
        
       -        if(s->s[0] != ' ')
       -                error(Eblank);
       +    if(s->s[0] != ' ')
       +        error(Eblank);
        
            if (loadflist(s) == 0)
                f = lookfile(&genstr, 0);
       @@ -601,102 +601,102 @@ tofile(String *s)
        File *
        getfile(String *s)
        {
       -        File *f;
       +    File *f;
        
       -        if(loadflist(s) == 0)
       -                Fsetname(f = newfile(), &genstr);
       -        else if((f=readflist(TRUE, FALSE)) == 0)
       -                error(Eblank);
       -        return current(f);
       +    if(loadflist(s) == 0)
       +        Fsetname(f = newfile(), &genstr);
       +    else if((f=readflist(TRUE, FALSE)) == 0)
       +        error(Eblank);
       +    return current(f);
        }
        
        void
        closefiles(File *f, String *s)
        {
       -        if(s->s[0] == 0){
       -                if(f == 0)
       -                        error(Enofile);
       -                trytoclose(f);
       -                return;
       -        }
       -        if(s->s[0] != ' ')
       -                error(Eblank);
       -        if(loadflist(s) == 0)
       -                error(Enewline);
       -        readflist(FALSE, TRUE);
       +    if(s->s[0] == 0){
       +        if(f == 0)
       +            error(Enofile);
       +        trytoclose(f);
       +        return;
       +    }
       +    if(s->s[0] != ' ')
       +        error(Eblank);
       +    if(loadflist(s) == 0)
       +        error(Enewline);
       +    readflist(FALSE, TRUE);
        }
        
        void
        copy(File *f, Address addr2)
        {
       -        Posn p;
       -        int ni;
       -        for(p=addr.r.p1; p<addr.r.p2; p+=ni){
       -                ni = addr.r.p2-p;
       -                if(ni > BLOCKSIZE)
       -                        ni = BLOCKSIZE;
       -                Fchars(f, genbuf, p, p+ni);
       -                Finsert(addr2.f, tmprstr(genbuf, ni), addr2.r.p2);
       -        }
       -        addr2.f->ndot.r.p2 = addr2.r.p2+(f->dot.r.p2-f->dot.r.p1);
       -        addr2.f->ndot.r.p1 = addr2.r.p2;
       +    Posn p;
       +    int ni;
       +    for(p=addr.r.p1; p<addr.r.p2; p+=ni){
       +        ni = addr.r.p2-p;
       +        if(ni > BLOCKSIZE)
       +            ni = BLOCKSIZE;
       +        Fchars(f, genbuf, p, p+ni);
       +        Finsert(addr2.f, tmprstr(genbuf, ni), addr2.r.p2);
       +    }
       +    addr2.f->ndot.r.p2 = addr2.r.p2+(f->dot.r.p2-f->dot.r.p1);
       +    addr2.f->ndot.r.p1 = addr2.r.p2;
        }
        
        void
        move(File *f, Address addr2)
        {
       -        if(addr.r.p2 <= addr2.r.p2){
       -                Fdelete(f, addr.r.p1, addr.r.p2);
       -                copy(f, addr2);
       -        }else if(addr.r.p1 >= addr2.r.p2){
       -                copy(f, addr2);
       -                Fdelete(f, addr.r.p1, addr.r.p2);
       -        }else
       -                error(Eoverlap);
       +    if(addr.r.p2 <= addr2.r.p2){
       +        Fdelete(f, addr.r.p1, addr.r.p2);
       +        copy(f, addr2);
       +    }else if(addr.r.p1 >= addr2.r.p2){
       +        copy(f, addr2);
       +        Fdelete(f, addr.r.p1, addr.r.p2);
       +    }else
       +        error(Eoverlap);
        }
        
        Posn
        nlcount(File *f, Posn p0, Posn p1)
        {
       -        Posn nl = 0;
       +    Posn nl = 0;
        
       -        Fgetcset(f, p0);
       -        while(p0++<p1)
       -                if(Fgetc(f)=='\n')
       -                        nl++;
       -        return nl;
       +    Fgetcset(f, p0);
       +    while(p0++<p1)
       +        if(Fgetc(f)=='\n')
       +            nl++;
       +    return nl;
        }
        
        void
        printposn(File *f, int charsonly)
        {
       -        Posn l1, l2;
       -
       -        if(!charsonly){
       -                l1 = 1+nlcount(f, (Posn)0, addr.r.p1);
       -                l2 = l1+nlcount(f, addr.r.p1, addr.r.p2);
       -                /* check if addr ends with '\n' */
       -                if(addr.r.p2>0 && addr.r.p2>addr.r.p1 && (Fgetcset(f, addr.r.p2-1),Fgetc(f)=='\n'))
       -                        --l2;
       -                dprint("%lu", l1);
       -                if(l2 != l1)
       -                        dprint(",%lu", l2);
       -                dprint("; ");
       -        }
       -        dprint("#%lu", addr.r.p1);
       -        if(addr.r.p2 != addr.r.p1)
       -                dprint(",#%lu", addr.r.p2);
       -        dprint("\n");
       +    Posn l1, l2;
       +
       +    if(!charsonly){
       +        l1 = 1+nlcount(f, (Posn)0, addr.r.p1);
       +        l2 = l1+nlcount(f, addr.r.p1, addr.r.p2);
       +        /* check if addr ends with '\n' */
       +        if(addr.r.p2>0 && addr.r.p2>addr.r.p1 && (Fgetcset(f, addr.r.p2-1),Fgetc(f)=='\n'))
       +            --l2;
       +        dprint("%lu", l1);
       +        if(l2 != l1)
       +            dprint(",%lu", l2);
       +        dprint("; ");
       +    }
       +    dprint("#%lu", addr.r.p1);
       +    if(addr.r.p2 != addr.r.p1)
       +        dprint(",#%lu", addr.r.p2);
       +    dprint("\n");
        }
        
        void
        settempfile(void)
        {
       -        if(tempfile.nalloc < file.nused){
       -                free(tempfile.listptr);
       -                tempfile.listptr = emalloc(sizeof(*tempfile.filepptr)*file.nused);
       -                tempfile.nalloc = file.nused;
       -        }
       -        tempfile.nused = file.nused;
       -        memmove(&tempfile.filepptr[0], &file.filepptr[0], file.nused*sizeof(File*));
       +    if(tempfile.nalloc < file.nused){
       +        free(tempfile.listptr);
       +        tempfile.listptr = emalloc(sizeof(*tempfile.filepptr)*file.nused);
       +        tempfile.nalloc = file.nused;
       +    }
       +    tempfile.nused = file.nused;
       +    memmove(&tempfile.filepptr[0], &file.filepptr[0], file.nused*sizeof(File*));
        }
 (DIR) diff --git a/sam/sam.h b/sam/sam.h
       @@ -8,80 +8,80 @@
         * BLOCKSIZE is relatively small to keep memory consumption down.
         */
        
       -#define        BLOCKSIZE        2048
       -#define        RUNESIZE        sizeof(Rune)
       -#define        NDISC                5
       -#define        NBUFFILES        3+2*NDISC        /* plan 9+undo+snarf+NDISC*(transcript+buf) */
       -#define NSUBEXP        10
       +#define BLOCKSIZE   2048
       +#define RUNESIZE    sizeof(Rune)
       +#define NDISC       5
       +#define NBUFFILES   3+2*NDISC   /* plan 9+undo+snarf+NDISC*(transcript+buf) */
       +#define NSUBEXP 10
        
       -#define        TRUE                1
       -#define        FALSE                0
       +#define TRUE        1
       +#define FALSE       0
        
       -#define        INFINITY        0x7FFFFFFFL
       -#define        INCR                25
       -#define        STRSIZE                (2*BLOCKSIZE)
       +#define INFINITY    0x7FFFFFFFL
       +#define INCR        25
       +#define STRSIZE     (2*BLOCKSIZE)
        
       -typedef long                Posn;                /* file position or address */
       -typedef        ushort                Mod;                /* modification number */
       +typedef long        Posn;       /* file position or address */
       +typedef ushort      Mod;        /* modification number */
        
       -typedef struct Address        Address;
       -typedef struct Block        Block;
       -typedef struct Buffer        Buffer;
       -typedef struct Disc        Disc;
       -typedef struct Discdesc        Discdesc;
       -typedef struct File        File;
       -typedef struct List        List;
       -typedef struct Mark        Mark;
       -typedef struct Range        Range;
       -typedef struct Rangeset        Rangeset;
       -typedef struct String        String;
       +typedef struct Address  Address;
       +typedef struct Block    Block;
       +typedef struct Buffer   Buffer;
       +typedef struct Disc Disc;
       +typedef struct Discdesc Discdesc;
       +typedef struct File File;
       +typedef struct List List;
       +typedef struct Mark Mark;
       +typedef struct Range    Range;
       +typedef struct Rangeset Rangeset;
       +typedef struct String   String;
        
        enum State
        {
       -        Clean =                ' ',
       -        Dirty =                '\'',
       -        Unread =        '-',
       -        Readerr =        '~'
       +    Clean =     ' ',
       +    Dirty =     '\'',
       +    Unread =    '-',
       +    Readerr =   '~'
        };
        
        struct Range
        {
       -        Posn        p1, p2;
       +    Posn    p1, p2;
        };
        
        struct Rangeset
        {
       -        Range        p[NSUBEXP];
       +    Range   p[NSUBEXP];
        };
        
        struct Address
        {
       -        Range        r;
       -        File        *f;
       +    Range   r;
       +    File    *f;
        };
        
       -struct List        /* code depends on a long being able to hold a pointer */
       +struct List /* code depends on a long being able to hold a pointer */
        {
       -        int        nalloc;
       -        int        nused;
       -        union{
       -                void        *listp;
       -                Block        *blkp;
       -                long        *longp;
       -                uchar*        *ucharp;
       -                String*        *stringp;
       -                File*        *filep;
       -                long        listv;
       -        }g;
       +    int nalloc;
       +    int nused;
       +    union{
       +        void    *listp;
       +        Block   *blkp;
       +        long    *longp;
       +        uchar*  *ucharp;
       +        String* *stringp;
       +        File*   *filep;
       +        long    listv;
       +    }g;
        };
        
       -#define        listptr                g.listp
       -#define        blkptr                g.blkp
       -#define        longptr                g.longp
       -#define        ucharpptr        g.ucharp
       -#define        stringpptr        g.stringp
       -#define        filepptr        g.filep
       -#define        listval                g.listv
       +#define listptr     g.listp
       +#define blkptr      g.blkp
       +#define longptr     g.longp
       +#define ucharpptr   g.ucharp
       +#define stringpptr  g.stringp
       +#define filepptr    g.filep
       +#define listval     g.listv
        
        /*
         * Block must fit in a long because the list routines manage arrays of
       @@ -92,8 +92,8 @@ struct List        /* code depends on a long being able to hold a pointer */
         * bigger is the easiest way.
        *
        * The necessary conditions are even stronger:
       -*           sizeof(struct Block)==sizeof(long)
       -*        && the first 32 bits must hold bnum and nrunes.
       +*      sizeof(struct Block)==sizeof(long)
       +*   && the first 32 bits must hold bnum and nrunes.
        * When sizeof(ushort)+sizeof(short) < sizeof(long),
        * add padding at the beginning on a little endian and at
        * the end on a big endian, as shown below for the DEC Alpha.
       @@ -101,87 +101,87 @@ struct List        /* code depends on a long being able to hold a pointer */
        struct Block
        {
        #if USE64BITS == 1
       -        char        pad[sizeof(long)-sizeof(ushort)-sizeof(short)];
       +    char    pad[sizeof(long)-sizeof(ushort)-sizeof(short)];
        #endif
       -        ushort        bnum;                /* absolute number on disk */
       -        short        nrunes;                /* runes stored in this block */
       +    ushort  bnum;       /* absolute number on disk */
       +    short   nrunes;     /* runes stored in this block */
        #if USE64BITS == 2
       -        char        pad[sizeof(long)-sizeof(ushort)-sizeof(short)];
       +    char    pad[sizeof(long)-sizeof(ushort)-sizeof(short)];
        #endif
        };
        
        struct Discdesc
        {
       -        int        fd;                /* plan 9 file descriptor of temp file */
       -        ulong        nbk;                /* high water mark */
       -        List        free;                /* array of free block indices */
       +    int fd;     /* plan 9 file descriptor of temp file */
       +    ulong   nbk;        /* high water mark */
       +    List    free;       /* array of free block indices */
        };
        
        struct Disc
        {
       -        Discdesc *desc;                /* descriptor of temp file */
       -        Posn        nrunes;                /* runes on disc file */
       -        List        block;                /* list of used block indices */
       +    Discdesc *desc;     /* descriptor of temp file */
       +    Posn    nrunes;     /* runes on disc file */
       +    List    block;      /* list of used block indices */
        };
        
        struct String
        {
       -        short        n;
       -        short        size;
       -        Rune        *s;
       +    short   n;
       +    short   size;
       +    Rune    *s;
        };
        
        struct Buffer
        {
       -        Disc        *disc;                /* disc storage */
       -        Posn        nrunes;                /* total length of buffer */
       -        String        cache;                /* in-core storage for efficiency */
       -        Posn        c1, c2;                /* cache start and end positions in disc */
       -                                /* note: if dirty, cache is really c1, c1+cache.n */
       -        int        dirty;                /* cache dirty */
       +    Disc    *disc;      /* disc storage */
       +    Posn    nrunes;     /* total length of buffer */
       +    String  cache;      /* in-core storage for efficiency */
       +    Posn    c1, c2;     /* cache start and end positions in disc */
       +                /* note: if dirty, cache is really c1, c1+cache.n */
       +    int dirty;      /* cache dirty */
        };
        
       -#define        NGETC        128
       +#define NGETC   128
        
        struct File
        {
       -        Buffer        *buf;                /* cached disc storage */
       -        Buffer        *transcript;        /* what's been done */
       -        Posn        markp;                /* file pointer to start of latest change */
       -        Mod        mod;                /* modification stamp */
       -        Posn        nrunes;                /* total length of file */
       -        Posn        hiposn;                /* highest address touched this Mod */
       -        Address        dot;                /* current position */
       -        Address        ndot;                /* new current position after update */
       -        Range        tdot;                /* what terminal thinks is current range */
       -        Range        mark;                /* tagged spot in text (don't confuse with Mark) */
       -        List        *rasp;                /* map of what terminal's got */
       -        String        name;                /* file name */
       -        short        tag;                /* for communicating with terminal */
       -        char        state;                /* Clean, Dirty, Unread, or Readerr*/
       -        char        closeok;        /* ok to close file? */
       -        char        deleted;        /* delete at completion of command */
       -        char        marked;                /* file has been Fmarked at least once; once
       -                                 * set, this will never go off as undo doesn't
       -                                 * revert to the dawn of time */
       -        long        dev;                /* file system from which it was read */
       -        long        qid;                /* file from which it was read */
       -        long        date;                /* time stamp of plan9 file */
       -        Posn        cp1, cp2;        /* Write-behind cache positions and */
       -        String        cache;                /* string */
       -        Rune        getcbuf[NGETC];
       -        int        ngetc;
       -        int        getci;
       -        Posn        getcp;
       +    Buffer  *buf;       /* cached disc storage */
       +    Buffer  *transcript;    /* what's been done */
       +    Posn    markp;      /* file pointer to start of latest change */
       +    Mod mod;        /* modification stamp */
       +    Posn    nrunes;     /* total length of file */
       +    Posn    hiposn;     /* highest address touched this Mod */
       +    Address dot;        /* current position */
       +    Address ndot;       /* new current position after update */
       +    Range   tdot;       /* what terminal thinks is current range */
       +    Range   mark;       /* tagged spot in text (don't confuse with Mark) */
       +    List    *rasp;      /* map of what terminal's got */
       +    String  name;       /* file name */
       +    short   tag;        /* for communicating with terminal */
       +    char    state;      /* Clean, Dirty, Unread, or Readerr*/
       +    char    closeok;    /* ok to close file? */
       +    char    deleted;    /* delete at completion of command */
       +    char    marked;     /* file has been Fmarked at least once; once
       +                 * set, this will never go off as undo doesn't
       +                 * revert to the dawn of time */
       +    long    dev;        /* file system from which it was read */
       +    long    qid;        /* file from which it was read */
       +    long    date;       /* time stamp of plan9 file */
       +    Posn    cp1, cp2;   /* Write-behind cache positions and */
       +    String  cache;      /* string */
       +    Rune    getcbuf[NGETC];
       +    int ngetc;
       +    int getci;
       +    Posn    getcp;
        };
        
        struct Mark
        {
       -        Posn        p;
       -        Range        dot;
       -        Range        mark;
       -        Mod        m;
       -        short        s1;
       +    Posn    p;
       +    Range   dot;
       +    Range   mark;
       +    Mod m;
       +    short   s1;
        };
        
        /*
       @@ -190,210 +190,210 @@ struct Mark
         */
        union Hdr
        {
       -        struct _csl
       -        {
       -                short        c;
       -                short        s;
       -                long        l;
       -        }csl;
       -        struct _cs
       -        {
       -                short        c;
       -                short        s;
       -        }cs;
       -        struct _cll
       -        {
       -                short        c;
       -                long        l;
       -                long        l1;
       -        }cll;
       -        Mark        mark;
       +    struct _csl
       +    {
       +        short   c;
       +        short   s;
       +        long    l;
       +    }csl;
       +    struct _cs
       +    {
       +        short   c;
       +        short   s;
       +    }cs;
       +    struct _cll
       +    {
       +        short   c;
       +        long    l;
       +        long    l1;
       +    }cll;
       +    Mark    mark;
        };
        
       -#define        Fgetc(f)  ((--(f)->ngetc<0)? Fgetcload(f, (f)->getcp) : (f)->getcbuf[(f)->getcp++, (f)->getci++])
       -#define        Fbgetc(f) (((f)->getci<=0)? Fbgetcload(f, (f)->getcp) : (f)->getcbuf[--(f)->getcp, --(f)->getci])
       +#define Fgetc(f)  ((--(f)->ngetc<0)? Fgetcload(f, (f)->getcp) : (f)->getcbuf[(f)->getcp++, (f)->getci++])
       +#define Fbgetc(f) (((f)->getci<=0)? Fbgetcload(f, (f)->getcp) : (f)->getcbuf[--(f)->getcp, --(f)->getci])
        
       -int        alnum(int);
       -void        Bclean(Buffer*);
       -void        Bterm(Buffer*);
       -void        Bdelete(Buffer*, Posn, Posn);
       -void        Bflush(Buffer*);
       -void        Binsert(Buffer*, String*, Posn);
       -Buffer        *Bopen(Discdesc*);
       -int        Bread(Buffer*, Rune*, int, Posn);
       -void        Dclose(Disc*);
       -void        Ddelete(Disc*, Posn, Posn);
       -void        Dinsert(Disc*, Rune*, int, Posn);
       -Disc        *Dopen(Discdesc*);
       -int        Dread(Disc*, Rune*, int, Posn);
       -void        Dreplace(Disc*, Posn, Posn, Rune*, int);
       -int        Fbgetcload(File*, Posn);
       -int        Fbgetcset(File*, Posn);
       -long        Fchars(File*, Rune*, Posn, Posn);
       -void        Fclose(File*);
       -void        Fdelete(File*, Posn, Posn);
       -int        Fgetcload(File*, Posn);
       -int        Fgetcset(File*, Posn);
       -void        Finsert(File*, String*, Posn);
       -File        *Fopen(void);
       -void        Fsetname(File*, String*);
       -void        Fstart(void);
       -int        Fupdate(File*, int, int);
       -int        Read(int, void*, int);
       -void        Seek(int, long, int);
       -int        plan9(File*, int, String*, int);
       -int        Write(int, void*, int);
       -int        bexecute(File*, Posn);
       -void        cd(String*);
       -void        closefiles(File*, String*);
       -void        closeio(Posn);
       -void        cmdloop(void);
       -void        cmdupdate(void);
       -void        compile(String*);
       -void        copy(File*, Address);
       -File        *current(File*);
       -void        delete(File*);
       -void        delfile(File*);
       -void        dellist(List*, int);
       -void        doubleclick(File*, Posn);
       -void        dprint(char*, ...);
       -void        edit(File*, int);
       -void        *emalloc(ulong);
       -void        *erealloc(void*, ulong);
       -void        error(Err);
       -void        error_c(Err, int);
       -void        error_s(Err, char*);
       -int        execute(File*, Posn, Posn);
       -int        filematch(File*, String*);
       -void        filename(File*);
       -File        *getfile(String*);
       -int        getname(File*, String*, int);
       -long        getnum(void);
       -void        hiccough(char*);
       -void        inslist(List*, int, long);
       -Address        lineaddr(Posn, Address, int);
       -void        listfree(List*);
       -void        load(File*);
       -File        *lookfile(String*, int);
       -void        lookorigin(File*, Posn, Posn, long);
       -int        lookup(int);
       -void        move(File*, Address);
       -void        moveto(File*, Range);
       -File        *newfile(void);
       -void        nextmatch(File*, String*, Posn, int);
       -int        newtmp(void);
       -void        notifyf(void*, char*);
       -void        panic(char*);
       -void        printposn(File*, int);
       -void        print_ss(char*, String*, String*);
       -void        print_s(char*, String*);
       -int        rcv(void);
       -Range        rdata(List*, Posn, Posn);
       -Posn        readio(File*, int*, int);
       -void        rescue(void);
       -void        resetcmd(void);
       -void        resetsys(void);
       -void        resetxec(void);
       -void        rgrow(List*, Posn, Posn);
       -void        samerr(char*);
       -void        settempfile(void);
       -int        skipbl(void);
       -void        snarf(File*, Posn, Posn, Buffer*, int);
       -void        sortname(File*);
       -void        startup(char*, int, char**, char**);
       -void        state(File*, int);
       -int        statfd(int, ulong*, ulong*, long*, long*, long*);
       -int        statfile(char*, ulong*, ulong*, long*, long*, long*);
       -void        Straddc(String*, int);
       -void        Strclose(String*);
       -int        Strcmp(String*, String*);
       -void        Strdelete(String*, Posn, Posn);
       -void        Strdupl(String*, Rune*);
       -void        Strduplstr(String*, String*);
       -void        Strinit(String*);
       -void        Strinit0(String*);
       -void        Strinsert(String*, String*, Posn);
       -void        Strinsure(String*, ulong);
       -void        Strzero(String*);
       -int        Strlen(Rune*);
       -char        *Strtoc(String*);
       -void        syserror(char*);
       -void        telldot(File*);
       -void        tellpat(void);
       -String        *tmpcstr(char*);
       -String        *tmprstr(Rune*, int);
       -void        freetmpstr(String*);
       -void        termcommand(void);
       -void        termwrite(char*);
       -File        *tofile(String*);
       -void        toterminal(File*, int);
       -void        trytoclose(File*);
       -void        trytoquit(void);
       -int        undo(void);
       -void        update(void);
       -int        waitfor(int);
       -void        warn(Warn);
       -void        warn_s(Warn, char*);
       -void        warn_SS(Warn, String*, String*);
       -void        warn_S(Warn, String*);
       -int        whichmenu(File*);
       -void        writef(File*);
       -Posn        writeio(File*);
       +int alnum(int);
       +void    Bclean(Buffer*);
       +void    Bterm(Buffer*);
       +void    Bdelete(Buffer*, Posn, Posn);
       +void    Bflush(Buffer*);
       +void    Binsert(Buffer*, String*, Posn);
       +Buffer  *Bopen(Discdesc*);
       +int Bread(Buffer*, Rune*, int, Posn);
       +void    Dclose(Disc*);
       +void    Ddelete(Disc*, Posn, Posn);
       +void    Dinsert(Disc*, Rune*, int, Posn);
       +Disc    *Dopen(Discdesc*);
       +int Dread(Disc*, Rune*, int, Posn);
       +void    Dreplace(Disc*, Posn, Posn, Rune*, int);
       +int Fbgetcload(File*, Posn);
       +int Fbgetcset(File*, Posn);
       +long    Fchars(File*, Rune*, Posn, Posn);
       +void    Fclose(File*);
       +void    Fdelete(File*, Posn, Posn);
       +int Fgetcload(File*, Posn);
       +int Fgetcset(File*, Posn);
       +void    Finsert(File*, String*, Posn);
       +File    *Fopen(void);
       +void    Fsetname(File*, String*);
       +void    Fstart(void);
       +int Fupdate(File*, int, int);
       +int Read(int, void*, int);
       +void    Seek(int, long, int);
       +int plan9(File*, int, String*, int);
       +int Write(int, void*, int);
       +int bexecute(File*, Posn);
       +void    cd(String*);
       +void    closefiles(File*, String*);
       +void    closeio(Posn);
       +void    cmdloop(void);
       +void    cmdupdate(void);
       +void    compile(String*);
       +void    copy(File*, Address);
       +File    *current(File*);
       +void    delete(File*);
       +void    delfile(File*);
       +void    dellist(List*, int);
       +void    doubleclick(File*, Posn);
       +void    dprint(char*, ...);
       +void    edit(File*, int);
       +void    *emalloc(ulong);
       +void    *erealloc(void*, ulong);
       +void    error(Err);
       +void    error_c(Err, int);
       +void    error_s(Err, char*);
       +int execute(File*, Posn, Posn);
       +int filematch(File*, String*);
       +void    filename(File*);
       +File    *getfile(String*);
       +int getname(File*, String*, int);
       +long    getnum(void);
       +void    hiccough(char*);
       +void    inslist(List*, int, long);
       +Address lineaddr(Posn, Address, int);
       +void    listfree(List*);
       +void    load(File*);
       +File    *lookfile(String*, int);
       +void    lookorigin(File*, Posn, Posn, long);
       +int lookup(int);
       +void    move(File*, Address);
       +void    moveto(File*, Range);
       +File    *newfile(void);
       +void    nextmatch(File*, String*, Posn, int);
       +int newtmp(void);
       +void    notifyf(void*, char*);
       +void    panic(char*);
       +void    printposn(File*, int);
       +void    print_ss(char*, String*, String*);
       +void    print_s(char*, String*);
       +int rcv(void);
       +Range   rdata(List*, Posn, Posn);
       +Posn    readio(File*, int*, int);
       +void    rescue(void);
       +void    resetcmd(void);
       +void    resetsys(void);
       +void    resetxec(void);
       +void    rgrow(List*, Posn, Posn);
       +void    samerr(char*);
       +void    settempfile(void);
       +int skipbl(void);
       +void    snarf(File*, Posn, Posn, Buffer*, int);
       +void    sortname(File*);
       +void    startup(char*, int, char**, char**);
       +void    state(File*, int);
       +int statfd(int, ulong*, ulong*, long*, long*, long*);
       +int statfile(char*, ulong*, ulong*, long*, long*, long*);
       +void    Straddc(String*, int);
       +void    Strclose(String*);
       +int Strcmp(String*, String*);
       +void    Strdelete(String*, Posn, Posn);
       +void    Strdupl(String*, Rune*);
       +void    Strduplstr(String*, String*);
       +void    Strinit(String*);
       +void    Strinit0(String*);
       +void    Strinsert(String*, String*, Posn);
       +void    Strinsure(String*, ulong);
       +void    Strzero(String*);
       +int Strlen(Rune*);
       +char    *Strtoc(String*);
       +void    syserror(char*);
       +void    telldot(File*);
       +void    tellpat(void);
       +String  *tmpcstr(char*);
       +String  *tmprstr(Rune*, int);
       +void    freetmpstr(String*);
       +void    termcommand(void);
       +void    termwrite(char*);
       +File    *tofile(String*);
       +void    toterminal(File*, int);
       +void    trytoclose(File*);
       +void    trytoquit(void);
       +int undo(void);
       +void    update(void);
       +int waitfor(int);
       +void    warn(Warn);
       +void    warn_s(Warn, char*);
       +void    warn_SS(Warn, String*, String*);
       +void    warn_S(Warn, String*);
       +int whichmenu(File*);
       +void    writef(File*);
       +Posn    writeio(File*);
        Discdesc *Dstart(void);
        
       -extern Rune        samname[];        /* compiler dependent */
       -extern Rune        *left[];
       -extern Rune        *right[];
       +extern Rune samname[];  /* compiler dependent */
       +extern Rune *left[];
       +extern Rune *right[];
        
       -extern char        *rsamname;        /* globals */
       -extern char        *samterm;
       -extern Rune        genbuf[];
       -extern char        *genc;
       -extern int        io;
       -extern int        patset;
       -extern int        quitok;
       -extern Address        addr;
       -extern Buffer        *undobuf;
       -extern Buffer        *snarfbuf;
       -extern Buffer        *plan9buf;
       -extern List        file;
       -extern List        tempfile;
       -extern File        *cmd;
       -extern File        *curfile;
       -extern File        *lastfile;
       -extern Mod        modnum;
       -extern Posn        cmdpt;
       -extern Posn        cmdptadv;
       -extern Rangeset        sel;
       -extern String        cmdstr;
       -extern String        genstr;
       -extern String        lastpat;
       -extern String        lastregexp;
       -extern String        plan9cmd;
       -extern int        downloaded;
       -extern int        eof;
       -extern int        bpipeok;
       -extern int        panicking;
       -extern Rune        empty[];
       -extern int        termlocked;
       -extern int        noflush;
       +extern char *rsamname;  /* globals */
       +extern char *samterm;
       +extern Rune genbuf[];
       +extern char *genc;
       +extern int  io;
       +extern int  patset;
       +extern int  quitok;
       +extern Address  addr;
       +extern Buffer   *undobuf;
       +extern Buffer   *snarfbuf;
       +extern Buffer   *plan9buf;
       +extern List file;
       +extern List tempfile;
       +extern File *cmd;
       +extern File *curfile;
       +extern File *lastfile;
       +extern Mod  modnum;
       +extern Posn cmdpt;
       +extern Posn cmdptadv;
       +extern Rangeset sel;
       +extern String   cmdstr;
       +extern String   genstr;
       +extern String   lastpat;
       +extern String   lastregexp;
       +extern String   plan9cmd;
       +extern int  downloaded;
       +extern int  eof;
       +extern int  bpipeok;
       +extern int  panicking;
       +extern Rune empty[];
       +extern int  termlocked;
       +extern int  noflush;
        
        #include "mesg.h"
        
       -void        outTs(Hmesg, int);
       -void        outT0(Hmesg);
       -void        outTl(Hmesg, long);
       -void        outTslS(Hmesg, int, long, String*);
       -void        outTS(Hmesg, String*);
       -void        outTsS(Hmesg, int, String*);
       -void        outTsllS(Hmesg, int, long, long, String*);
       -void        outTsll(Hmesg, int, long, long);
       -void        outTsl(Hmesg, int, long);
       -void        outTsv(Hmesg, int, long);
       -void        outstart(Hmesg);
       -void        outcopy(int, void*);
       -void        outshort(int);
       -void        outlong(long);
       -void        outvlong(void*);
       -void        outsend(void);
       -void        outflush(void);
       +void    outTs(Hmesg, int);
       +void    outT0(Hmesg);
       +void    outTl(Hmesg, long);
       +void    outTslS(Hmesg, int, long, String*);
       +void    outTS(Hmesg, String*);
       +void    outTsS(Hmesg, int, String*);
       +void    outTsllS(Hmesg, int, long, long, String*);
       +void    outTsll(Hmesg, int, long, long);
       +void    outTsl(Hmesg, int, long);
       +void    outTsv(Hmesg, int, long);
       +void    outstart(Hmesg);
       +void    outcopy(int, void*);
       +void    outshort(int);
       +void    outlong(long);
       +void    outvlong(void*);
       +void    outsend(void);
       +void    outflush(void);
 (DIR) diff --git a/sam/shell.c b/sam/shell.c
       @@ -4,154 +4,154 @@
        
        #include <limits.h>
        
       -extern        jmp_buf        mainloop;
       +extern  jmp_buf mainloop;
        
       -char        errfile[PATH_MAX + 1];
       -String        plan9cmd;        /* null terminated */
       -Buffer        *plan9buf;
       -void        checkerrs(void);
       +char    errfile[PATH_MAX + 1];
       +String  plan9cmd;   /* null terminated */
       +Buffer  *plan9buf;
       +void    checkerrs(void);
        
        int
        plan9(File *f, int type, String *s, int nest)
        {
       -        long l;
       -        int m;
       -        int pid, fd;
       -        int retcode;
       -        int pipe1[2], pipe2[2];
       +    long l;
       +    int m;
       +    int pid, fd;
       +    int retcode;
       +    int pipe1[2], pipe2[2];
        
       -        if(s->s[0]==0 && plan9cmd.s[0]==0)
       -                error(Enocmd);
       -        else if(s->s[0])
       -                Strduplstr(&plan9cmd, s);
       -        if(downloaded)
       -                samerr(errfile);
       -        else
       -                strcpy(errfile, "/dev/tty");
       -        if(type!='!' && pipe(pipe1)==-1)
       -                error(Epipe);
       -        if(type=='|')
       -                snarf(f, addr.r.p1, addr.r.p2, plan9buf, 1);
       -        if(downloaded)
       -                remove(errfile);
       -        if((pid=fork()) == 0){
       -                if(downloaded){        /* also put nasty fd's into errfile */
       -                        fd = create(errfile, 1, 0666L);
       -                        if(fd < 0)
       -                                fd = create("/dev/null", 1, 0666L);
       -                        dup(fd, 2);
       -                        close(fd);
       -                        /* 2 now points at err file */
       -                        if(type == '>')
       -                                dup(2, 1);
       -                        else if(type=='!'){
       -                                dup(2, 1);
       -                                fd = open("/dev/null", 0);
       -                                dup(fd, 0);
       -                                close(fd);
       -                        }
       -                }
       -                if(type != '!') {
       -                        if(type=='<' || type=='|')
       -                                dup(pipe1[1], 1);
       -                        else if(type == '>')
       -                                dup(pipe1[0], 0);
       -                        close(pipe1[0]);
       -                        close(pipe1[1]);
       -                }
       -                if(type == '|'){
       -                        if(pipe(pipe2) == -1)
       -                                exits("pipe");
       -                        if((pid = fork())==0){
       -                                /*
       -                                 * It's ok if we get SIGPIPE here
       -                                 */
       -                                close(pipe2[0]);
       -                                io = pipe2[1];
       -                                if(retcode=!setjmp(mainloop)){        /* assignment = */
       -                                        char *c;
       -                                        for(l = 0; l<plan9buf->nrunes; l+=m){
       -                                                m = plan9buf->nrunes-l;
       -                                                if(m>BLOCKSIZE-1)
       -                                                        m = BLOCKSIZE-1;
       -                                                Bread(plan9buf, genbuf, m, l);
       -                                                genbuf[m] = 0;
       -                                                c = Strtoc(tmprstr(genbuf, m+1));
       -                                                Write(pipe2[1], c, strlen(c));
       -                                                free(c);
       -                                        }
       -                                }
       -                                exits(retcode? "error" : 0);
       -                        }
       -                        if(pid==-1){
       -                                fprintf(stderr, "Can't fork?!\n");
       -                                exits("fork");
       -                        }
       -                        dup(pipe2[0], 0);
       -                        close(pipe2[0]);
       -                        close(pipe2[1]);
       -                }
       -                if(type=='<'){
       -                        close(0);        /* so it won't read from terminal */
       -                        open("/dev/null", 0);
       -                }
       -                execlp(SHPATH, SHPATH, "-c", Strtoc(&plan9cmd), (char *)0);
       -                exits("exec");
       -        }
       -        if(pid == -1)
       -                error(Efork);
       -        if(type=='<' || type=='|'){
       -                int nulls;
       -                if(downloaded && addr.r.p1 != addr.r.p2)
       -                        outTl(Hsnarflen, addr.r.p2-addr.r.p1);
       -                snarf(f, addr.r.p1, addr.r.p2, snarfbuf, 0);
       -                Fdelete(f, addr.r.p1, addr.r.p2);
       -                close(pipe1[1]);
       -                io = pipe1[0];
       -                f->tdot.p1 = -1;
       -                f->ndot.r.p2 = addr.r.p2+readio(f, &nulls, 0);
       -                f->ndot.r.p1 = addr.r.p2;
       -                closeio((Posn)-1);
       -        }else if(type=='>'){
       -                close(pipe1[0]);
       -                io = pipe1[1];
       -                bpipeok = 1;
       -                writeio(f);
       -                bpipeok = 0;
       -                closeio((Posn)-1);
       -        }
       -        retcode = waitfor(pid);
       -        if(type=='|' || type=='<')
       -                if(retcode!=0)
       -                        warn(Wbadstatus);
       -        if(downloaded)
       -                checkerrs();
       -        if(!nest)
       -                dprint("!\n");
       -        return retcode;
       +    if(s->s[0]==0 && plan9cmd.s[0]==0)
       +        error(Enocmd);
       +    else if(s->s[0])
       +        Strduplstr(&plan9cmd, s);
       +    if(downloaded)
       +        samerr(errfile);
       +    else
       +        strcpy(errfile, "/dev/tty");
       +    if(type!='!' && pipe(pipe1)==-1)
       +        error(Epipe);
       +    if(type=='|')
       +        snarf(f, addr.r.p1, addr.r.p2, plan9buf, 1);
       +    if(downloaded)
       +        remove(errfile);
       +    if((pid=fork()) == 0){
       +        if(downloaded){ /* also put nasty fd's into errfile */
       +            fd = create(errfile, 1, 0666L);
       +            if(fd < 0)
       +                fd = create("/dev/null", 1, 0666L);
       +            dup(fd, 2);
       +            close(fd);
       +            /* 2 now points at err file */
       +            if(type == '>')
       +                dup(2, 1);
       +            else if(type=='!'){
       +                dup(2, 1);
       +                fd = open("/dev/null", 0);
       +                dup(fd, 0);
       +                close(fd);
       +            }
       +        }
       +        if(type != '!') {
       +            if(type=='<' || type=='|')
       +                dup(pipe1[1], 1);
       +            else if(type == '>')
       +                dup(pipe1[0], 0);
       +            close(pipe1[0]);
       +            close(pipe1[1]);
       +        }
       +        if(type == '|'){
       +            if(pipe(pipe2) == -1)
       +                exits("pipe");
       +            if((pid = fork())==0){
       +                /*
       +                 * It's ok if we get SIGPIPE here
       +                 */
       +                close(pipe2[0]);
       +                io = pipe2[1];
       +                if(retcode=!setjmp(mainloop)){  /* assignment = */
       +                    char *c;
       +                    for(l = 0; l<plan9buf->nrunes; l+=m){
       +                        m = plan9buf->nrunes-l;
       +                        if(m>BLOCKSIZE-1)
       +                            m = BLOCKSIZE-1;
       +                        Bread(plan9buf, genbuf, m, l);
       +                        genbuf[m] = 0;
       +                        c = Strtoc(tmprstr(genbuf, m+1));
       +                        Write(pipe2[1], c, strlen(c));
       +                        free(c);
       +                    }
       +                }
       +                exits(retcode? "error" : 0);
       +            }
       +            if(pid==-1){
       +                fprintf(stderr, "Can't fork?!\n");
       +                exits("fork");
       +            }
       +            dup(pipe2[0], 0);
       +            close(pipe2[0]);
       +            close(pipe2[1]);
       +        }
       +        if(type=='<'){
       +            close(0);   /* so it won't read from terminal */
       +            open("/dev/null", 0);
       +        }
       +        execlp(SHPATH, SHPATH, "-c", Strtoc(&plan9cmd), (char *)0);
       +        exits("exec");
       +    }
       +    if(pid == -1)
       +        error(Efork);
       +    if(type=='<' || type=='|'){
       +        int nulls;
       +        if(downloaded && addr.r.p1 != addr.r.p2)
       +            outTl(Hsnarflen, addr.r.p2-addr.r.p1);
       +        snarf(f, addr.r.p1, addr.r.p2, snarfbuf, 0);
       +        Fdelete(f, addr.r.p1, addr.r.p2);
       +        close(pipe1[1]);
       +        io = pipe1[0];
       +        f->tdot.p1 = -1;
       +        f->ndot.r.p2 = addr.r.p2+readio(f, &nulls, 0);
       +        f->ndot.r.p1 = addr.r.p2;
       +        closeio((Posn)-1);
       +    }else if(type=='>'){
       +        close(pipe1[0]);
       +        io = pipe1[1];
       +        bpipeok = 1;
       +        writeio(f);
       +        bpipeok = 0;
       +        closeio((Posn)-1);
       +    }
       +    retcode = waitfor(pid);
       +    if(type=='|' || type=='<')
       +        if(retcode!=0)
       +            warn(Wbadstatus);
       +    if(downloaded)
       +        checkerrs();
       +    if(!nest)
       +        dprint("!\n");
       +    return retcode;
        }
        
        void
        checkerrs(void)
        {
       -        char buf[256];
       -        int f, n, nl;
       -        char *p;
       -        long l;
       +    char buf[256];
       +    int f, n, nl;
       +    char *p;
       +    long l;
        
       -        if(statfile(errfile, 0, 0, 0, &l, 0) > 0 && l != 0){
       -                if((f=open((char *)errfile, 0)) != -1){
       -                        if((n=read(f, buf, sizeof buf-1)) > 0){
       -                                for(nl=0,p=buf; nl<3 && p<&buf[n]; p++)
       -                                        if(*p=='\n')
       -                                                nl++;
       -                                *p = 0;
       -                                dprint("%s", buf);
       -                                if(p-buf < l-1)
       -                                        dprint("(sam: more in %s)\n", errfile);
       -                        }
       -                        close(f);
       -                }
       -        }else
       -                remove((char *)errfile);
       +    if(statfile(errfile, 0, 0, 0, &l, 0) > 0 && l != 0){
       +        if((f=open((char *)errfile, 0)) != -1){
       +            if((n=read(f, buf, sizeof buf-1)) > 0){
       +                for(nl=0,p=buf; nl<3 && p<&buf[n]; p++)
       +                    if(*p=='\n')
       +                        nl++;
       +                *p = 0;
       +                dprint("%s", buf);
       +                if(p-buf < l-1)
       +                    dprint("(sam: more in %s)\n", errfile);
       +            }
       +            close(f);
       +        }
       +    }else
       +        remove((char *)errfile);
        }
 (DIR) diff --git a/sam/string.c b/sam/string.c
       @@ -1,138 +1,138 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
        #include "sam.h"
        
       -#define        MINSIZE        16                /* minimum number of chars allocated */
       -#define        MAXSIZE        256                /* maximum number of chars for an empty string */
       +#define MINSIZE 16      /* minimum number of chars allocated */
       +#define MAXSIZE 256     /* maximum number of chars for an empty string */
        
        
        void
        Strinit(String *p)
        {
       -        p->s = emalloc(MINSIZE*RUNESIZE);
       -        p->n = 0;
       -        p->size = MINSIZE;
       +    p->s = emalloc(MINSIZE*RUNESIZE);
       +    p->n = 0;
       +    p->size = MINSIZE;
        }
        
        void
        Strinit0(String *p)
        {
       -        p->s = emalloc(MINSIZE*RUNESIZE);
       -        p->s[0] = 0;
       -        p->n = 1;
       -        p->size = MINSIZE;
       +    p->s = emalloc(MINSIZE*RUNESIZE);
       +    p->s[0] = 0;
       +    p->n = 1;
       +    p->size = MINSIZE;
        }
        
        void
        Strclose(String *p)
        {
       -        free(p->s);
       +    free(p->s);
        }
        
        void
        Strzero(String *p)
        {
       -        if(p->size > MAXSIZE){
       -                p->s = erealloc(p->s, RUNESIZE*MAXSIZE); /* throw away the garbage */
       -                p->size = MAXSIZE;
       -        }
       -        p->n = 0;
       +    if(p->size > MAXSIZE){
       +        p->s = erealloc(p->s, RUNESIZE*MAXSIZE); /* throw away the garbage */
       +        p->size = MAXSIZE;
       +    }
       +    p->n = 0;
        }
        
        int
        Strlen(Rune *r)
        {
       -        Rune *s;
       +    Rune *s;
        
       -        for(s=r; *s; s++)
       -                ;
       -        return s-r;
       +    for(s=r; *s; s++)
       +        ;
       +    return s-r;
        }
        
        void
       -Strdupl(String *p, Rune *s)        /* copies the null */
       +Strdupl(String *p, Rune *s) /* copies the null */
        {
       -        p->n = Strlen(s)+1;
       -        Strinsure(p, p->n);
       -        memmove(p->s, s, p->n*RUNESIZE);
       +    p->n = Strlen(s)+1;
       +    Strinsure(p, p->n);
       +    memmove(p->s, s, p->n*RUNESIZE);
        }
        
        void
       -Strduplstr(String *p, String *q)        /* will copy the null if there's one there */
       +Strduplstr(String *p, String *q)    /* will copy the null if there's one there */
        {
       -        Strinsure(p, q->n);
       -        p->n = q->n;
       -        memmove(p->s, q->s, q->n*RUNESIZE);
       +    Strinsure(p, q->n);
       +    p->n = q->n;
       +    memmove(p->s, q->s, q->n*RUNESIZE);
        }
        
        void
        Straddc(String *p, int c)
        {
       -        Strinsure(p, p->n+1);
       -        p->s[p->n++] = c;
       +    Strinsure(p, p->n+1);
       +    p->s[p->n++] = c;
        }
        
        void
        Strinsure(String *p, ulong n)
        {
       -        if(n > STRSIZE)
       -                error(Etoolong);
       -        if(p->size < n){        /* p needs to grow */
       -                n += 100;
       -                p->s = erealloc(p->s, n*RUNESIZE);
       -                p->size = n;
       -        }
       +    if(n > STRSIZE)
       +        error(Etoolong);
       +    if(p->size < n){    /* p needs to grow */
       +        n += 100;
       +        p->s = erealloc(p->s, n*RUNESIZE);
       +        p->size = n;
       +    }
        }
        
        void
        Strinsert(String *p, String *q, Posn p0)
        {
       -        Strinsure(p, p->n+q->n);
       -        memmove(p->s+p0+q->n, p->s+p0, (p->n-p0)*RUNESIZE);
       -        memmove(p->s+p0, q->s, q->n*RUNESIZE);
       -        p->n += q->n;
       +    Strinsure(p, p->n+q->n);
       +    memmove(p->s+p0+q->n, p->s+p0, (p->n-p0)*RUNESIZE);
       +    memmove(p->s+p0, q->s, q->n*RUNESIZE);
       +    p->n += q->n;
        }
        
        void
        Strdelete(String *p, Posn p1, Posn p2)
        {
       -        memmove(p->s+p1, p->s+p2, (p->n-p2)*RUNESIZE);
       -        p->n -= p2-p1;
       +    memmove(p->s+p1, p->s+p2, (p->n-p2)*RUNESIZE);
       +    p->n -= p2-p1;
        }
        
        int
        Strcmp(String *a, String *b)
        {
       -        int i, c;
       +    int i, c;
        
       -        for(i=0; i<a->n && i<b->n; i++)
       -                if(c = (a->s[i] - b->s[i]))        /* assign = */
       -                        return c;
       -        /* damn NULs confuse everything */
       -        i = a->n - b->n;
       -        if(i == 1){
       -                if(a->s[a->n-1] == 0)
       -                        return 0;
       -        }else if(i == -1){
       -                if(b->s[b->n-1] == 0)
       -                        return 0;
       -        }
       -        return i;
       +    for(i=0; i<a->n && i<b->n; i++)
       +        if(c = (a->s[i] - b->s[i])) /* assign = */
       +            return c;
       +    /* damn NULs confuse everything */
       +    i = a->n - b->n;
       +    if(i == 1){
       +        if(a->s[a->n-1] == 0)
       +            return 0;
       +    }else if(i == -1){
       +        if(b->s[b->n-1] == 0)
       +            return 0;
       +    }
       +    return i;
        }
        
        char*
        Strtoc(String *s)
        {
       -        int i;
       -        char *c, *d;
       -        Rune *r;
       -        c = emalloc(s->n*UTFmax + 1);  /* worst case UTFmax bytes per rune, plus NUL */
       -        d = c;
       -        r = s->s;
       -        for(i=0; i<s->n; i++)
       -                d += runetochar(d, r++);
       -        if(d==c || d[-1]!=0)
       -                *d = 0;
       -        return c;
       +    int i;
       +    char *c, *d;
       +    Rune *r;
       +    c = emalloc(s->n*UTFmax + 1);  /* worst case UTFmax bytes per rune, plus NUL */
       +    d = c;
       +    r = s->s;
       +    for(i=0; i<s->n; i++)
       +        d += runetochar(d, r++);
       +    if(d==c || d[-1]!=0)
       +        *d = 0;
       +    return c;
        
        }
        
       @@ -142,12 +142,12 @@ Strtoc(String *s)
        String*
        tmprstr(Rune *r, int n)
        {
       -        static String p;
       +    static String p;
        
       -        p.s = r;
       -        p.n = n;
       -        p.size = n;
       -        return &p;
       +    p.s = r;
       +    p.n = n;
       +    p.size = n;
       +    return &p;
        }
        
        /*
       @@ -156,24 +156,24 @@ tmprstr(Rune *r, int n)
        String*
        tmpcstr(char *s)
        {
       -        String *p;
       -        Rune *r;
       -        int i, n;
       +    String *p;
       +    Rune *r;
       +    int i, n;
        
       -        n = utflen(s);        /* don't include NUL */
       -        p = emalloc(sizeof(String));
       -        r = emalloc(n*RUNESIZE);
       -        p->s = r;
       -        for(i=0; i<n; i++,r++)
       -                s += chartorune(r, s);
       -        p->n = n;
       -        p->size = n;
       -        return p;
       +    n = utflen(s);  /* don't include NUL */
       +    p = emalloc(sizeof(String));
       +    r = emalloc(n*RUNESIZE);
       +    p->s = r;
       +    for(i=0; i<n; i++,r++)
       +        s += chartorune(r, s);
       +    p->n = n;
       +    p->size = n;
       +    return p;
        }
        
        void
        freetmpstr(String *s)
        {
       -        free(s->s);
       -        free(s);
       +    free(s->s);
       +    free(s);
        }
 (DIR) diff --git a/sam/sys.c b/sam/sys.c
       @@ -10,52 +10,52 @@ static int inerror=FALSE;
        void
        resetsys(void)
        {
       -        inerror = FALSE;
       +    inerror = FALSE;
        }
        
        void
        syserror(char *a)
        {
       -        char buf[ERRLEN];
       -
       -        if(!inerror){
       -                inerror=TRUE;
       -                errstr(buf);
       -                dprint("%s: ", a);
       -                error_s(Eio, buf);
       -        }
       +    char buf[ERRLEN];
       +
       +    if(!inerror){
       +        inerror=TRUE;
       +        errstr(buf);
       +        dprint("%s: ", a);
       +        error_s(Eio, buf);
       +    }
        }
        
        int
        Read(int f, void *a, int n)
        {
       -        char buf[ERRLEN];
       -
       -        if(read(f, (char *)a, n)!=n) {
       -                if (lastfile)
       -                        lastfile->state = Readerr;
       -                errstr(buf);
       -                if (downloaded)
       -                        fprintf(stderr, "read error: %s\n", buf);
       -                rescue();
       -                exits("read");
       -        }
       -        return n;
       +    char buf[ERRLEN];
       +
       +    if(read(f, (char *)a, n)!=n) {
       +        if (lastfile)
       +            lastfile->state = Readerr;
       +        errstr(buf);
       +        if (downloaded)
       +            fprintf(stderr, "read error: %s\n", buf);
       +        rescue();
       +        exits("read");
       +    }
       +    return n;
        }
        
        int
        Write(int f, void *a, int n)
        {
       -        int m;
       +    int m;
        
       -        if((m=write(f, (char *)a, n))!=n)
       -                syserror("write");
       -        return m;
       +    if((m=write(f, (char *)a, n))!=n)
       +        syserror("write");
       +    return m;
        }
        
        void
        Seek(int f, long n, int w)
        {
       -        if(seek(f, n, w)==-1)
       -                syserror("seek");
       +    if(seek(f, n, w)==-1)
       +        syserror("seek");
        }
 (DIR) diff --git a/sam/unix.c b/sam/unix.c
       @@ -5,13 +5,13 @@
        #include <sys/wait.h>
        #include <signal.h>
        
       -#ifdef        NEEDVARARG
       -#include        <varargs.h>
       +#ifdef  NEEDVARARG
       +#include    <varargs.h>
        #else
       -#include        <stdarg.h>
       +#include    <stdarg.h>
        #endif
        
       -Rune        samname[] = { '~', '~', 's', 'a', 'm', '~', '~', 0 };
       +Rune    samname[] = { '~', '~', 's', 'a', 'm', '~', '~', 0 };
        
        static Rune l1[] = { '{', '[', '(', '<', 0253, 0};
        static Rune l2[] = { '\n', 0};
       @@ -26,98 +26,98 @@ Rune *right[]= { r1, r2, r3, 0};
        void
        print_ss(char *s, String *a, String *b)
        {
       -        char *ap, *bp, *cp;
       -        Rune *rp;
       -
       -        ap = emalloc(a->n+1);
       -        for (cp = ap, rp = a->s; *rp; rp++)
       -                cp += runetochar(cp, rp);
       -        *cp = 0;
       -        bp = emalloc(b->n+1);
       -        for (cp = bp, rp = b->s; *rp; rp++)
       -                cp += runetochar(cp, rp);
       -        *cp = 0;
       -        dprint("?warning: %s `%.*s' and `%.*s'\n", s, a->n, ap, b->n, bp);
       -        free(ap);
       -        free(bp);
       +    char *ap, *bp, *cp;
       +    Rune *rp;
       +
       +    ap = emalloc(a->n+1);
       +    for (cp = ap, rp = a->s; *rp; rp++)
       +        cp += runetochar(cp, rp);
       +    *cp = 0;
       +    bp = emalloc(b->n+1);
       +    for (cp = bp, rp = b->s; *rp; rp++)
       +        cp += runetochar(cp, rp);
       +    *cp = 0;
       +    dprint("?warning: %s `%.*s' and `%.*s'\n", s, a->n, ap, b->n, bp);
       +    free(ap);
       +    free(bp);
        }
        
        void
        print_s(char *s, String *a)
        {
       -        char *ap, *cp;
       -        Rune *rp;
       -
       -        ap = emalloc(a->n+1);
       -        for (cp = ap, rp = a->s; *rp; rp++)
       -                cp += runetochar(cp, rp);
       -        *cp = 0;
       -        dprint("?warning: %s `%.*s'\n", s, a->n, ap);
       -        free(ap);
       +    char *ap, *cp;
       +    Rune *rp;
       +
       +    ap = emalloc(a->n+1);
       +    for (cp = ap, rp = a->s; *rp; rp++)
       +        cp += runetochar(cp, rp);
       +    *cp = 0;
       +    dprint("?warning: %s `%.*s'\n", s, a->n, ap);
       +    free(ap);
        }
        
        int
        statfile(char *name, ulong *dev, ulong *id, long *time, long *length, long *appendonly)
        {
       -        struct stat dirb;
       -
       -        if (stat(name, &dirb) == -1)
       -                return -1;
       -        if (dev)
       -                *dev = dirb.st_dev;
       -        if (id)
       -                *id = dirb.st_ino;
       -        if (time)
       -                *time = dirb.st_mtime;
       -        if (length)
       -                *length = dirb.st_size;
       -        if(appendonly)
       -                *appendonly = 0;
       -        return 1;
       +    struct stat dirb;
       +
       +    if (stat(name, &dirb) == -1)
       +        return -1;
       +    if (dev)
       +        *dev = dirb.st_dev;
       +    if (id)
       +        *id = dirb.st_ino;
       +    if (time)
       +        *time = dirb.st_mtime;
       +    if (length)
       +        *length = dirb.st_size;
       +    if(appendonly)
       +        *appendonly = 0;
       +    return 1;
        }
        
        int
        statfd(int fd, ulong *dev, ulong *id, long *time, long *length, long *appendonly)
        {
       -        struct stat dirb;
       -
       -        if (fstat(fd, &dirb) == -1)
       -                return -1;
       -        if (dev)
       -                *dev = dirb.st_dev;
       -        if (id)
       -                *id = dirb.st_ino;
       -        if (time)
       -                *time = dirb.st_mtime;
       -        if (length)
       -                *length = dirb.st_size;
       -        if(appendonly)
       -                *appendonly = 0;
       -        return 1;
       +    struct stat dirb;
       +
       +    if (fstat(fd, &dirb) == -1)
       +        return -1;
       +    if (dev)
       +        *dev = dirb.st_dev;
       +    if (id)
       +        *id = dirb.st_ino;
       +    if (time)
       +        *time = dirb.st_mtime;
       +    if (length)
       +        *length = dirb.st_size;
       +    if(appendonly)
       +        *appendonly = 0;
       +    return 1;
        }
        
        void
        hup(int sig)
        {
       -        rescue();
       -        exit(1);
       +    rescue();
       +    exit(1);
        }
        
        int
        notify (void(*f)(void *, char *))
        {
       -        signal(SIGINT, SIG_IGN);
       -        signal(SIGHUP, hup);
       -        signal(SIGPIPE, SIG_IGN);
       -#ifdef        v10
       -        close(3);                /* redirect v10 /dev/tty */
       -        open("/dev/null", 2);
       +    signal(SIGINT, SIG_IGN);
       +    signal(SIGHUP, hup);
       +    signal(SIGPIPE, SIG_IGN);
       +#ifdef  v10
       +    close(3);       /* redirect v10 /dev/tty */
       +    open("/dev/null", 2);
        #endif
       -        return 1;
       +    return 1;
        }
        
        void
       -notifyf(void *a, char *b)        /* never called */
       +notifyf(void *a, char *b)   /* never called */
        {
        }
        
       @@ -133,61 +133,61 @@ newtmp(void)
        void
        samerr(char *buf)
        {
       -        snprintf(buf, PATH_MAX, "%s/sam.err", getenv("HOME") ? getenv("HOME") : "/tmp", getuser());
       +    snprintf(buf, PATH_MAX, "%s/sam.err", getenv("HOME") ? getenv("HOME") : "/tmp", getuser());
        }
        
        int
        waitfor(int pid)
        {
       -        int wm;
       -        int rpid;
       +    int wm;
       +    int rpid;
        
       -        do; while((rpid = wait(&wm)) != pid && rpid != -1);
       -        return (WEXITSTATUS(wm));
       +    do; while((rpid = wait(&wm)) != pid && rpid != -1);
       +    return (WEXITSTATUS(wm));
        }
        
        void*
        emalloc(ulong n)
        {
       -        void *p;
       -
       -        if (n < sizeof(int))
       -                n = sizeof(int);
       -        p = malloc(n);
       -        if(p == 0)
       -                panic("malloc fails");
       -        memset(p, 0, n);
       -        return p;
       +    void *p;
       +
       +    if (n < sizeof(int))
       +        n = sizeof(int);
       +    p = malloc(n);
       +    if(p == 0)
       +        panic("malloc fails");
       +    memset(p, 0, n);
       +    return p;
        }
        
        void*
        erealloc(void *p, ulong n)
        {
       -        p = realloc(p, n);
       -        if(p == 0)
       -                panic("realloc fails");
       -        return p;
       +    p = realloc(p, n);
       +    if(p == 0)
       +        panic("realloc fails");
       +    return p;
        }
        
        void
        exits(char *message)
        {
        
       -        if (message == 0)
       -                exit(0);
       -        else
       -                exit(1);
       +    if (message == 0)
       +        exit(0);
       +    else
       +        exit(1);
        }
        
        void
        dprint(char *z, ...)
        {
       -        va_list args;
       -        char buf[BLOCKSIZE];
       +    va_list args;
       +    char buf[BLOCKSIZE];
        
       -        va_start(args, z);
       -        vsprintf(buf, z, args);
       -        termwrite(buf);
       -        va_end(args);
       +    va_start(args, z);
       +    vsprintf(buf, z, args);
       +    termwrite(buf);
       +    va_end(args);
        }
        
 (DIR) diff --git a/sam/xec.c b/sam/xec.c
       @@ -2,491 +2,491 @@
        #include "sam.h"
        #include "parse.h"
        
       -int        Glooping;
       -int        nest;
       +int Glooping;
       +int nest;
        
       -int        append(File*, Cmd*, Posn);
       -int        display(File*);
       -void        looper(File*, Cmd*, int);
       -void        filelooper(Cmd*, int);
       -void        linelooper(File*, Cmd*);
       +int append(File*, Cmd*, Posn);
       +int display(File*);
       +void    looper(File*, Cmd*, int);
       +void    filelooper(Cmd*, int);
       +void    linelooper(File*, Cmd*);
        
        void
        resetxec(void)
        {
       -        Glooping = nest = 0;
       +    Glooping = nest = 0;
        }
        
        int
        cmdexec(File *f, Cmd *cp)
        {
       -        int i;
       -        Addr *ap;
       -        Address a;
       -
       -        if(f && f->state==Unread)
       -                load(f);
       -        if(f==0 && (cp->addr==0 || cp->addr->type!='"') &&
       -            !utfrune("bBnqUXY!", cp->cmdc) &&
       -            cp->cmdc!=('c'|0x100) && !(cp->cmdc=='D' && cp->ctext))
       -                error(Enofile);
       -        i = lookup(cp->cmdc);
       -        if(i >= 0 && cmdtab[i].defaddr != aNo){
       -                if((ap=cp->addr)==0 && cp->cmdc!='\n'){
       -                        cp->addr = ap = newaddr();
       -                        ap->type = '.';
       -                        if(cmdtab[i].defaddr == aAll)
       -                                ap->type = '*';
       -                }else if(ap && ap->type=='"' && ap->next==0 && cp->cmdc!='\n'){
       -                        ap->next = newaddr();
       -                        ap->next->type = '.';
       -                        if(cmdtab[i].defaddr == aAll)
       -                                ap->next->type = '*';
       -                }
       -                if(cp->addr){        /* may be false for '\n' (only) */
       -                        static Address none = {0,0,0};
       -                        if(f)
       -                                addr = address(ap, f->dot, 0);
       -                        else        /* a " */
       -                                addr = address(ap, none, 0);
       -                        f = addr.f;
       -                }
       -        }
       -        current(f);
       -        switch(cp->cmdc){
       -        case '{':
       -                a = cp->addr? address(cp->addr, f->dot, 0): f->dot;
       -                for(cp = cp->ccmd; cp; cp = cp->next){
       -                        a.f->dot = a;
       -                        cmdexec(a.f, cp);
       -                }
       -                break;
       -        default:
       -                i=(*cmdtab[i].fn)(f, cp);
       -                return i;
       -        }
       -        return 1;
       +    int i;
       +    Addr *ap;
       +    Address a;
       +
       +    if(f && f->state==Unread)
       +        load(f);
       +    if(f==0 && (cp->addr==0 || cp->addr->type!='"') &&
       +        !utfrune("bBnqUXY!", cp->cmdc) &&
       +        cp->cmdc!=('c'|0x100) && !(cp->cmdc=='D' && cp->ctext))
       +        error(Enofile);
       +    i = lookup(cp->cmdc);
       +    if(i >= 0 && cmdtab[i].defaddr != aNo){
       +        if((ap=cp->addr)==0 && cp->cmdc!='\n'){
       +            cp->addr = ap = newaddr();
       +            ap->type = '.';
       +            if(cmdtab[i].defaddr == aAll)
       +                ap->type = '*';
       +        }else if(ap && ap->type=='"' && ap->next==0 && cp->cmdc!='\n'){
       +            ap->next = newaddr();
       +            ap->next->type = '.';
       +            if(cmdtab[i].defaddr == aAll)
       +                ap->next->type = '*';
       +        }
       +        if(cp->addr){   /* may be false for '\n' (only) */
       +            static Address none = {0,0,0};
       +            if(f)
       +                addr = address(ap, f->dot, 0);
       +            else    /* a " */
       +                addr = address(ap, none, 0);
       +            f = addr.f;
       +        }
       +    }
       +    current(f);
       +    switch(cp->cmdc){
       +    case '{':
       +        a = cp->addr? address(cp->addr, f->dot, 0): f->dot;
       +        for(cp = cp->ccmd; cp; cp = cp->next){
       +            a.f->dot = a;
       +            cmdexec(a.f, cp);
       +        }
       +        break;
       +    default:
       +        i=(*cmdtab[i].fn)(f, cp);
       +        return i;
       +    }
       +    return 1;
        }
        
        
        int
        a_cmd(File *f, Cmd *cp)
        {
       -        return append(f, cp, addr.r.p2);
       +    return append(f, cp, addr.r.p2);
        }
        
        int
        b_cmd(File *f, Cmd *cp)
        {
       -        USED(f);
       -        f = cp->cmdc=='b'? tofile(cp->ctext) : getfile(cp->ctext);
       -        if(f->state == Unread)
       -                load(f);
       -        else if(nest == 0)
       -                filename(f);
       -        return TRUE;
       +    USED(f);
       +    f = cp->cmdc=='b'? tofile(cp->ctext) : getfile(cp->ctext);
       +    if(f->state == Unread)
       +        load(f);
       +    else if(nest == 0)
       +        filename(f);
       +    return TRUE;
        }
        
        int
        c_cmd(File *f, Cmd *cp)
        {
       -        Fdelete(f, addr.r.p1, addr.r.p2);
       -        f->ndot.r.p1 = f->ndot.r.p2 = addr.r.p2;
       -        return append(f, cp, addr.r.p2);
       +    Fdelete(f, addr.r.p1, addr.r.p2);
       +    f->ndot.r.p1 = f->ndot.r.p2 = addr.r.p2;
       +    return append(f, cp, addr.r.p2);
        }
        
        int
        d_cmd(File *f, Cmd *cp)
        {
       -        USED(cp);
       -        Fdelete(f, addr.r.p1, addr.r.p2);
       -        f->ndot.r.p1 = f->ndot.r.p2 = addr.r.p1;
       -        return TRUE;
       +    USED(cp);
       +    Fdelete(f, addr.r.p1, addr.r.p2);
       +    f->ndot.r.p1 = f->ndot.r.p2 = addr.r.p1;
       +    return TRUE;
        }
        
        int
        D_cmd(File *f, Cmd *cp)
        {
       -        closefiles(f, cp->ctext);
       -        return TRUE;
       +    closefiles(f, cp->ctext);
       +    return TRUE;
        }
        
        int
        e_cmd(File *f, Cmd *cp)
        {
       -        if(getname(f, cp->ctext, cp->cmdc=='e')==0)
       -                error(Enoname);
       -        edit(f, cp->cmdc);
       -        return TRUE;
       +    if(getname(f, cp->ctext, cp->cmdc=='e')==0)
       +        error(Enoname);
       +    edit(f, cp->cmdc);
       +    return TRUE;
        }
        
        int
        f_cmd(File *f, Cmd *cp)
        {
       -        getname(f, cp->ctext, TRUE);
       -        filename(f);
       -        return TRUE;
       +    getname(f, cp->ctext, TRUE);
       +    filename(f);
       +    return TRUE;
        }
        
        int
        g_cmd(File *f, Cmd *cp)
        {
       -        if(f!=addr.f)panic("g_cmd f!=addr.f");
       -        compile(cp->re);
       -        if(execute(f, addr.r.p1, addr.r.p2) ^ cp->cmdc=='v'){
       -                f->dot = addr;
       -                return cmdexec(f, cp->ccmd);
       -        }
       -        return TRUE;
       +    if(f!=addr.f)panic("g_cmd f!=addr.f");
       +    compile(cp->re);
       +    if(execute(f, addr.r.p1, addr.r.p2) ^ cp->cmdc=='v'){
       +        f->dot = addr;
       +        return cmdexec(f, cp->ccmd);
       +    }
       +    return TRUE;
        }
        
        int
        i_cmd(File *f, Cmd *cp)
        {
       -        return append(f, cp, addr.r.p1);
       +    return append(f, cp, addr.r.p1);
        }
        
        int
        k_cmd(File *f, Cmd *cp)
        {
       -        USED(cp);
       -        f->mark = addr.r;
       -        return TRUE;
       +    USED(cp);
       +    f->mark = addr.r;
       +    return TRUE;
        }
        
        int
        m_cmd(File *f, Cmd *cp)
        {
       -        Address addr2;
       -
       -        addr2 = address(cp->caddr, f->dot, 0);
       -        if(cp->cmdc=='m')
       -                move(f, addr2);
       -        else
       -                copy(f, addr2);
       -        return TRUE;
       +    Address addr2;
       +
       +    addr2 = address(cp->caddr, f->dot, 0);
       +    if(cp->cmdc=='m')
       +        move(f, addr2);
       +    else
       +        copy(f, addr2);
       +    return TRUE;
        }
        
        int
        n_cmd(File *f, Cmd *cp)
        {
       -        int i;
       -        USED(f);
       -        USED(cp);
       -        for(i = 0; i<file.nused; i++){
       -                if(file.filepptr[i] == cmd)
       -                        continue;
       -                f = file.filepptr[i];
       -                Strduplstr(&genstr, &f->name);
       -                filename(f);
       -        }
       -        return TRUE;
       +    int i;
       +    USED(f);
       +    USED(cp);
       +    for(i = 0; i<file.nused; i++){
       +        if(file.filepptr[i] == cmd)
       +            continue;
       +        f = file.filepptr[i];
       +        Strduplstr(&genstr, &f->name);
       +        filename(f);
       +    }
       +    return TRUE;
        }
        
        int
        p_cmd(File *f, Cmd *cp)
        {
       -        USED(cp);
       -        return display(f);
       +    USED(cp);
       +    return display(f);
        }
        
        int
        q_cmd(File *f, Cmd *cp)
        {
       -        USED(cp);
       -        USED(f);
       -        trytoquit();
       -        if(downloaded){
       -                outT0(Hexit);
       -                return TRUE;
       -        }
       -        return FALSE;
       +    USED(cp);
       +    USED(f);
       +    trytoquit();
       +    if(downloaded){
       +        outT0(Hexit);
       +        return TRUE;
       +    }
       +    return FALSE;
        }
        
        int
        s_cmd(File *f, Cmd *cp)
        {
       -        int i, j, c, n;
       -        Posn p1, op, didsub = 0, delta = 0;
       -
       -        n = cp->num;
       -        op= -1;
       -        compile(cp->re);
       -        for(p1 = addr.r.p1; p1<=addr.r.p2 && execute(f, p1, addr.r.p2); ){
       -                if(sel.p[0].p1==sel.p[0].p2){        /* empty match? */
       -                        if(sel.p[0].p1==op){
       -                                p1++;
       -                                continue;
       -                        }
       -                        p1 = sel.p[0].p2+1;
       -                }else
       -                        p1 = sel.p[0].p2;
       -                op = sel.p[0].p2;
       -                if(--n>0)
       -                        continue;
       -                Strzero(&genstr);
       -                for(i = 0; i<cp->ctext->n; i++)
       -                        if((c = cp->ctext->s[i])=='\\' && i<cp->ctext->n-1){
       -                                c = cp->ctext->s[++i];
       -                                if('1'<=c && c<='9') {
       -                                        j = c-'0';
       -                                        if(sel.p[j].p2-sel.p[j].p1>BLOCKSIZE)
       -                                                error(Elongtag);
       -                                        Fchars(f, genbuf, sel.p[j].p1, sel.p[j].p2);
       -                                        Strinsert(&genstr, tmprstr(genbuf, (sel.p[j].p2-sel.p[j].p1)), genstr.n);
       -                                }else
       -                                         Straddc(&genstr, c);
       -                        }else if(c!='&')
       -                                Straddc(&genstr, c);
       -                        else{
       -                                if(sel.p[0].p2-sel.p[0].p1>BLOCKSIZE)
       -                                        error(Elongrhs);
       -                                Fchars(f, genbuf, sel.p[0].p1, sel.p[0].p2);
       -                                Strinsert(&genstr,
       -                                        tmprstr(genbuf, (int)(sel.p[0].p2-sel.p[0].p1)),
       -                                        genstr.n);
       -                        }
       -                if(sel.p[0].p1!=sel.p[0].p2){
       -                        Fdelete(f, sel.p[0].p1, sel.p[0].p2);
       -                        delta-=sel.p[0].p2-sel.p[0].p1;
       -                }
       -                if(genstr.n){
       -                        Finsert(f, &genstr, sel.p[0].p2);
       -                        delta+=genstr.n;
       -                }
       -                didsub = 1;
       -                if(!cp->flag)
       -                        break;
       -        }
       -        if(!didsub && nest==0)
       -                error(Enosub);
       -        f->ndot.r.p1 = addr.r.p1, f->ndot.r.p2 = addr.r.p2+delta;
       -        return TRUE;
       +    int i, j, c, n;
       +    Posn p1, op, didsub = 0, delta = 0;
       +
       +    n = cp->num;
       +    op= -1;
       +    compile(cp->re);
       +    for(p1 = addr.r.p1; p1<=addr.r.p2 && execute(f, p1, addr.r.p2); ){
       +        if(sel.p[0].p1==sel.p[0].p2){   /* empty match? */
       +            if(sel.p[0].p1==op){
       +                p1++;
       +                continue;
       +            }
       +            p1 = sel.p[0].p2+1;
       +        }else
       +            p1 = sel.p[0].p2;
       +        op = sel.p[0].p2;
       +        if(--n>0)
       +            continue;
       +        Strzero(&genstr);
       +        for(i = 0; i<cp->ctext->n; i++)
       +            if((c = cp->ctext->s[i])=='\\' && i<cp->ctext->n-1){
       +                c = cp->ctext->s[++i];
       +                if('1'<=c && c<='9') {
       +                    j = c-'0';
       +                    if(sel.p[j].p2-sel.p[j].p1>BLOCKSIZE)
       +                        error(Elongtag);
       +                    Fchars(f, genbuf, sel.p[j].p1, sel.p[j].p2);
       +                    Strinsert(&genstr, tmprstr(genbuf, (sel.p[j].p2-sel.p[j].p1)), genstr.n);
       +                }else
       +                    Straddc(&genstr, c);
       +            }else if(c!='&')
       +                Straddc(&genstr, c);
       +            else{
       +                if(sel.p[0].p2-sel.p[0].p1>BLOCKSIZE)
       +                    error(Elongrhs);
       +                Fchars(f, genbuf, sel.p[0].p1, sel.p[0].p2);
       +                Strinsert(&genstr,
       +                    tmprstr(genbuf, (int)(sel.p[0].p2-sel.p[0].p1)),
       +                    genstr.n);
       +            }
       +        if(sel.p[0].p1!=sel.p[0].p2){
       +            Fdelete(f, sel.p[0].p1, sel.p[0].p2);
       +            delta-=sel.p[0].p2-sel.p[0].p1;
       +        }
       +        if(genstr.n){
       +            Finsert(f, &genstr, sel.p[0].p2);
       +            delta+=genstr.n;
       +        }
       +        didsub = 1;
       +        if(!cp->flag)
       +            break;
       +    }
       +    if(!didsub && nest==0)
       +        error(Enosub);
       +    f->ndot.r.p1 = addr.r.p1, f->ndot.r.p2 = addr.r.p2+delta;
       +    return TRUE;
        }
        
        int
        u_cmd(File *f, Cmd *cp)
        {
       -        int n;
       -        USED(f);
       -        USED(cp);
       -        n = cp->num;
       -        while(n-- && undo())
       -                ;
       -        return TRUE;
       +    int n;
       +    USED(f);
       +    USED(cp);
       +    n = cp->num;
       +    while(n-- && undo())
       +        ;
       +    return TRUE;
        }
        
        int
        w_cmd(File *f, Cmd *cp)
        {
       -        if(getname(f, cp->ctext, FALSE)==0)
       -                error(Enoname);
       -        writef(f);
       -        return TRUE;
       +    if(getname(f, cp->ctext, FALSE)==0)
       +        error(Enoname);
       +    writef(f);
       +    return TRUE;
        }
        
        int
        x_cmd(File *f, Cmd *cp)
        {
       -        if(cp->re)
       -                looper(f, cp, cp->cmdc=='x');
       -        else
       -                linelooper(f, cp);
       -        return TRUE;
       +    if(cp->re)
       +        looper(f, cp, cp->cmdc=='x');
       +    else
       +        linelooper(f, cp);
       +    return TRUE;
        }
        
        int
        X_cmd(File *f, Cmd *cp)
        {
       -        USED(f);
       -        filelooper(cp, cp->cmdc=='X');
       -        return TRUE;
       +    USED(f);
       +    filelooper(cp, cp->cmdc=='X');
       +    return TRUE;
        }
        
        int
        plan9_cmd(File *f, Cmd *cp)
        {
       -        plan9(f, cp->cmdc, cp->ctext, nest);
       -        return TRUE;
       +    plan9(f, cp->cmdc, cp->ctext, nest);
       +    return TRUE;
        }
        
        int
        eq_cmd(File *f, Cmd *cp)
        {
       -        int charsonly;
       -
       -        switch(cp->ctext->n){
       -        case 1:
       -                charsonly = FALSE;
       -                break;
       -        case 2:
       -                if(cp->ctext->s[0]=='#'){
       -                        charsonly = TRUE;
       -                        break;
       -                }
       -        default:
       -                SET(charsonly);
       -                error(Enewline);
       -        }
       -        printposn(f, charsonly);
       -        return TRUE;
       +    int charsonly;
       +
       +    switch(cp->ctext->n){
       +    case 1:
       +        charsonly = FALSE;
       +        break;
       +    case 2:
       +        if(cp->ctext->s[0]=='#'){
       +            charsonly = TRUE;
       +            break;
       +        }
       +    default:
       +        SET(charsonly);
       +        error(Enewline);
       +    }
       +    printposn(f, charsonly);
       +    return TRUE;
        }
        
        int
        nl_cmd(File *f, Cmd *cp)
        {
       -        if(cp->addr == 0){
       -                /* First put it on newline boundaries */
       -                addr = lineaddr((Posn)0, f->dot, -1);
       -                addr.r.p2 = lineaddr((Posn)0, f->dot, 1).r.p2;
       -                if(addr.r.p1==f->dot.r.p1 && addr.r.p2==f->dot.r.p2)
       -                        addr = lineaddr((Posn)1, f->dot, 1);
       -                display(f);
       -        }else if(downloaded)
       -                moveto(f, addr.r);
       -        else
       -                display(f);
       -        return TRUE;
       +    if(cp->addr == 0){
       +        /* First put it on newline boundaries */
       +        addr = lineaddr((Posn)0, f->dot, -1);
       +        addr.r.p2 = lineaddr((Posn)0, f->dot, 1).r.p2;
       +        if(addr.r.p1==f->dot.r.p1 && addr.r.p2==f->dot.r.p2)
       +            addr = lineaddr((Posn)1, f->dot, 1);
       +        display(f);
       +    }else if(downloaded)
       +        moveto(f, addr.r);
       +    else
       +        display(f);
       +    return TRUE;
        }
        
        int
        cd_cmd(File *f, Cmd *cp)
        {
       -        USED(f);
       -        cd(cp->ctext);
       -        return TRUE;
       +    USED(f);
       +    cd(cp->ctext);
       +    return TRUE;
        }
        
        int
        append(File *f, Cmd *cp, Posn p)
        {
       -        if(cp->ctext->n>0 && cp->ctext->s[cp->ctext->n-1]==0)
       -                --cp->ctext->n;
       -        if(cp->ctext->n>0)
       -                Finsert(f, cp->ctext, p);
       -        f->ndot.r.p1 = p;
       -        f->ndot.r.p2 = p+cp->ctext->n;
       -        return TRUE;
       +    if(cp->ctext->n>0 && cp->ctext->s[cp->ctext->n-1]==0)
       +        --cp->ctext->n;
       +    if(cp->ctext->n>0)
       +        Finsert(f, cp->ctext, p);
       +    f->ndot.r.p1 = p;
       +    f->ndot.r.p2 = p+cp->ctext->n;
       +    return TRUE;
        }
        
        int
        display(File *f)
        {
       -        Posn p1, p2;
       -        int np, n;
       -        char *c;
       -
       -        p1 = addr.r.p1;
       -        p2 = addr.r.p2;
       -        while(p1 < p2){
       -                np = p2-p1;
       -                if(np>BLOCKSIZE-1)
       -                        np = BLOCKSIZE-1;
       -                n = Fchars(f, genbuf, p1, p1+np);
       -                if(n <= 0)
       -                        panic("display");
       -                genbuf[n] = 0;
       -                c = Strtoc(tmprstr(genbuf, n+1));
       -                if(downloaded)
       -                        termwrite(c);
       -                else
       -                        Write(1, c, strlen(c));
       -                free(c);
       -                p1+=n;
       -        }
       -        f->dot = addr;
       -        return TRUE;
       +    Posn p1, p2;
       +    int np, n;
       +    char *c;
       +
       +    p1 = addr.r.p1;
       +    p2 = addr.r.p2;
       +    while(p1 < p2){
       +        np = p2-p1;
       +        if(np>BLOCKSIZE-1)
       +            np = BLOCKSIZE-1;
       +        n = Fchars(f, genbuf, p1, p1+np);
       +        if(n <= 0)
       +            panic("display");
       +        genbuf[n] = 0;
       +        c = Strtoc(tmprstr(genbuf, n+1));
       +        if(downloaded)
       +            termwrite(c);
       +        else
       +            Write(1, c, strlen(c));
       +        free(c);
       +        p1+=n;
       +    }
       +    f->dot = addr;
       +    return TRUE;
        }
        
        void
        looper(File *f, Cmd *cp, int xy)
        {
       -        Posn p, op;
       -        Range r;
       -
       -        r = addr.r;
       -        op= xy? -1 : r.p1;
       -        nest++;
       -        compile(cp->re);
       -        for(p = r.p1; p<=r.p2; ){
       -                if(!execute(f, p, r.p2)){ /* no match, but y should still run */
       -                        if(xy || op>r.p2)
       -                                break;
       -                        f->dot.r.p1 = op, f->dot.r.p2 = r.p2;
       -                        p = r.p2+1;        /* exit next loop */
       -                }else{
       -                        if(sel.p[0].p1==sel.p[0].p2){        /* empty match? */
       -                                if(sel.p[0].p1==op){
       -                                        p++;
       -                                        continue;
       -                                }
       -                                p = sel.p[0].p2+1;
       -                        }else
       -                                p = sel.p[0].p2;
       -                        if(xy)
       -                                f->dot.r = sel.p[0];
       -                        else
       -                                f->dot.r.p1 = op, f->dot.r.p2 = sel.p[0].p1;
       -                }
       -                op = sel.p[0].p2;
       -                cmdexec(f, cp->ccmd);
       -                compile(cp->re);
       -        }
       -        --nest;
       +    Posn p, op;
       +    Range r;
       +
       +    r = addr.r;
       +    op= xy? -1 : r.p1;
       +    nest++;
       +    compile(cp->re);
       +    for(p = r.p1; p<=r.p2; ){
       +        if(!execute(f, p, r.p2)){ /* no match, but y should still run */
       +            if(xy || op>r.p2)
       +                break;
       +            f->dot.r.p1 = op, f->dot.r.p2 = r.p2;
       +            p = r.p2+1; /* exit next loop */
       +        }else{
       +            if(sel.p[0].p1==sel.p[0].p2){   /* empty match? */
       +                if(sel.p[0].p1==op){
       +                    p++;
       +                    continue;
       +                }
       +                p = sel.p[0].p2+1;
       +            }else
       +                p = sel.p[0].p2;
       +            if(xy)
       +                f->dot.r = sel.p[0];
       +            else
       +                f->dot.r.p1 = op, f->dot.r.p2 = sel.p[0].p1;
       +        }
       +        op = sel.p[0].p2;
       +        cmdexec(f, cp->ccmd);
       +        compile(cp->re);
       +    }
       +    --nest;
        }
        
        void
        linelooper(File *f, Cmd *cp)
        {
       -        Posn p;
       -        Range r, linesel;
       -        Address a3;
       -
       -        nest++;
       -        r = addr.r;
       -        a3.f = f;
       -        a3.r.p1 = a3.r.p2 = r.p1;
       -        for(p = r.p1; p<r.p2; p = a3.r.p2){
       -                a3.r.p1 = a3.r.p2;
       -/*pjw                if(p!=r.p1 || (linesel = lineaddr((Posn)0, a3, 1)).r.p2==p)*/
       -                if(p!=r.p1 || ((linesel = lineaddr((Posn)0, a3, 1).r), linesel.p2==p))
       -                        linesel = lineaddr((Posn)1, a3, 1).r;
       -                if(linesel.p1 >= r.p2)
       -                        break;
       -                if(linesel.p2 >= r.p2)
       -                        linesel.p2 = r.p2;
       -                if(linesel.p2 > linesel.p1)
       -                        if(linesel.p1>=a3.r.p2 && linesel.p2>a3.r.p2){
       -                                f->dot.r = linesel;
       -                                cmdexec(f, cp->ccmd);
       -                                a3.r = linesel;
       -                                continue;
       -                        }
       -                break;
       -        }
       -        --nest;
       +    Posn p;
       +    Range r, linesel;
       +    Address a3;
       +
       +    nest++;
       +    r = addr.r;
       +    a3.f = f;
       +    a3.r.p1 = a3.r.p2 = r.p1;
       +    for(p = r.p1; p<r.p2; p = a3.r.p2){
       +        a3.r.p1 = a3.r.p2;
       +/*pjw       if(p!=r.p1 || (linesel = lineaddr((Posn)0, a3, 1)).r.p2==p)*/
       +        if(p!=r.p1 || ((linesel = lineaddr((Posn)0, a3, 1).r), linesel.p2==p))
       +            linesel = lineaddr((Posn)1, a3, 1).r;
       +        if(linesel.p1 >= r.p2)
       +            break;
       +        if(linesel.p2 >= r.p2)
       +            linesel.p2 = r.p2;
       +        if(linesel.p2 > linesel.p1)
       +            if(linesel.p1>=a3.r.p2 && linesel.p2>a3.r.p2){
       +                f->dot.r = linesel;
       +                cmdexec(f, cp->ccmd);
       +                a3.r = linesel;
       +                continue;
       +            }
       +        break;
       +    }
       +    --nest;
        }
        
        void
        filelooper(Cmd *cp, int XY)
        {
       -        File *f, *cur;
       -        int i;
       -
       -        if(Glooping++)
       -                error(EnestXY);
       -        nest++;
       -        settempfile();
       -        cur = curfile;
       -        for(i = 0; i<tempfile.nused; i++){
       -                f = tempfile.filepptr[i];
       -                if(f==cmd)
       -                        continue;
       -                if(cp->re==0 || filematch(f, cp->re)==XY)
       -                        cmdexec(f, cp->ccmd);
       -        }
       -        if(cur && whichmenu(cur)>=0)        /* check that cur is still a file */
       -                current(cur);
       -        --Glooping;
       -        --nest;
       +    File *f, *cur;
       +    int i;
       +
       +    if(Glooping++)
       +        error(EnestXY);
       +    nest++;
       +    settempfile();
       +    cur = curfile;
       +    for(i = 0; i<tempfile.nused; i++){
       +        f = tempfile.filepptr[i];
       +        if(f==cmd)
       +            continue;
       +        if(cp->re==0 || filematch(f, cp->re)==XY)
       +            cmdexec(f, cp->ccmd);
       +    }
       +    if(cur && whichmenu(cur)>=0)    /* check that cur is still a file */
       +        current(cur);
       +    --Glooping;
       +    --nest;
        }
 (DIR) diff --git a/samterm/flayer.c b/samterm/flayer.c
       @@ -6,441 +6,441 @@
        #include "flayer.h"
        #include "samterm.h"
        
       -#define        DELTA        10
       +#define DELTA   10
        
       -static Flayer        **llist;        /* front to back */
       -static int        nllist;
       -static int        nlalloc;
       +static Flayer   **llist;    /* front to back */
       +static int  nllist;
       +static int  nlalloc;
        static Rectangle lDrect;
        
       -extern Bitmap        screen;
       -extern Mouse        mouse;
       +extern Bitmap   screen;
       +extern Mouse    mouse;
        
        extern unsigned long _bgpixel;
        
       -Vis                visibility(Flayer *);
       -void                newvisibilities(int);
       -void                llinsert(Flayer*);
       -void                lldelete(Flayer*);
       +Vis     visibility(Flayer *);
       +void        newvisibilities(int);
       +void        llinsert(Flayer*);
       +void        lldelete(Flayer*);
        
        void
        flstart(Rectangle r)
        {
       -        lDrect = r;
       +    lDrect = r;
        }
        
        void
        flnew(Flayer *l, Rune *(*fn)(Flayer*, long, ulong*), int u0, void *u1)
        {
       -        if(nllist == nlalloc){
       -                nlalloc += DELTA;
       -                llist = realloc(llist, nlalloc*sizeof(Flayer**));
       -                if(llist == 0)
       -                        panic("flnew");
       -        }
       -        l->textfn = fn;
       -        l->user0 = u0;
       -        l->user1 = u1;
       +    if(nllist == nlalloc){
       +        nlalloc += DELTA;
       +        llist = realloc(llist, nlalloc*sizeof(Flayer**));
       +        if(llist == 0)
       +            panic("flnew");
       +    }
       +    l->textfn = fn;
       +    l->user0 = u0;
       +    l->user1 = u1;
            l->bg = _bgpixel;
       -        llinsert(l);
       +    llinsert(l);
        }
        
        Rectangle
        flrect(Flayer *l, Rectangle r)
        {
       -        rectclip(&r, lDrect);
       -        l->entire = r;
       -        l->scroll = inset(r, FLMARGIN);
       -        r.min.x =
       -         l->scroll.max.x = r.min.x+FLMARGIN+FLSCROLLWID+(FLGAP-FLMARGIN);
       -        return r;
       +    rectclip(&r, lDrect);
       +    l->entire = r;
       +    l->scroll = inset(r, FLMARGIN);
       +    r.min.x =
       +     l->scroll.max.x = r.min.x+FLMARGIN+FLSCROLLWID+(FLGAP-FLMARGIN);
       +    return r;
        }
        
        void
        flinit(Flayer *l, Rectangle r, XftFont *ft, unsigned long bg)
        {
       -        lldelete(l);
       -        llinsert(l);
       -        l->visible = All;
       -        l->origin = l->p0 = l->p1 = 0;
       -        frinit(&l->f, inset(flrect(l, r), FLMARGIN), ft, &screen, bg);
       +    lldelete(l);
       +    llinsert(l);
       +    l->visible = All;
       +    l->origin = l->p0 = l->p1 = 0;
       +    frinit(&l->f, inset(flrect(l, r), FLMARGIN), ft, &screen, bg);
            l->bg = bg;
       -        newvisibilities(1);
       -        bitblt2(&screen, l->entire.min, &screen, l->entire, 0, 0, l->bg);
       -        scrdraw(l, 0L);
       -        flborder(l, 0);
       +    newvisibilities(1);
       +    bitblt2(&screen, l->entire.min, &screen, l->entire, 0, 0, l->bg);
       +    scrdraw(l, 0L);
       +    flborder(l, 0);
        }
        
        void
        flclose(Flayer *l)
        {
       -        if(l->visible == All)
       -                bitblt2(&screen, l->entire.min, &screen, l->entire, 0, _bgpixel, _bgpixel);
       -        else if(l->visible == Some){
       -                if(l->f.b == 0)
       -                        l->f.b = balloc(l->entire, screen.ldepth);
       -                if(l->f.b){
       -                        bitblt2(l->f.b, l->entire.min, l->f.b, l->entire, 0, _bgpixel, _bgpixel);
       -                        flrefresh(l, l->entire, 0);
       -                }
       -        }
       -        frclear(&l->f);
       -        lldelete(l);
       -        if(l->f.b && l->visible!=All)
       -                bfree(l->f.b);
       -        l->textfn = 0;
       -        newvisibilities(1);
       +    if(l->visible == All)
       +        bitblt2(&screen, l->entire.min, &screen, l->entire, 0, _bgpixel, _bgpixel);
       +    else if(l->visible == Some){
       +        if(l->f.b == 0)
       +            l->f.b = balloc(l->entire, screen.ldepth);
       +        if(l->f.b){
       +            bitblt2(l->f.b, l->entire.min, l->f.b, l->entire, 0, _bgpixel, _bgpixel);
       +            flrefresh(l, l->entire, 0);
       +        }
       +    }
       +    frclear(&l->f);
       +    lldelete(l);
       +    if(l->f.b && l->visible!=All)
       +        bfree(l->f.b);
       +    l->textfn = 0;
       +    newvisibilities(1);
        }
        
        void
        flborder(Flayer *l, int wide)
        {
       -        if(flprepare(l)){
       -                border(l->f.b, l->entire, FLMARGIN, 0, l->bg);
       -                border(l->f.b, l->entire, wide? FLMARGIN : 1, F&~D, l->bg);
       -                if(l->visible==Some)
       -                        flrefresh(l, l->entire, 0);
       -        }
       +    if(flprepare(l)){
       +        border(l->f.b, l->entire, FLMARGIN, 0, l->bg);
       +        border(l->f.b, l->entire, wide? FLMARGIN : 1, F&~D, l->bg);
       +        if(l->visible==Some)
       +            flrefresh(l, l->entire, 0);
       +    }
        }
        
        Flayer *
        flwhich(Point p)
        {
       -        int i;
       -
       -        if(p.x==0 && p.y==0)
       -                return nllist? llist[0] : 0;
       -        for(i=0; i<nllist; i++)
       -                if(ptinrect(p, llist[i]->entire))
       -                        return llist[i];
       -        return 0;
       +    int i;
       +
       +    if(p.x==0 && p.y==0)
       +        return nllist? llist[0] : 0;
       +    for(i=0; i<nllist; i++)
       +        if(ptinrect(p, llist[i]->entire))
       +            return llist[i];
       +    return 0;
        }
        
        void
        flupfront(Flayer *l)
        {
       -        int v = l->visible;
       +    int v = l->visible;
        
       -        lldelete(l);
       -        llinsert(l);
       -        if(v!=All)
       -                newvisibilities(0);
       +    lldelete(l);
       +    llinsert(l);
       +    if(v!=All)
       +        newvisibilities(0);
        }
        
        void
        newvisibilities(int redraw)
       -        /* if redraw false, we know it's a flupfront, and needn't
       -         * redraw anyone becoming partially covered */
       +    /* if redraw false, we know it's a flupfront, and needn't
       +     * redraw anyone becoming partially covered */
        {
       -        int i;
       -        Vis ov;
       -        Flayer *l;
       -
       -        for(i = 0; i<nllist; i++){
       -                l = llist[i];
       -                ov = l->visible;
       -                l->visible = visibility(l);
       -#define        V(a, b)        (((a)<<2)|((b)))
       -                switch(V(ov, l->visible)){
       -                case V(Some, None):
       -                        if(l->f.b)
       -                                bfree(l->f.b);
       -                case V(All, None):
       -                case V(All, Some):
       -                        l->f.b = 0;
       -                        frclear(&l->f);
       -                        break;
       -
       -                case V(Some, Some):
       -                        if(l->f.b==0 && redraw)
       -                case V(None, Some):
       -                                flprepare(l);
       -                        if(l->f.b && redraw){
       -                                flrefresh(l, l->entire, 0);
       -                                bfree(l->f.b);
       -                                l->f.b = 0;
       -                                frclear(&l->f);
       -                        }
       -                case V(None, None):
       -                case V(All, All):
       -                        break;
       -
       -                case V(Some, All):
       -                        if(l->f.b){
       -                                bitblt2(&screen, l->entire.min, l->f.b, l->entire, S, 0, l->bg);
       -                                bfree(l->f.b);
       -                                l->f.b = &screen;
       -                                break;
       -                        }
       -                case V(None, All):
       -                        flprepare(l);
       -                        break;
       -                }
       -                if(ov==None && l->visible!=None)
       -                        flnewlyvisible(l);
       -        }
       +    int i;
       +    Vis ov;
       +    Flayer *l;
       +
       +    for(i = 0; i<nllist; i++){
       +        l = llist[i];
       +        ov = l->visible;
       +        l->visible = visibility(l);
       +#define V(a, b) (((a)<<2)|((b)))
       +        switch(V(ov, l->visible)){
       +        case V(Some, None):
       +            if(l->f.b)
       +                bfree(l->f.b);
       +        case V(All, None):
       +        case V(All, Some):
       +            l->f.b = 0;
       +            frclear(&l->f);
       +            break;
       +
       +        case V(Some, Some):
       +            if(l->f.b==0 && redraw)
       +        case V(None, Some):
       +                flprepare(l);
       +            if(l->f.b && redraw){
       +                flrefresh(l, l->entire, 0);
       +                bfree(l->f.b);
       +                l->f.b = 0;
       +                frclear(&l->f);
       +            }
       +        case V(None, None):
       +        case V(All, All):
       +            break;
       +
       +        case V(Some, All):
       +            if(l->f.b){
       +                bitblt2(&screen, l->entire.min, l->f.b, l->entire, S, 0, l->bg);
       +                bfree(l->f.b);
       +                l->f.b = &screen;
       +                break;
       +            }
       +        case V(None, All):
       +            flprepare(l);
       +            break;
       +        }
       +        if(ov==None && l->visible!=None)
       +            flnewlyvisible(l);
       +    }
        }
        
        void
        llinsert(Flayer *l)
        {
       -        int i;
       -        for(i=nllist; i>0; --i)
       -                llist[i]=llist[i-1];
       -        llist[0]=l;
       -        nllist++;
       +    int i;
       +    for(i=nllist; i>0; --i)
       +        llist[i]=llist[i-1];
       +    llist[0]=l;
       +    nllist++;
        }
        
        void
        lldelete(Flayer *l)
        {
       -        int i;
       -
       -        for(i=0; i<nllist; i++)
       -                if(llist[i]==l){
       -                        --nllist;
       -                        for(; i<nllist; i++)
       -                                llist[i] = llist[i+1];
       -                        return;
       -                }
       -        panic("lldelete");
       +    int i;
       +
       +    for(i=0; i<nllist; i++)
       +        if(llist[i]==l){
       +            --nllist;
       +            for(; i<nllist; i++)
       +                llist[i] = llist[i+1];
       +            return;
       +        }
       +    panic("lldelete");
        }
        
        void
        flinsert(Flayer *l, Rune *sp, Rune *ep, long p0)
        {
       -        if(flprepare(l)){
       -                frinsert(&l->f, sp, ep, p0-l->origin);
       -                scrdraw(l, scrtotal(l));
       -                if(l->visible==Some)
       -                        flrefresh(l, l->entire, 0);
       -        }
       +    if(flprepare(l)){
       +        frinsert(&l->f, sp, ep, p0-l->origin);
       +        scrdraw(l, scrtotal(l));
       +        if(l->visible==Some)
       +            flrefresh(l, l->entire, 0);
       +    }
        }
        
        void
        fldelete(Flayer *l, long p0, long p1)
        {
       -        if(flprepare(l)){
       -                p0 -= l->origin;
       -                if(p0 < 0)
       -                        p0 = 0;
       -                p1 -= l->origin;
       -                if(p1<0)
       -                        p1 = 0;
       -                frdelete(&l->f, p0, p1);
       -                scrdraw(l, scrtotal(l));
       -                if(l->visible==Some)
       -                        flrefresh(l, l->entire, 0);
       -        }
       +    if(flprepare(l)){
       +        p0 -= l->origin;
       +        if(p0 < 0)
       +            p0 = 0;
       +        p1 -= l->origin;
       +        if(p1<0)
       +            p1 = 0;
       +        frdelete(&l->f, p0, p1);
       +        scrdraw(l, scrtotal(l));
       +        if(l->visible==Some)
       +            flrefresh(l, l->entire, 0);
       +    }
        }
        
        int
        flselect(Flayer *l)
        {
       -        int ret = 0;
       -        if(l->visible!=All)
       -                flupfront(l);
       -        if(mouse.msec-l->click<Clicktime)
       -                ret = 1;
       -        frselect(&l->f, &mouse);
       -        if(l->f.p0==l->f.p1){
       -                if(ret == 1 && l->f.p0+l->origin==l->p0){
       -                        ret = 1;
       -                        l->click = 0;
       -                }else {
       -                        ret = 0;
       -                        l->click = mouse.msec;
       -                }
       -        }else {
       -                ret = 0;
       -                l->click = 0;
       -        }
       -        l->p0 = l->f.p0+l->origin, l->p1 = l->f.p1+l->origin;
       -        return ret;
       +    int ret = 0;
       +    if(l->visible!=All)
       +        flupfront(l);
       +    if(mouse.msec-l->click<Clicktime)
       +        ret = 1;
       +    frselect(&l->f, &mouse);
       +    if(l->f.p0==l->f.p1){
       +        if(ret == 1 && l->f.p0+l->origin==l->p0){
       +            ret = 1;
       +            l->click = 0;
       +        }else {
       +            ret = 0;
       +            l->click = mouse.msec;
       +        }
       +    }else {
       +        ret = 0;
       +        l->click = 0;
       +    }
       +    l->p0 = l->f.p0+l->origin, l->p1 = l->f.p1+l->origin;
       +    return ret;
        }
        
        void
        flsetselect(Flayer *l, long p0, long p1)
        {
       -        ulong fp0, fp1;
       -
       -        l->click = 0;
       -        if(l->visible==None || !flprepare(l)){
       -                l->p0 = p0, l->p1 = p1;
       -                return;
       -        }
       -        l->p0 = p0, l->p1 = p1;
       -        flfp0p1(l, &fp0, &fp1);
       -        if(fp0==l->f.p0 && fp1==l->f.p1)
       -                return;
       -        frselectp(&l->f, F&~D);
       -        l->f.p0 = fp0, l->f.p1 = fp1;
       -        frselectp(&l->f, F&~D);
       -        if(l->visible==Some)
       -                flrefresh(l, l->entire, 0);
       +    ulong fp0, fp1;
       +
       +    l->click = 0;
       +    if(l->visible==None || !flprepare(l)){
       +        l->p0 = p0, l->p1 = p1;
       +        return;
       +    }
       +    l->p0 = p0, l->p1 = p1;
       +    flfp0p1(l, &fp0, &fp1);
       +    if(fp0==l->f.p0 && fp1==l->f.p1)
       +        return;
       +    frselectp(&l->f, F&~D);
       +    l->f.p0 = fp0, l->f.p1 = fp1;
       +    frselectp(&l->f, F&~D);
       +    if(l->visible==Some)
       +        flrefresh(l, l->entire, 0);
        }
        
        void
        flfp0p1(Flayer *l, ulong *pp0, ulong *pp1)
        {
       -        long p0 = l->p0-l->origin, p1 = l->p1-l->origin;
       -
       -        if(p0 < 0)
       -                p0 = 0;
       -        if(p1 < 0)
       -                p1 = 0;
       -        if(p0 > l->f.nchars)
       -                p0 = l->f.nchars;
       -        if(p1 > l->f.nchars)
       -                p1 = l->f.nchars;
       -        *pp0 = p0;
       -        *pp1 = p1;
       +    long p0 = l->p0-l->origin, p1 = l->p1-l->origin;
       +
       +    if(p0 < 0)
       +        p0 = 0;
       +    if(p1 < 0)
       +        p1 = 0;
       +    if(p0 > l->f.nchars)
       +        p0 = l->f.nchars;
       +    if(p1 > l->f.nchars)
       +        p1 = l->f.nchars;
       +    *pp0 = p0;
       +    *pp1 = p1;
        }
        
        Rectangle
        rscale(Rectangle r, Point old, Point new)
        {
       -        r.min.x = r.min.x*new.x/old.x;
       -        r.min.y = r.min.y*new.y/old.y;
       -        r.max.x = r.max.x*new.x/old.x;
       -        r.max.y = r.max.y*new.y/old.y;
       -        return r;
       +    r.min.x = r.min.x*new.x/old.x;
       +    r.min.y = r.min.y*new.y/old.y;
       +    r.max.x = r.max.x*new.x/old.x;
       +    r.max.y = r.max.y*new.y/old.y;
       +    return r;
        }
        
        void
        flreshape(Rectangle dr)
        {
       -        int i;
       -        Flayer *l;
       -        Frame *f;
       -        Rectangle r, olDrect;
       -        int move;
       -
       -        olDrect = lDrect;
       -        lDrect = dr;
       -        move = 0;
       +    int i;
       +    Flayer *l;
       +    Frame *f;
       +    Rectangle r, olDrect;
       +    int move;
       +
       +    olDrect = lDrect;
       +    lDrect = dr;
       +    move = 0;
            bitblt2(&screen, lDrect.min, &screen, lDrect, 0, 0, _bgpixel);
        
       -        for(i=0; i<nllist; i++){
       -                l = llist[i];
       -                f = &l->f;
       -                if(move)
       -                        r = raddp(rsubp(l->entire, olDrect.min), dr.min);
       -                else{
       -                        r = raddp(rscale(rsubp(l->entire, olDrect.min),
       -                                sub(olDrect.max, olDrect.min),
       -                                sub(dr.max, dr.min)), dr.min);
       -                        if(l->visible==Some && f->b){
       -                                bfree(f->b);
       -                                frclear(f);
       -                        }
       -                        f->b = 0;
       -                        if(l->visible!=None)
       -                                frclear(f);
       -                }
       -                if(!rectclip(&r, dr))
       -                        panic("flreshape");
       -                if(r.max.x-r.min.x<100)
       -                        r.min.x = dr.min.x;
       -                if(r.max.x-r.min.x<100)
       -                        r.max.x = dr.max.x;
       -                if(r.max.y-r.min.y<2*FLMARGIN+f->fheight)
       -                        r.min.y = dr.min.y;
       -                if(r.max.y-r.min.y<2*FLMARGIN+f->fheight)
       -                        r.max.y = dr.max.y;
       -                if(!move)
       -                        l->visible = None;
       -                frsetrects(f, inset(flrect(l, r), FLMARGIN), f->b);
       -                if(!move && f->b)
       -                        scrdraw(l, scrtotal(l));
       -        }
       -        newvisibilities(1);
       +    for(i=0; i<nllist; i++){
       +        l = llist[i];
       +        f = &l->f;
       +        if(move)
       +            r = raddp(rsubp(l->entire, olDrect.min), dr.min);
       +        else{
       +            r = raddp(rscale(rsubp(l->entire, olDrect.min),
       +                sub(olDrect.max, olDrect.min),
       +                sub(dr.max, dr.min)), dr.min);
       +            if(l->visible==Some && f->b){
       +                bfree(f->b);
       +                frclear(f);
       +            }
       +            f->b = 0;
       +            if(l->visible!=None)
       +                frclear(f);
       +        }
       +        if(!rectclip(&r, dr))
       +            panic("flreshape");
       +        if(r.max.x-r.min.x<100)
       +            r.min.x = dr.min.x;
       +        if(r.max.x-r.min.x<100)
       +            r.max.x = dr.max.x;
       +        if(r.max.y-r.min.y<2*FLMARGIN+f->fheight)
       +            r.min.y = dr.min.y;
       +        if(r.max.y-r.min.y<2*FLMARGIN+f->fheight)
       +            r.max.y = dr.max.y;
       +        if(!move)
       +            l->visible = None;
       +        frsetrects(f, inset(flrect(l, r), FLMARGIN), f->b);
       +        if(!move && f->b)
       +            scrdraw(l, scrtotal(l));
       +    }
       +    newvisibilities(1);
        }
        
        int
        flprepare(Flayer *l)
        {
       -        Frame *f;
       -        ulong n;
       -        Rune *r;
       -
       -        if(l->visible == None)
       -                return 0;
       -        f = &l->f;
       -        if(f->b == 0){
       -                if(l->visible == All)
       -                        f->b = &screen;
       -                else if((f->b = balloc(l->entire, screen.ldepth))==0)
       -                        return 0;
       -                bitblt2(f->b, l->entire.min, f->b, l->entire, 0, 0, l->bg);
       -                border(f->b, l->entire, l==llist[0]? FLMARGIN : 1, F&~D, l->bg);
       -                n = f->nchars;
       -                frinit(f, f->entire, f->font, f->b, l->bg);
       -                r = (*l->textfn)(l, n, &n);
       -                frinsert(f, r, r+n, (ulong)0);
       -                frselectp(f, F&~D);
       -                flfp0p1(l, &l->f.p0, &l->f.p1);
       -                frselectp(f, F&~D);
       -                scrdraw(l, scrtotal(l));
       -        }
       -        return 1;
       +    Frame *f;
       +    ulong n;
       +    Rune *r;
       +
       +    if(l->visible == None)
       +        return 0;
       +    f = &l->f;
       +    if(f->b == 0){
       +        if(l->visible == All)
       +            f->b = &screen;
       +        else if((f->b = balloc(l->entire, screen.ldepth))==0)
       +            return 0;
       +        bitblt2(f->b, l->entire.min, f->b, l->entire, 0, 0, l->bg);
       +        border(f->b, l->entire, l==llist[0]? FLMARGIN : 1, F&~D, l->bg);
       +        n = f->nchars;
       +        frinit(f, f->entire, f->font, f->b, l->bg);
       +        r = (*l->textfn)(l, n, &n);
       +        frinsert(f, r, r+n, (ulong)0);
       +        frselectp(f, F&~D);
       +        flfp0p1(l, &l->f.p0, &l->f.p1);
       +        frselectp(f, F&~D);
       +        scrdraw(l, scrtotal(l));
       +    }
       +    return 1;
        }
        
       -static        int        somevis, someinvis, justvis;
       +static  int somevis, someinvis, justvis;
        
        Vis
        visibility(Flayer *l)
        {
       -        somevis = someinvis = 0;
       -        justvis = 1;
       -        flrefresh(l, l->entire, 0);
       -        justvis = 0;
       -        if(somevis==0)
       -                return None;
       -        if(someinvis==0)
       -                return All;
       -        return Some;
       +    somevis = someinvis = 0;
       +    justvis = 1;
       +    flrefresh(l, l->entire, 0);
       +    justvis = 0;
       +    if(somevis==0)
       +        return None;
       +    if(someinvis==0)
       +        return All;
       +    return Some;
        }
        
        void
        flrefresh(Flayer *l, Rectangle r, int i)
        {
       -        Flayer *t;
       -        Rectangle s;
       +    Flayer *t;
       +    Rectangle s;
        
            Top:
       -        if((t=llist[i++]) == l){
       -                if(!justvis)
       -                        bitblt2(&screen, r.min, l->f.b, r, S, 0, l->bg);
       -                somevis = 1;
       -        }else{
       -                if(!rectXrect(t->entire, r))
       -                        goto Top;        /* avoid stacking unnecessarily */
       -                if(t->entire.min.x>r.min.x){
       -                        s = r;
       -                        s.max.x = t->entire.min.x;
       -                        flrefresh(l, s, i);
       -                        r.min.x = t->entire.min.x;
       -                }
       -                if(t->entire.min.y>r.min.y){
       -                        s = r;
       -                        s.max.y = t->entire.min.y;
       -                        flrefresh(l, s, i);
       -                        r.min.y = t->entire.min.y;
       -                }
       -                if(t->entire.max.x<r.max.x){
       -                        s = r;
       -                        s.min.x = t->entire.max.x;
       -                        flrefresh(l, s, i);
       -                        r.max.x = t->entire.max.x;
       -                }
       -                if(t->entire.max.y<r.max.y){
       -                        s = r;
       -                        s.min.y = t->entire.max.y;
       -                        flrefresh(l, s, i);
       -                        r.max.y = t->entire.max.y;
       -                }
       -                /* remaining piece of r is blocked by t; forget about it */
       -                someinvis = 1;
       -        }
       +    if((t=llist[i++]) == l){
       +        if(!justvis)
       +            bitblt2(&screen, r.min, l->f.b, r, S, 0, l->bg);
       +        somevis = 1;
       +    }else{
       +        if(!rectXrect(t->entire, r))
       +            goto Top;   /* avoid stacking unnecessarily */
       +        if(t->entire.min.x>r.min.x){
       +            s = r;
       +            s.max.x = t->entire.min.x;
       +            flrefresh(l, s, i);
       +            r.min.x = t->entire.min.x;
       +        }
       +        if(t->entire.min.y>r.min.y){
       +            s = r;
       +            s.max.y = t->entire.min.y;
       +            flrefresh(l, s, i);
       +            r.min.y = t->entire.min.y;
       +        }
       +        if(t->entire.max.x<r.max.x){
       +            s = r;
       +            s.min.x = t->entire.max.x;
       +            flrefresh(l, s, i);
       +            r.max.x = t->entire.max.x;
       +        }
       +        if(t->entire.max.y<r.max.y){
       +            s = r;
       +            s.min.y = t->entire.max.y;
       +            flrefresh(l, s, i);
       +            r.max.y = t->entire.max.y;
       +        }
       +        /* remaining piece of r is blocked by t; forget about it */
       +        someinvis = 1;
       +    }
        }
 (DIR) diff --git a/samterm/flayer.h b/samterm/flayer.h
       @@ -1,13 +1,13 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
        #undef None
        typedef enum Vis{
       -        None=0,
       -        Some,
       -        All
       +    None=0,
       +    Some,
       +    All
        }Vis;
        
        enum{
       -        Clicktime=1000                /* one second */
       +    Clicktime=1000      /* one second */
        };
        
        typedef struct Flayer Flayer;
       @@ -18,35 +18,35 @@ typedef struct Flayer Flayer;
        struct Flayer
        {
            unsigned long bg;
       -        Frame                f;
       -        long                origin;        /* offset of first char in flayer */
       -        long                p0, p1;
       -        long                click;        /* time at which selection click occurred, in HZ */
       -        Rune                *(*textfn)(Flayer*, long, ulong*);
       -        int                user0;
       -        void                *user1;
       -        Rectangle        entire;
       -        Rectangle        scroll;
       -        Vis                visible;
       +    Frame       f;
       +    long        origin; /* offset of first char in flayer */
       +    long        p0, p1;
       +    long        click;  /* time at which selection click occurred, in HZ */
       +    Rune        *(*textfn)(Flayer*, long, ulong*);
       +    int     user0;
       +    void        *user1;
       +    Rectangle   entire;
       +    Rectangle   scroll;
       +    Vis     visible;
        };
        
       -void        flborder(Flayer*, int);
       -void        flclose(Flayer*);
       -void        fldelete(Flayer*, long, long);
       -void        flfp0p1(Flayer*, ulong*, ulong*);
       -void        flinit(Flayer*, Rectangle, XftFont*, unsigned long bg);
       -void        flinsert(Flayer*, Rune*, Rune*, long);
       -void        flnew(Flayer*, Rune *(*fn)(Flayer*, long, ulong*), int, void*);
       -int        flprepare(Flayer*);
       +void    flborder(Flayer*, int);
       +void    flclose(Flayer*);
       +void    fldelete(Flayer*, long, long);
       +void    flfp0p1(Flayer*, ulong*, ulong*);
       +void    flinit(Flayer*, Rectangle, XftFont*, unsigned long bg);
       +void    flinsert(Flayer*, Rune*, Rune*, long);
       +void    flnew(Flayer*, Rune *(*fn)(Flayer*, long, ulong*), int, void*);
       +int flprepare(Flayer*);
        Rectangle flrect(Flayer*, Rectangle);
       -void        flrefresh(Flayer*, Rectangle, int);
       -void        flreshape(Rectangle);
       -int        flselect(Flayer*);
       -void        flsetselect(Flayer*, long, long);
       -void        flstart(Rectangle);
       -void        flupfront(Flayer*);
       -Flayer        *flwhich(Point);
       +void    flrefresh(Flayer*, Rectangle, int);
       +void    flreshape(Rectangle);
       +int flselect(Flayer*);
       +void    flsetselect(Flayer*, long, long);
       +void    flstart(Rectangle);
       +void    flupfront(Flayer*);
       +Flayer  *flwhich(Point);
        
       -#define        FLMARGIN        4
       -#define        FLSCROLLWID        12
       -#define        FLGAP                4
       +#define FLMARGIN    4
       +#define FLSCROLLWID 12
       +#define FLGAP       4
 (DIR) diff --git a/samterm/icons.c b/samterm/icons.c
       @@ -4,17 +4,17 @@
        #include <libg.h>
        
        uchar darkgreybits[] = {
       -        0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
       -        0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
       -        0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
       -        0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
       +    0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
       +    0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
       +    0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
       +    0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
        };
        
       -Bitmap        *darkgrey;
       +Bitmap  *darkgrey;
        
        void
        iconinit(void)
        {
       -        darkgrey = balloc(Rect(0, 0, 16, 16), 0);
       -        wrbitmap(darkgrey, 0, 16, darkgreybits);
       +    darkgrey = balloc(Rect(0, 0, 16, 16), 0);
       +    wrbitmap(darkgrey, 0, 16, darkgreybits);
        }
 (DIR) diff --git a/samterm/io.c b/samterm/io.c
       @@ -6,143 +6,143 @@
        #include "flayer.h"
        #include "samterm.h"
        
       -int        cursorfd;
       -int        input;
       -int        got;
       -int        block;
       -Keystroke        keystroke;
       -int        reshaped;
       -uchar        *hostp;
       -uchar        *hoststop;
       -uchar        *externbase;
       -uchar        *externp;
       -uchar        *externstop;
       -void        panic(char*);
       +int cursorfd;
       +int input;
       +int got;
       +int block;
       +Keystroke   keystroke;
       +int reshaped;
       +uchar   *hostp;
       +uchar   *hoststop;
       +uchar   *externbase;
       +uchar   *externp;
       +uchar   *externstop;
       +void    panic(char*);
        
        void
        initio(void){
       -        einit(Emouse|Ekeyboard);
       -        estart(Ehost, 0, 0);
       -        extstart();
       +    einit(Emouse|Ekeyboard);
       +    estart(Ehost, 0, 0);
       +    extstart();
        }
        
        void
        frgetmouse(void)
        {
       -        mouse = emouse();
       +    mouse = emouse();
        }
        
        void
        mouseunblock(void)
        {
       -        got &= ~Emouse;
       +    got &= ~Emouse;
        }
        
        void
        kbdblock(void)
       -{                /* ca suffit */
       -        block = Ekeyboard|Eextern;
       +{       /* ca suffit */
       +    block = Ekeyboard|Eextern;
        }
        
        int
        button(int but)
        {
       -        frgetmouse();
       -        return mouse.buttons&(1<<(but-1));
       +    frgetmouse();
       +    return mouse.buttons&(1<<(but-1));
        }
        
        void
        externload(Event *e)
        {
       -        externbase = malloc(e->n);
       -        if(externbase == 0)
       -                return;
       -        memmove(externbase, e->data, e->n);
       -        externp = externbase;
       -        externstop = externbase + e->n;
       -        got |= Eextern;
       +    externbase = malloc(e->n);
       +    if(externbase == 0)
       +        return;
       +    memmove(externbase, e->data, e->n);
       +    externp = externbase;
       +    externstop = externbase + e->n;
       +    got |= Eextern;
        }
        
        int
        waitforio(void)
        {
       -        ulong type;
       -        static Event e;
       -
       -        if(got & ~block)
       -                return got & ~block;
       -        type = eread(~(got|block), &e);
       -        switch(type){
       -        case Ehost:
       -                hostp = e.data;
       -                hoststop = hostp + e.n;
       -                block = 0;
       -                break;
       -        case Eextern:
       -                externload(&e);
       -                break;
       -        case Ekeyboard:
       -                keystroke = e.keystroke;
       -                break;
       -        case Emouse:
       -                mouse = e.mouse;
       -                break;
       -        }
       -        got |= type;
       -        return got; 
       +    ulong type;
       +    static Event e;
       +
       +    if(got & ~block)
       +        return got & ~block;
       +    type = eread(~(got|block), &e);
       +    switch(type){
       +    case Ehost:
       +        hostp = e.data;
       +        hoststop = hostp + e.n;
       +        block = 0;
       +        break;
       +    case Eextern:
       +        externload(&e);
       +        break;
       +    case Ekeyboard:
       +        keystroke = e.keystroke;
       +        break;
       +    case Emouse:
       +        mouse = e.mouse;
       +        break;
       +    }
       +    got |= type;
       +    return got; 
        }
        
        int
        rcvchar(void)
        {
       -        int c;
       +    int c;
        
       -        if(!(got & Ehost))
       -                return -1;
       -        c = *hostp++;
       -        if(hostp == hoststop)
       -                got &= ~Ehost;
       -        return c;
       +    if(!(got & Ehost))
       +        return -1;
       +    c = *hostp++;
       +    if(hostp == hoststop)
       +        got &= ~Ehost;
       +    return c;
        }
        
        char*
        rcvstring(void)
        {
       -        *hoststop = 0;
       -        got &= ~Ehost;
       -        return (char*)hostp;
       +    *hoststop = 0;
       +    got &= ~Ehost;
       +    return (char*)hostp;
        }
        
        int
        getch(void)
        {
       -        int c;
       +    int c;
        
       -        while((c = rcvchar()) == -1){
       -                block = ~Ehost;
       -                waitforio();
       -                block = 0;
       -        }
       -        return c;
       +    while((c = rcvchar()) == -1){
       +        block = ~Ehost;
       +        waitforio();
       +        block = 0;
       +    }
       +    return c;
        }
        
        int
        externchar(void)
        {
       -        Rune r;
       +    Rune r;
        
            loop:
       -        if(got & (Eextern & ~block)){
       -                externp += chartorune(&r, (char*)externp);
       -                if(externp >= externstop){
       -                        got &= ~Eextern;
       -                        free(externbase);
       -                }
       -                if(r == 0)
       -                        goto loop;
       -                return r;
       -        }
       -        return -1;
       +    if(got & (Eextern & ~block)){
       +        externp += chartorune(&r, (char*)externp);
       +        if(externp >= externstop){
       +            got &= ~Eextern;
       +            free(externbase);
       +        }
       +        if(r == 0)
       +            goto loop;
       +        return r;
       +    }
       +    return -1;
        }
        
        Keystroke
       @@ -155,52 +155,52 @@ Keystroke
        kbdchar(void)
        {
            Keystroke k = {0};
       -        static Event e;
       +    static Event e;
        
       -        k.c = externchar();
       -        if(k.c > 0)
       -                return k;
       -        if(got & Ekeyboard){
       -                k = keystroke;
       +    k.c = externchar();
       +    if(k.c > 0)
       +        return k;
       +    if(got & Ekeyboard){
       +        k = keystroke;
                memset(&keystroke, 0, sizeof(keystroke));
       -                got &= ~Ekeyboard;
       -                return k;
       -        }
       -        while(ecanread(Eextern)){
       -                eread(Eextern, &e);
       -                externload(&e);
       -                k.c = externchar();
       -                if(k.c > 0)
       -                        return k;
       -        }
       -        if(!ecankbd()){
       +        got &= ~Ekeyboard;
       +        return k;
       +    }
       +    while(ecanread(Eextern)){
       +        eread(Eextern, &e);
       +        externload(&e);
       +        k.c = externchar();
       +        if(k.c > 0)
       +            return k;
       +    }
       +    if(!ecankbd()){
                k.c = -1;
       -                return k;
       +        return k;
            }
       -        return ekbd();
       +    return ekbd();
        }
        
        void
        ereshaped(Rectangle r)
        {
       -        USED(r);
       +    USED(r);
        
       -        reshaped = 1;
       +    reshaped = 1;
        }
        
        int
        RESHAPED(void)
        {
       -        if(reshaped){
       -                screen.r = bscreenrect(&screen.clipr);
       -                reshaped = 0;
       -                return 1;
       -        }
       -        return 0;
       +    if(reshaped){
       +        screen.r = bscreenrect(&screen.clipr);
       +        reshaped = 0;
       +        return 1;
       +    }
       +    return 0;
        }
        
        void
        mouseexit(void)
        {
       -        exits(0);
       +    exits(0);
        }
 (DIR) diff --git a/samterm/main.c b/samterm/main.c
       @@ -13,22 +13,22 @@
        extern unsigned long _bgpixel;
        extern void hmoveto(int, long, Flayer *);
        
       -Text        cmd;
       -Rune        *scratch;
       -long        nscralloc;
       -extern Bitmap        screen;
       +Text    cmd;
       +Rune    *scratch;
       +long    nscralloc;
       +extern Bitmap   screen;
        unsigned int cursor;
       -Mouse        mouse;
       -Flayer        *which = 0;
       +Mouse   mouse;
       +Flayer  *which = 0;
        Flayer  *flast = 0;
       -Flayer        *work = 0;
       -long        snarflen;
       -long        typestart = -1;
       -long        typeend = -1;
       -long        typeesc = -1;
       -long        modified = 0;                /* strange lookahead for menus */
       -char        lock = 1;
       -char        hasunlocked = 0;
       +Flayer  *work = 0;
       +long    snarflen;
       +long    typestart = -1;
       +long    typeend = -1;
       +long    typeesc = -1;
       +long    modified = 0;       /* strange lookahead for menus */
       +char    lock = 1;
       +char    hasunlocked = 0;
        int expandtabs = 0;
        char *machine = "localhost";
        int nofifo = 0;
       @@ -42,11 +42,11 @@ int oldcompat = 0;
        int
        main(int argc, char *argv[])
        {
       -        int i, got, scr, opt;
       -        Text *t;
       -        Rectangle r;
       -        Flayer *nwhich;
       -        int fwdbut;
       +    int i, got, scr, opt;
       +    Text *t;
       +    Rectangle r;
       +    Flayer *nwhich;
       +    int fwdbut;
        
            while ((opt = getopt(argc, argv, "efr:")) != -1){
                switch (opt){
       @@ -64,81 +64,81 @@ main(int argc, char *argv[])
                }
            }
        
       -        getscreen(argc, argv);
       -        fwdbut = scrollfwdbut();
       -        iconinit();
       -        initio();
       -        scratch = alloc(100*RUNESIZE);
       -        nscralloc = 100;
       -        r = screen.r;
       -        r.max.y = r.min.y+Dy(r)/5;
       -        flstart(screen.clipr);
       -        rinit(&cmd.rasp);
       -        flnew(&cmd.l[0], stgettext, 1, &cmd);
       +    getscreen(argc, argv);
       +    fwdbut = scrollfwdbut();
       +    iconinit();
       +    initio();
       +    scratch = alloc(100*RUNESIZE);
       +    nscralloc = 100;
       +    r = screen.r;
       +    r.max.y = r.min.y+Dy(r)/5;
       +    flstart(screen.clipr);
       +    rinit(&cmd.rasp);
       +    flnew(&cmd.l[0], stgettext, 1, &cmd);
            cmd.l[0].bg = getbg();
       -        flinit(&cmd.l[0], r, font, cmd.l[0].bg);
       -        cmd.nwin = 1;
       -        which = &cmd.l[0];
       -        cmd.tag = Untagged;
       -        outTs(Tversion, VERSION);
       -        startnewfile(Tstartcmdfile, &cmd);
       -
       -        got = 0;
       -        for(;;got = waitforio()){
       -                if(hasunlocked && RESHAPED())
       -                        reshape();
       -                if(got&RHost)
       -                        rcv();
       -                if(got&RExtern){
       -                        for(i=0; cmd.l[i].textfn==0; i++)
       -                                ;
       -                        current(&cmd.l[i]);
       -                        flsetselect(which, cmd.rasp.nrunes, cmd.rasp.nrunes);
       -                        type(which, RExtern);
       -                }
       -                if(got&RKeyboard)
       -                        if(which)
       -                                type(which, RKeyboard);
       -                        else
       -                                kbdblock();
       -                if(got&RMouse){
       -                        if(lock==2 || !ptinrect(mouse.xy, screen.r)){
       -                                mouseunblock();
       -                                continue;
       -                        }
       -                        nwhich = flwhich(mouse.xy);
       -                        scr = which && ptinrect(mouse.xy, which->scroll);
       -                        if(mouse.buttons)
       -                                flushtyping(1);
       +    flinit(&cmd.l[0], r, font, cmd.l[0].bg);
       +    cmd.nwin = 1;
       +    which = &cmd.l[0];
       +    cmd.tag = Untagged;
       +    outTs(Tversion, VERSION);
       +    startnewfile(Tstartcmdfile, &cmd);
       +
       +    got = 0;
       +    for(;;got = waitforio()){
       +        if(hasunlocked && RESHAPED())
       +            reshape();
       +        if(got&RHost)
       +            rcv();
       +        if(got&RExtern){
       +            for(i=0; cmd.l[i].textfn==0; i++)
       +                ;
       +            current(&cmd.l[i]);
       +            flsetselect(which, cmd.rasp.nrunes, cmd.rasp.nrunes);
       +            type(which, RExtern);
       +        }
       +        if(got&RKeyboard)
       +            if(which)
       +                type(which, RKeyboard);
       +            else
       +                kbdblock();
       +        if(got&RMouse){
       +            if(lock==2 || !ptinrect(mouse.xy, screen.r)){
       +                mouseunblock();
       +                continue;
       +            }
       +            nwhich = flwhich(mouse.xy);
       +            scr = which && ptinrect(mouse.xy, which->scroll);
       +            if(mouse.buttons)
       +                flushtyping(1);
                    if(mouse.buttons&1){
       -                                if(nwhich){
       -                                        if(nwhich!=which)
       -                                                current(nwhich);
       -                                        else if(scr)
       -                                                scroll(which, 1, fwdbut == 3 ? 1 : 3);
       -                                        else{
       -                                                t=(Text *)which->user1;
       -                                                if(flselect(which)){
       -                                                        outTsl(Tdclick, t->tag, which->p0);
       -                                                        t->lock++;
       -                                                }else if(t!=&cmd)
       -                                                        outcmd();
       -                                        }
       -                                }
       -                        }else if((mouse.buttons&2) && which){
       -                                if(scr)
       -                                        scroll(which, 2, 2);
       -                                else
       -                                        menu2hit();
       -                        }else if((mouse.buttons&4)){
       -                                if(scr)
       -                                        scroll(which, 3, fwdbut == 3 ? 3 : 1);
       -                                else
       -                                        menu3hit();
       -                        }
       -                        mouseunblock();
       -                }
       -        }
       +                if(nwhich){
       +                    if(nwhich!=which)
       +                        current(nwhich);
       +                    else if(scr)
       +                        scroll(which, 1, fwdbut == 3 ? 1 : 3);
       +                    else{
       +                        t=(Text *)which->user1;
       +                        if(flselect(which)){
       +                            outTsl(Tdclick, t->tag, which->p0);
       +                            t->lock++;
       +                        }else if(t!=&cmd)
       +                            outcmd();
       +                    }
       +                }
       +            }else if((mouse.buttons&2) && which){
       +                if(scr)
       +                    scroll(which, 2, 2);
       +                else
       +                    menu2hit();
       +            }else if((mouse.buttons&4)){
       +                if(scr)
       +                    scroll(which, 3, fwdbut == 3 ? 3 : 1);
       +                else
       +                    menu3hit();
       +            }
       +            mouseunblock();
       +        }
       +    }
        
            return EXIT_SUCCESS;
        }
       @@ -146,253 +146,253 @@ main(int argc, char *argv[])
        
        void
        reshape(void){
       -        int i;
       +    int i;
        
       -        flreshape(screen.clipr);
       -        for(i = 0; i<nname; i++)
       -                if(text[i])
       -                        hcheck(text[i]->tag);
       +    flreshape(screen.clipr);
       +    for(i = 0; i<nname; i++)
       +        if(text[i])
       +            hcheck(text[i]->tag);
        }
        
        void
        current(Flayer *nw)
        {
       -        Text *t;
       +    Text *t;
        
       -        if(which)
       -                flborder(which, 0);
       -        if(nw){
       -                flushtyping(1);
       -                flupfront(nw);
       -                flborder(nw, 1);
       -                buttons(Up);
       -                t = (Text *)nw->user1;
       -                t->front = nw-&t->l[0];
       -                if(t != &cmd)
       -                        work = nw;
       -        }
       -        which = nw;
       +    if(which)
       +        flborder(which, 0);
       +    if(nw){
       +        flushtyping(1);
       +        flupfront(nw);
       +        flborder(nw, 1);
       +        buttons(Up);
       +        t = (Text *)nw->user1;
       +        t->front = nw-&t->l[0];
       +        if(t != &cmd)
       +            work = nw;
       +    }
       +    which = nw;
        }
        
        void
        closeup(Flayer *l)
        {
       -        Text *t=(Text *)l->user1;
       -        int m;
       -
       -        m = whichmenu(t->tag);
       -        if(m < 0)
       -                return;
       -        flclose(l);
       -        if(l == which){
       -                which = 0;
       -                current(flwhich(Pt(0, 0)));
       -        }
       -        if(l == flast)
       -                flast = 0;
       -        if(l == work)
       -                work = 0;
       -        if(--t->nwin == 0){
       -                rclear(&t->rasp);
       -                free((uchar *)t);
       -                text[m] = 0;
       -        }else if(l == &t->l[t->front]){
       -                for(m=0; m<NL; m++)        /* find one; any one will do */
       -                        if(t->l[m].textfn){
       -                                t->front = m;
       -                                return;
       -                        }
       -                panic("close");
       -        }
       +    Text *t=(Text *)l->user1;
       +    int m;
       +
       +    m = whichmenu(t->tag);
       +    if(m < 0)
       +        return;
       +    flclose(l);
       +    if(l == which){
       +        which = 0;
       +        current(flwhich(Pt(0, 0)));
       +    }
       +    if(l == flast)
       +        flast = 0;
       +    if(l == work)
       +        work = 0;
       +    if(--t->nwin == 0){
       +        rclear(&t->rasp);
       +        free((uchar *)t);
       +        text[m] = 0;
       +    }else if(l == &t->l[t->front]){
       +        for(m=0; m<NL; m++) /* find one; any one will do */
       +            if(t->l[m].textfn){
       +                t->front = m;
       +                return;
       +            }
       +        panic("close");
       +    }
        }
        
        Flayer *
        findl(Text *t)
        {
       -        int i;
       -        for(i = 0; i<NL; i++)
       -                if(t->l[i].textfn==0)
       -                        return &t->l[i];
       -        return 0;
       +    int i;
       +    for(i = 0; i<NL; i++)
       +        if(t->l[i].textfn==0)
       +            return &t->l[i];
       +    return 0;
        }
        
        void
        duplicate(Flayer *l, Rectangle r, XftFont *f, int close)
        {
       -        Text *t=(Text *)l->user1;
       -        Flayer *nl = findl(t);
       -        Rune *rp;
       -        ulong n;
       -
       -        if(nl){
       -                flnew(nl, stgettext, l->user0, (char *)t);
       -                flinit(nl, r, f, l->bg);
       -                nl->origin = l->origin;
       -                rp = (*l->textfn)(l, l->f.nchars, &n);
       -                flinsert(nl, rp, rp+n, l->origin);
       -                flsetselect(nl, l->p0, l->p1);
       -                if(close){
       -                        flclose(l);
       -                        if(l==which)
       -                                which = 0;
       -                }else
       -                        t->nwin++;
       -                current(nl);
       -                hcheck(t->tag);
       -        }
       -        cursorswitch(cursor);
       +    Text *t=(Text *)l->user1;
       +    Flayer *nl = findl(t);
       +    Rune *rp;
       +    ulong n;
       +
       +    if(nl){
       +        flnew(nl, stgettext, l->user0, (char *)t);
       +        flinit(nl, r, f, l->bg);
       +        nl->origin = l->origin;
       +        rp = (*l->textfn)(l, l->f.nchars, &n);
       +        flinsert(nl, rp, rp+n, l->origin);
       +        flsetselect(nl, l->p0, l->p1);
       +        if(close){
       +            flclose(l);
       +            if(l==which)
       +                which = 0;
       +        }else
       +            t->nwin++;
       +        current(nl);
       +        hcheck(t->tag);
       +    }
       +    cursorswitch(cursor);
        }
        
        void
        buttons(int updown)
        {
       -        while(((mouse.buttons&7)!=0) != updown)
       -                frgetmouse();
       +    while(((mouse.buttons&7)!=0) != updown)
       +        frgetmouse();
        }
        
        int
        getr(Rectangle *rp)
        {
       -        Point p;
       -        Rectangle r;
       -
       -        *rp = getrect(3, &mouse);
       -        if(rp->max.x && rp->max.x-rp->min.x<=5 && rp->max.y-rp->min.y<=5){
       -                p = rp->min;
       -                r = cmd.l[cmd.front].entire;
       -                *rp = screen.r;
       -                if(cmd.nwin==1){
       -                        if (p.y <= r.min.y)
       -                                rp->max.y = r.min.y;
       -                        else if (p.y >= r.max.y)
       -                                rp->min.y = r.max.y;
       -                        if (p.x <= r.min.x)
       -                                rp->max.x = r.min.x;
       -                        else if (p.x >= r.max.x)
       -                                rp->min.x = r.max.x;
       -                }
       -        }
       -        return rectclip(rp, screen.r) &&
       -           rp->max.x-rp->min.x>100 && rp->max.y-rp->min.y>40;
       +    Point p;
       +    Rectangle r;
       +
       +    *rp = getrect(3, &mouse);
       +    if(rp->max.x && rp->max.x-rp->min.x<=5 && rp->max.y-rp->min.y<=5){
       +        p = rp->min;
       +        r = cmd.l[cmd.front].entire;
       +        *rp = screen.r;
       +        if(cmd.nwin==1){
       +            if (p.y <= r.min.y)
       +                rp->max.y = r.min.y;
       +            else if (p.y >= r.max.y)
       +                rp->min.y = r.max.y;
       +            if (p.x <= r.min.x)
       +                rp->max.x = r.min.x;
       +            else if (p.x >= r.max.x)
       +                rp->min.x = r.max.x;
       +        }
       +    }
       +    return rectclip(rp, screen.r) &&
       +       rp->max.x-rp->min.x>100 && rp->max.y-rp->min.y>40;
        }
        
        void
        snarf(Text *t, int w)
        {
       -        Flayer *l = &t->l[w];
       +    Flayer *l = &t->l[w];
        
       -        if(l->p1>l->p0){
       -                snarflen = l->p1-l->p0;
       -                outTsll(Tsnarf, t->tag, l->p0, l->p1);
       -        }
       +    if(l->p1>l->p0){
       +        snarflen = l->p1-l->p0;
       +        outTsll(Tsnarf, t->tag, l->p0, l->p1);
       +    }
        }
        
        void
        cut(Text *t, int w, int save, int check)
        {
       -        long p0, p1;
       -        Flayer *l;
       -
       -        l = &t->l[w];
       -        p0 = l->p0;
       -        p1 = l->p1;
       -        if(p0 == p1)
       -                return;
       -        if(p0 < 0)
       -                panic("cut");
       -        if(save)
       -                snarf(t, w);
       -        outTsll(Tcut, t->tag, p0, p1);
       -        flsetselect(l, p0, p0);
       -        t->lock++;
       -        hcut(t->tag, p0, p1-p0);
       -        if(check)
       -                hcheck(t->tag);
       +    long p0, p1;
       +    Flayer *l;
       +
       +    l = &t->l[w];
       +    p0 = l->p0;
       +    p1 = l->p1;
       +    if(p0 == p1)
       +        return;
       +    if(p0 < 0)
       +        panic("cut");
       +    if(save)
       +        snarf(t, w);
       +    outTsll(Tcut, t->tag, p0, p1);
       +    flsetselect(l, p0, p0);
       +    t->lock++;
       +    hcut(t->tag, p0, p1-p0);
       +    if(check)
       +        hcheck(t->tag);
        }
        
        void
        paste(Text *t, int w)
        {
       -        if(snarflen){
       -                cut(t, w, 0, 0);
       -                t->lock++;
       -                outTsl(Tpaste, t->tag, t->l[w].p0);
       -        }
       +    if(snarflen){
       +        cut(t, w, 0, 0);
       +        t->lock++;
       +        outTsl(Tpaste, t->tag, t->l[w].p0);
       +    }
        }
        
        void
        scrorigin(Flayer *l, int but, long p0)
        {
       -        Text *t=(Text *)l->user1;
       +    Text *t=(Text *)l->user1;
        
       -        switch(but){
       -        case 1:
       +    switch(but){
       +    case 1:
                if (oldcompat)
       -                    outTsll(Torigin, t->tag, l->origin, p0);
       +            outTsll(Torigin, t->tag, l->origin, p0);
                else
       -                    outTslll(Torigin, t->tag, l->origin, p0, getlayer(l, t));
       -                break;
       -        case 2:
       +            outTslll(Torigin, t->tag, l->origin, p0, getlayer(l, t));
       +        break;
       +    case 2:
                if (oldcompat)
       -                    outTsll(Torigin, t->tag, p0, 1L);
       +            outTsll(Torigin, t->tag, p0, 1L);
                else
       -                    outTslll(Torigin, t->tag, p0, 1L, getlayer(l, t));
       -                break;
       -        case 3:
       -                horigin(t->tag, p0, NULL);
       -        }
       +            outTslll(Torigin, t->tag, p0, 1L, getlayer(l, t));
       +        break;
       +    case 3:
       +        horigin(t->tag, p0, NULL);
       +    }
        }
        
        int
        alnum(int c)
        {
       -        /*
       -         * Hard to get absolutely right.  Use what we know about ASCII
       -         * and assume anything above the Latin control characters is
       -         * potentially an alphanumeric.
       -         */
       -        if(c<=' ')
       -                return 0;
       -        if(0x7F<=c && c<=0xA0)
       -                return 0;
       -        if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
       -                return 0;
       -        return 1;
       +    /*
       +     * Hard to get absolutely right.  Use what we know about ASCII
       +     * and assume anything above the Latin control characters is
       +     * potentially an alphanumeric.
       +     */
       +    if(c<=' ')
       +        return 0;
       +    if(0x7F<=c && c<=0xA0)
       +        return 0;
       +    if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
       +        return 0;
       +    return 1;
        }
        
        int
        raspc(Rasp *r, long p)
        {
       -        ulong n;
       -        rload(r, p, p+1, &n);
       -        if(n)
       -                return scratch[0];
       -        return 0;
       +    ulong n;
       +    rload(r, p, p+1, &n);
       +    if(n)
       +        return scratch[0];
       +    return 0;
        }
        
        long
        ctlw(Rasp *r, long o, long p)
        {
       -        int c;
       +    int c;
        
       -        if(--p < o)
       -                return o;
       -        if(raspc(r, p)=='\n')
       -                return p;
       -        for(; p>=o && !alnum(c=raspc(r, p)); --p)
       -                if(c=='\n')
       -                        return p+1;
       -        for(; p>o && alnum(raspc(r, p-1)); --p)
       -                ;
       -        return p>=o? p : o;
       +    if(--p < o)
       +        return o;
       +    if(raspc(r, p)=='\n')
       +        return p;
       +    for(; p>=o && !alnum(c=raspc(r, p)); --p)
       +        if(c=='\n')
       +            return p+1;
       +    for(; p>o && alnum(raspc(r, p-1)); --p)
       +        ;
       +    return p>=o? p : o;
        }
        
        long
        ctlu(Rasp *r, long o, long p)
        {
       -        for(; p-1>=o && raspc(r, p-1)!='\n'; --p)
       -                ;
       -        return p>=o? p : o;
       +    for(; p-1>=o && raspc(r, p-1)!='\n'; --p)
       +        ;
       +    return p>=o? p : o;
        }
        
        int
       @@ -415,25 +415,25 @@ center(Flayer *l, long a)
        int
        onethird(Flayer *l, long a)
        {
       -        Text *t;
       -        Rectangle s;
       -        long lines;
       -
       -        t = l->user1;
       -        if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
       -                if(a > t->rasp.nrunes)
       -                        a = t->rasp.nrunes;
       -                s = inset(l->scroll, 1);
       -                lines = ((s.max.y-s.min.y)/l->f.fheight+1)/3;
       -                if (lines < 2)
       -                        lines = 2;
       +    Text *t;
       +    Rectangle s;
       +    long lines;
       +
       +    t = l->user1;
       +    if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
       +        if(a > t->rasp.nrunes)
       +            a = t->rasp.nrunes;
       +        s = inset(l->scroll, 1);
       +        lines = ((s.max.y-s.min.y)/l->f.fheight+1)/3;
       +        if (lines < 2)
       +            lines = 2;
                if (oldcompat)
       -                    outTsll(Torigin, t->tag, a, lines);
       +            outTsll(Torigin, t->tag, a, lines);
                else
       -                    outTslll(Torigin, t->tag, a, lines, getlayer(l, t));
       -                return 1;
       -        }
       -        return 0;
       +            outTslll(Torigin, t->tag, a, lines, getlayer(l, t));
       +        return 1;
       +    }
       +    return 0;
        }
        
        
       @@ -445,28 +445,28 @@ extern Display * _dpy;
        void
        flushtyping(int clearesc)
        {
       -        Text *t;
       -        ulong n;
       -
       -        if(clearesc)
       -                typeesc = -1;        
       -        if(typestart == typeend) {
       -                modified = 0;
       -                return;
       -        }
       -        t = which->user1;
       -        if(t != &cmd)
       -                modified = 1;
       -        rload(&t->rasp, typestart, typeend, &n);
       -        scratch[n] = 0;
       -        if(t==&cmd && typeend==t->rasp.nrunes && scratch[typeend-typestart-1]=='\n'){
       -                setlock();
       -                outcmd();
       -        }
       -        outTslS(Ttype, t->tag, typestart, scratch);
       -        typestart = -1;
       -        typeend = -1;
       -        XFlush(_dpy);
       +    Text *t;
       +    ulong n;
       +
       +    if(clearesc)
       +        typeesc = -1;   
       +    if(typestart == typeend) {
       +        modified = 0;
       +        return;
       +    }
       +    t = which->user1;
       +    if(t != &cmd)
       +        modified = 1;
       +    rload(&t->rasp, typestart, typeend, &n);
       +    scratch[n] = 0;
       +    if(t==&cmd && typeend==t->rasp.nrunes && scratch[typeend-typestart-1]=='\n'){
       +        setlock();
       +        outcmd();
       +    }
       +    outTslS(Ttype, t->tag, typestart, scratch);
       +    typestart = -1;
       +    typeend = -1;
       +    XFlush(_dpy);
        }
        
        static long
       @@ -678,7 +678,7 @@ cmddelword(Flayer *l, long a, Text *t)
            l->p1 = a;
            if (l->p1 != l->p0){
                if(typestart<=l->p0 && l->p1<=typeend){
       -            t->lock++;        /* to call hcut */
       +            t->lock++;  /* to call hcut */
                    hcut(t->tag, l->p0, l->p1-l->p0);
                    /* hcheck is local because we know rasp is contiguous */
                    hcheck(t->tag);
       @@ -700,7 +700,7 @@ cmddelbol(Flayer *l, long a, Text *t)
            l->p1 = a;
            if (l->p1 != l->p0){
                if(typestart<=l->p0 && l->p1<=typeend){
       -            t->lock++;        /* to call hcut */
       +            t->lock++;  /* to call hcut */
                    hcut(t->tag, l->p0, l->p1-l->p0);
                    /* hcheck is local because we know rasp is contiguous */
                    hcheck(t->tag);
       @@ -722,7 +722,7 @@ cmddel(Flayer *l, long a, Text *t)
            l->p1 = a;
            if (l->p1 != l->p0){
                if(typestart<=l->p0 && l->p1<=typeend){
       -            t->lock++;        /* to call hcut */
       +            t->lock++;  /* to call hcut */
                    hcut(t->tag, l->p0, l->p1-l->p0);
                    /* hcheck is local because we know rasp is contiguous */
                    hcheck(t->tag);
       @@ -842,29 +842,29 @@ CommandEntry commands[Cmax] ={
        };
        
        void
       -type(Flayer *l, int res)        /* what a bloody mess this is -- but it's getting better! */
       +type(Flayer *l, int res)    /* what a bloody mess this is -- but it's getting better! */
        {
       -        Text *t = (Text *)l->user1;
       -        Rune buf[100];
       +    Text *t = (Text *)l->user1;
       +    Rune buf[100];
            Keystroke k = {0};
       -        Rune *p = buf;
       -        int backspacing, moving;
       -        long a;
       +    Rune *p = buf;
       +    int backspacing, moving;
       +    long a;
        
       -        if(lock || t->lock){
       -                kbdblock();
       -                return;
       -        }
       +    if(lock || t->lock){
       +        kbdblock();
       +        return;
       +    }
        
            k = qpeekc();
       -        a = l->p0;
       +    a = l->p0;
            if (a != l->p1 && k.k != Kcommand){
       -                flushtyping(1);
       -                cut(t, t->front, 1, 1);
       -                return; /* it may now be locked */
       -        }
       +        flushtyping(1);
       +        cut(t, t->front, 1, 1);
       +        return; /* it may now be locked */
       +    }
        
       -        while (((k = kbdchar()), k.c) > 0) {
       +    while (((k = kbdchar()), k.c) > 0) {
                if (k.k == Kcommand)
                    break;
        
       @@ -880,10 +880,10 @@ type(Flayer *l, int res)        /* what a bloody mess this is -- but it's getting bette
                    break;
                }
        
       -                *p++ = k.c;
       -                if (k.c == '\n' || p >= buf + sizeof(buf) / sizeof(buf[0]))
       -                        break;
       -        }
       +        *p++ = k.c;
       +        if (k.c == '\n' || p >= buf + sizeof(buf) / sizeof(buf[0]))
       +            break;
       +    }
        
            if (k.k == Kcommand){
                if (k.c < 0 || k.c >= Cmax || commands[k.c].f == NULL)
       @@ -909,7 +909,7 @@ type(Flayer *l, int res)        /* what a bloody mess this is -- but it's getting bette
                    typeesc = a;
        
                hgrow(t->tag, a, p-buf, 0);
       -        t->lock++;        /* pretend we Trequest'ed for hdatarune*/
       +        t->lock++;  /* pretend we Trequest'ed for hdatarune*/
                hdatarune(t->tag, a, buf, p-buf);
                a += p-buf;
                l->p0 = a;
       @@ -938,42 +938,42 @@ type(Flayer *l, int res)        /* what a bloody mess this is -- but it's getting bette
        void
        outcmd(void)
        {
       -        if(work)
       -                outTsll(Tworkfile, ((Text *)work->user1)->tag, work->p0, work->p1);
       +    if(work)
       +        outTsll(Tworkfile, ((Text *)work->user1)->tag, work->p0, work->p1);
        }
        
        void
        panic(char *s)
        {
       -        fprintf(stderr, "samterm:panic: ");
       -        perror(s);
       -        abort();
       +    fprintf(stderr, "samterm:panic: ");
       +    perror(s);
       +    abort();
        }
        
        Rune*
        stgettext(Flayer *l, long n, ulong *np)
        {
       -        Text *t;
       +    Text *t;
        
       -        t = l->user1;
       -        rload(&t->rasp, l->origin, l->origin+n, np);
       -        return scratch;
       +    t = l->user1;
       +    rload(&t->rasp, l->origin, l->origin+n, np);
       +    return scratch;
        }
        
        long
        scrtotal(Flayer *l)
        {
       -        return ((Text *)l->user1)->rasp.nrunes;
       +    return ((Text *)l->user1)->rasp.nrunes;
        }
        
        void*
        alloc(ulong n)
        {
       -        void *p;
       +    void *p;
        
       -        p = malloc(n);
       -        if(p == 0)
       -                panic("alloc");
       -        memset(p, 0, n);
       -        return p;
       +    p = malloc(n);
       +    if(p == 0)
       +        panic("alloc");
       +    memset(p, 0, n);
       +    return p;
        }
 (DIR) diff --git a/samterm/menu.c b/samterm/menu.c
       @@ -6,377 +6,377 @@
        #include "flayer.h"
        #include "samterm.h"
        
       -uchar        *name[MAXFILES];        /* first byte is ' ' or '\'': modified state */
       -Text        *text[MAXFILES];        /* pointer to Text associated with file */
       -ushort        tag[MAXFILES];                /* text[i].tag, even if text[i] not defined */
       -int        nname;
       -int        mw;
       +uchar   *name[MAXFILES];    /* first byte is ' ' or '\'': modified state */
       +Text    *text[MAXFILES];    /* pointer to Text associated with file */
       +ushort  tag[MAXFILES];      /* text[i].tag, even if text[i] not defined */
       +int nname;
       +int mw;
        
       -char        *genmenu3(int);
       -char        *genmenu2(int);
       -char        *genmenu2c(int);
       +char    *genmenu3(int);
       +char    *genmenu2(int);
       +char    *genmenu2c(int);
        
        extern unsigned long _bgpixel;
        
        enum Menu2
        {
       -        Cut,
       -        Paste,
       -        Snarf,
       -        Look,
       -        Exch,
       -        Search,
       -        NMENU2 = Search,
       -        Send = Search,
       -        NMENU2C
       +    Cut,
       +    Paste,
       +    Snarf,
       +    Look,
       +    Exch,
       +    Search,
       +    NMENU2 = Search,
       +    Send = Search,
       +    NMENU2C
        };
        
        enum Menu3
        {
       -        New,
       -        Zerox,
       -        Reshape,
       -        Close,
       -        Write,
       -        NMENU3
       +    New,
       +    Zerox,
       +    Reshape,
       +    Close,
       +    Write,
       +    NMENU3
        };
        
       -char        *menu2str[] = {
       -        "cut",
       -        "paste",
       -        "snarf",
       -        "look",
       -        "<exch>",
       -        0,                /* storage for last pattern */
       +char    *menu2str[] = {
       +    "cut",
       +    "paste",
       +    "snarf",
       +    "look",
       +    "<exch>",
       +    0,      /* storage for last pattern */
        };
        
       -char        *menu3str[] = {
       -        "new",
       -        "zerox",
       -        "reshape",
       -        "close",
       -        "write",
       +char    *menu3str[] = {
       +    "new",
       +    "zerox",
       +    "reshape",
       +    "close",
       +    "write",
        };
        
       -Menu        menu2 =        {0, genmenu2};
       -Menu        menu2c ={0, genmenu2c};
       -Menu        menu3 =        {0, genmenu3};
       +Menu    menu2 = {0, genmenu2};
       +Menu    menu2c ={0, genmenu2c};
       +Menu    menu3 = {0, genmenu3};
        
        void
        menu2hit(void)
        {
       -        Text *t=(Text *)which->user1;
       -        int w = which-t->l;
       -        int m;
       -
       -        m = menuhit(2, &mouse, t==&cmd? &menu2c : &menu2);
       -        if(lock || t->lock)
       -                return;
       -
       -        switch(m){
       -        case Cut:
       -                cut(t, w, 1, 1);
       -                break;
       -
       -        case Paste:
       -                paste(t, w);
       -                break;
       -
       -        case Snarf:
       -                snarf(t, w);
       -                break;
       -
       -        case Exch:
       -                snarf(t, w);
       -                outT0(Tstartsnarf);
       -                setlock();
       -                break;
       -
       -        case Look:
       -                outTsll(Tlook, t->tag, which->p0, which->p1);
       -                setlock();
       -                break;
       -
       -        case Search:
       -                outcmd();
       -                if(t==&cmd)
       -                        outTsll(Tsend, 0 /*ignored*/, which->p0, which->p1);
       -                else
       -                        outT0(Tsearch);
       -                setlock();
       -                break;
       -        }
       +    Text *t=(Text *)which->user1;
       +    int w = which-t->l;
       +    int m;
       +
       +    m = menuhit(2, &mouse, t==&cmd? &menu2c : &menu2);
       +    if(lock || t->lock)
       +        return;
       +
       +    switch(m){
       +    case Cut:
       +        cut(t, w, 1, 1);
       +        break;
       +
       +    case Paste:
       +        paste(t, w);
       +        break;
       +
       +    case Snarf:
       +        snarf(t, w);
       +        break;
       +
       +    case Exch:
       +        snarf(t, w);
       +        outT0(Tstartsnarf);
       +        setlock();
       +        break;
       +
       +    case Look:
       +        outTsll(Tlook, t->tag, which->p0, which->p1);
       +        setlock();
       +        break;
       +
       +    case Search:
       +        outcmd();
       +        if(t==&cmd)
       +            outTsll(Tsend, 0 /*ignored*/, which->p0, which->p1);
       +        else
       +            outT0(Tsearch);
       +        setlock();
       +        break;
       +    }
        }
        
        void
        menu3hit(void)
        {
       -        Rectangle r;
       -        Flayer *l;
       -        int m, i;
       -        Text *t;
       -
       -        mw = -1;
       -        switch(m = menuhit(3, &mouse, &menu3)){
       -        case -1:
       -                break;
       -
       -        case New:
       -                if(!lock)
       -                        sweeptext(1, 0);
       -                break;
       -
       -        case Zerox:
       -        case Reshape:
       -                if(!lock){
       -                        cursorswitch(BullseyeCursor);
       -                        buttons(Down);
       -                        if((mouse.buttons&4) && (l = flwhich(mouse.xy)) && getr(&r))
       -                                duplicate(l, r, l->f.font, m==Reshape);
       -                        else
       -                                cursorswitch(cursor);
       -                        buttons(Up);
       -                }
       -                break;
       -
       -        case Close:
       -                if(!lock){
       -                        cursorswitch(BullseyeCursor);
       -                        buttons(Down);
       -                        if((mouse.buttons&4) && (l = flwhich(mouse.xy)) && !lock){
       -                                t=(Text *)l->user1;
       -                                if (t->nwin>1)
       -                                        closeup(l);
       -                                else if(t!=&cmd) {
       -                                        outTs(Tclose, t->tag);
       -                                        setlock();
       -                                }
       -                        }
       -                        cursorswitch(cursor);
       -                        buttons(Up);
       -                }
       -                break;
       -
       -        case Write:
       -                if(!lock){
       -                        cursorswitch(BullseyeCursor);
       -                        buttons(Down);
       -                        if((mouse.buttons&4) && (l = flwhich(mouse.xy))){
       -                                outTs(Twrite, ((Text *)l->user1)->tag);
       -                                setlock();
       -                        }else
       -                                cursorswitch(cursor);
       -                        buttons(Up);
       -                }
       -                break;
       -
       -        default:
       -                if(t = text[m-NMENU3]){
       -                        i = t->front;
       -                        if(t->nwin==0 || t->l[i].textfn==0)
       -                                return;        /* not ready yet; try again later */
       -                        if(t->nwin>1 && which==&t->l[i])
       -                                do
       -                                        if(++i==NL)
       -                                                i = 0;
       -                                while(i!=t->front && t->l[i].textfn==0);
       -                        current(&t->l[i]);
       -                }else if(!lock)
       -                        sweeptext(0, tag[m-NMENU3]);
       -                break;
       -        }
       +    Rectangle r;
       +    Flayer *l;
       +    int m, i;
       +    Text *t;
       +
       +    mw = -1;
       +    switch(m = menuhit(3, &mouse, &menu3)){
       +    case -1:
       +        break;
       +
       +    case New:
       +        if(!lock)
       +            sweeptext(1, 0);
       +        break;
       +
       +    case Zerox:
       +    case Reshape:
       +        if(!lock){
       +            cursorswitch(BullseyeCursor);
       +            buttons(Down);
       +            if((mouse.buttons&4) && (l = flwhich(mouse.xy)) && getr(&r))
       +                duplicate(l, r, l->f.font, m==Reshape);
       +            else
       +                cursorswitch(cursor);
       +            buttons(Up);
       +        }
       +        break;
       +
       +    case Close:
       +        if(!lock){
       +            cursorswitch(BullseyeCursor);
       +            buttons(Down);
       +            if((mouse.buttons&4) && (l = flwhich(mouse.xy)) && !lock){
       +                t=(Text *)l->user1;
       +                if (t->nwin>1)
       +                    closeup(l);
       +                else if(t!=&cmd) {
       +                    outTs(Tclose, t->tag);
       +                    setlock();
       +                }
       +            }
       +            cursorswitch(cursor);
       +            buttons(Up);
       +        }
       +        break;
       +
       +    case Write:
       +        if(!lock){
       +            cursorswitch(BullseyeCursor);
       +            buttons(Down);
       +            if((mouse.buttons&4) && (l = flwhich(mouse.xy))){
       +                outTs(Twrite, ((Text *)l->user1)->tag);
       +                setlock();
       +            }else
       +                cursorswitch(cursor);
       +            buttons(Up);
       +        }
       +        break;
       +
       +    default:
       +        if(t = text[m-NMENU3]){
       +            i = t->front;
       +            if(t->nwin==0 || t->l[i].textfn==0)
       +                return; /* not ready yet; try again later */
       +            if(t->nwin>1 && which==&t->l[i])
       +                do
       +                    if(++i==NL)
       +                        i = 0;
       +                while(i!=t->front && t->l[i].textfn==0);
       +            current(&t->l[i]);
       +        }else if(!lock)
       +            sweeptext(0, tag[m-NMENU3]);
       +        break;
       +    }
        }
        
        
        Text *
        sweeptext(int new, int tag)
        {
       -        Rectangle r;
       -        Text *t;
       -
       -        if(getr(&r) && (t = malloc(sizeof(Text)))){
       -                memset((void*)t, 0, sizeof(Text));
       -                current((Flayer *)0);
       -                flnew(&t->l[0], stgettext, 0, (char *)t);
       -                flinit(&t->l[0], r, font, getbg());        /*bnl*/
       -                t->nwin = 1;
       -                rinit(&t->rasp);
       -                if(new)
       -                        startnewfile(Tstartnewfile, t);
       -                else{
       -                        rinit(&t->rasp);
       -                        t->tag = tag;
       -                        startfile(t);
       -                }
       -                return t;
       -        }
       -        return 0;
       +    Rectangle r;
       +    Text *t;
       +
       +    if(getr(&r) && (t = malloc(sizeof(Text)))){
       +        memset((void*)t, 0, sizeof(Text));
       +        current((Flayer *)0);
       +        flnew(&t->l[0], stgettext, 0, (char *)t);
       +        flinit(&t->l[0], r, font, getbg()); /*bnl*/
       +        t->nwin = 1;
       +        rinit(&t->rasp);
       +        if(new)
       +            startnewfile(Tstartnewfile, t);
       +        else{
       +            rinit(&t->rasp);
       +            t->tag = tag;
       +            startfile(t);
       +        }
       +        return t;
       +    }
       +    return 0;
        }
        
        int
        whichmenu(int tg)
        {
       -        int i;
       +    int i;
        
       -        for(i=0; i<nname; i++)
       -                if(tag[i] == tg)
       -                        return i;
       -        return -1;
       +    for(i=0; i<nname; i++)
       +        if(tag[i] == tg)
       +            return i;
       +    return -1;
        }
        
        void
        menuins(int n, uchar *s, Text *t, int m, int tg)
        {
       -        int i;
       -
       -        if(nname == MAXFILES)
       -                panic("menuins");
       -        for(i=nname; i>n; --i)
       -                name[i]=name[i-1], text[i]=text[i-1], tag[i]=tag[i-1];
       -        text[n] = t;
       -        tag[n] = tg;
       -        name[n] = alloc(strlen((char*)s)+2);
       -        name[n][0] = m;
       -        strcpy((char*)name[n]+1, (char*)s);
       -        nname++;
       -        menu3.lasthit = n+NMENU3;
       +    int i;
       +
       +    if(nname == MAXFILES)
       +        panic("menuins");
       +    for(i=nname; i>n; --i)
       +        name[i]=name[i-1], text[i]=text[i-1], tag[i]=tag[i-1];
       +    text[n] = t;
       +    tag[n] = tg;
       +    name[n] = alloc(strlen((char*)s)+2);
       +    name[n][0] = m;
       +    strcpy((char*)name[n]+1, (char*)s);
       +    nname++;
       +    menu3.lasthit = n+NMENU3;
        }
        
        void
        menudel(int n)
        {
       -        int i;
       -
       -        if(nname==0 || n>=nname || text[n])
       -                panic("menudel");
       -        free(name[n]);
       -        --nname;
       -        for(i = n; i<nname; i++)
       -                name[i]=name[i+1], text[i]=text[i+1], tag[i]=tag[i+1];
       +    int i;
       +
       +    if(nname==0 || n>=nname || text[n])
       +        panic("menudel");
       +    free(name[n]);
       +    --nname;
       +    for(i = n; i<nname; i++)
       +        name[i]=name[i+1], text[i]=text[i+1], tag[i]=tag[i+1];
        }
        
        void
        setpat(char *s)
        {
       -        static char pat[17];
       +    static char pat[17];
        
       -        pat[0] = '/';
       -        strncpy(pat+1, s, 15);
       -        menu2str[Search] = pat;
       +    pat[0] = '/';
       +    strncpy(pat+1, s, 15);
       +    menu2str[Search] = pat;
        }
        
       -#define        NBUF        64
       +#define NBUF    64
        static uchar buf[NBUF*UTFmax]={' ', ' ', ' ', ' '};
        
        char *
        paren(char *s)
        {
       -        uchar *t = buf;
       +    uchar *t = buf;
        
       -        *t++ = '(';
       -        do; while(*t++ = *s++);
       -        t[-1] = ')';
       -        *t = 0;
       -        return (char *)buf;
       +    *t++ = '(';
       +    do; while(*t++ = *s++);
       +    t[-1] = ')';
       +    *t = 0;
       +    return (char *)buf;
        }
        char*
        genmenu2(int n)
        {
       -        Text *t=(Text *)which->user1;
       -        char *p;
       -        if(n>=NMENU2+(menu2str[Search]!=0))
       -                return 0;
       -        p = menu2str[n];
       -        if(!lock && !t->lock || n==Search || n==Look)
       -                return p;
       -        return paren(p);
       +    Text *t=(Text *)which->user1;
       +    char *p;
       +    if(n>=NMENU2+(menu2str[Search]!=0))
       +        return 0;
       +    p = menu2str[n];
       +    if(!lock && !t->lock || n==Search || n==Look)
       +        return p;
       +    return paren(p);
        }
        char*
        genmenu2c(int n)
        {
       -        Text *t=(Text *)which->user1;
       -        char *p;
       -        if(n >= NMENU2C)
       -                return 0;
       -        if(n == Send)
       -                p="send";
       -        else
       -                p = menu2str[n];
       -        if(!lock && !t->lock)
       -                return p;
       -        return paren(p);
       +    Text *t=(Text *)which->user1;
       +    char *p;
       +    if(n >= NMENU2C)
       +        return 0;
       +    if(n == Send)
       +        p="send";
       +    else
       +        p = menu2str[n];
       +    if(!lock && !t->lock)
       +        return p;
       +    return paren(p);
        }
        char *
        genmenu3(int n)
        {
       -        Text *t;
       -        int c, i, k, l, w;
       -        Rune r;
       -        char *p;
       -
       -        if(n >= NMENU3+nname)
       -                return 0;
       -        if(n < NMENU3){
       -                p = menu3str[n];
       -                if(lock)
       -                        p = paren(p);
       -                return p;
       -        }
       -        n -= NMENU3;
       -        if(n == 0)        /* unless we've been fooled, this is cmd */
       -                return (char *)&name[n][1];
       -        if(mw == -1){
       -                mw = 7;        /* strlen("~~sam~~"); */
       -                for(i=1; i<nname; i++){
       -                        w = utflen((char*)name[i]+1)+4;        /* include "'+. " */
       -                        if(w > mw)
       -                                mw = w;
       -                }
       -        }
       -        if(mw > NBUF)
       -                mw = NBUF;
       -        t = text[n];
       -        buf[0] = name[n][0];
       -        buf[1] = '-';
       -        buf[2] = ' ';
       -        buf[3] = ' ';
       -        if(t){
       -                if(t->nwin == 1)
       -                        buf[1] = '+';
       -                else if(t->nwin > 1)
       -                        buf[1] = '*';
       -                if(work && t==(Text *)work->user1) {
       -                        buf[2]= '.';
       -                        if(modified)
       -                                buf[0] = '\'';
       -                }
       -        }
       -        l = utflen((char*)name[n]+1);
       -        if(l > NBUF-4-2){
       -                i = 4;
       -                k = 1;
       -                while(i < NBUF/2){
       -                        k += chartorune(&r, (char*)name[n]+k);
       -                        i++;
       -                }
       -                c = name[n][k];
       -                name[n][k] = 0;
       -                strcpy((char*)buf+4, (char*)name[n]+1);
       -                name[n][k] = c;
       -                strcat((char*)buf, "...");
       -                while((l-i) >= NBUF/2-4){
       -                        k += chartorune(&r, (char*)name[n]+k);
       -                        i++;
       -                }
       -                strcat((char*)buf, (char*)name[n]+k);
       -        }else
       -                strcpy((char*)buf+4, (char*)name[n]+1);
       -        i = utflen((char*)buf);
       -        k = strlen((char*)buf);
       -        while(i<mw && k<sizeof buf-1){
       -                buf[k++] = ' ';
       -                i++;
       -        }
       -        buf[k] = 0;
       -        return (char *)buf;
       +    Text *t;
       +    int c, i, k, l, w;
       +    Rune r;
       +    char *p;
       +
       +    if(n >= NMENU3+nname)
       +        return 0;
       +    if(n < NMENU3){
       +        p = menu3str[n];
       +        if(lock)
       +            p = paren(p);
       +        return p;
       +    }
       +    n -= NMENU3;
       +    if(n == 0)  /* unless we've been fooled, this is cmd */
       +        return (char *)&name[n][1];
       +    if(mw == -1){
       +        mw = 7; /* strlen("~~sam~~"); */
       +        for(i=1; i<nname; i++){
       +            w = utflen((char*)name[i]+1)+4; /* include "'+. " */
       +            if(w > mw)
       +                mw = w;
       +        }
       +    }
       +    if(mw > NBUF)
       +        mw = NBUF;
       +    t = text[n];
       +    buf[0] = name[n][0];
       +    buf[1] = '-';
       +    buf[2] = ' ';
       +    buf[3] = ' ';
       +    if(t){
       +        if(t->nwin == 1)
       +            buf[1] = '+';
       +        else if(t->nwin > 1)
       +            buf[1] = '*';
       +        if(work && t==(Text *)work->user1) {
       +            buf[2]= '.';
       +            if(modified)
       +                buf[0] = '\'';
       +        }
       +    }
       +    l = utflen((char*)name[n]+1);
       +    if(l > NBUF-4-2){
       +        i = 4;
       +        k = 1;
       +        while(i < NBUF/2){
       +            k += chartorune(&r, (char*)name[n]+k);
       +            i++;
       +        }
       +        c = name[n][k];
       +        name[n][k] = 0;
       +        strcpy((char*)buf+4, (char*)name[n]+1);
       +        name[n][k] = c;
       +        strcat((char*)buf, "...");
       +        while((l-i) >= NBUF/2-4){
       +            k += chartorune(&r, (char*)name[n]+k);
       +            i++;
       +        }
       +        strcat((char*)buf, (char*)name[n]+k);
       +    }else
       +        strcpy((char*)buf+4, (char*)name[n]+1);
       +    i = utflen((char*)buf);
       +    k = strlen((char*)buf);
       +    while(i<mw && k<sizeof buf-1){
       +        buf[k++] = ' ';
       +        i++;
       +    }
       +    buf[k] = 0;
       +    return (char *)buf;
        }
 (DIR) diff --git a/samterm/mesg.c b/samterm/mesg.c
       @@ -10,294 +10,294 @@
        extern char *exname;
        extern Flayer *flast;
        
       -#define        HSIZE        3        /* Type + short count */
       -Header        h;
       -uchar        indata[DATASIZE+1];        /* room for NUL */
       -uchar        outdata[DATASIZE];
       -short        outcount;
       -int        hversion;
       -
       -void        inmesg(Hmesg, int);
       -int        inshort(int);
       -long        inlong(int);
       -long        invlong(int);
       -void        hsetdot(int, long, long);
       -void        hmoveto(int, long, Flayer *);
       -void        hsetsnarf(int);
       -void        clrlock(void);
       -int        snarfswap(char*, int, char**);
       +#define HSIZE   3   /* Type + short count */
       +Header  h;
       +uchar   indata[DATASIZE+1]; /* room for NUL */
       +uchar   outdata[DATASIZE];
       +short   outcount;
       +int hversion;
       +
       +void    inmesg(Hmesg, int);
       +int inshort(int);
       +long    inlong(int);
       +long    invlong(int);
       +void    hsetdot(int, long, long);
       +void    hmoveto(int, long, Flayer *);
       +void    hsetsnarf(int);
       +void    clrlock(void);
       +int snarfswap(char*, int, char**);
        
        void
        rcv(void)
        {
       -        int c;
       -        static int state = 0;
       -        static int count = 0;
       -        static int i = 0;
       -        static int errs = 0;
       -
       -        while((c=rcvchar()) != -1)
       -                switch(state){
       -                case 0:
       -                        h.type = c;
       -                        state++;
       -                        break;
       -
       -                case 1:
       -                        h.count0 = c;
       -                        state++;
       -                        break;
       -
       -                case 2:
       -                        h.count1 = c;
       -                        count = h.count0|(h.count1<<8);
       -                        i = 0;
       -                        if(count > DATASIZE){
       -                                if(++errs < 5){
       -                                        dumperrmsg(count, h.type, h.count0, c);
       -                                        state = 0;
       -                                        continue;
       -                                }
       -                                fprintf(stderr, "type %d count %d\n", h.type, count);
       -                                panic("count>DATASIZE");
       -                        }
       -                        if(count == 0)
       -                                goto zerocount;
       -                        state++;
       -                        break;
       -
       -                case 3:
       -                        indata[i++] = c;
       -                        if(i == count){
       -                zerocount:
       -                                indata[i] = 0;
       -                                inmesg(h.type, count);
       -                                state = count = 0;
       -                                continue;
       -                        }
       -                        break;
       -                }
       +    int c;
       +    static int state = 0;
       +    static int count = 0;
       +    static int i = 0;
       +    static int errs = 0;
       +
       +    while((c=rcvchar()) != -1)
       +        switch(state){
       +        case 0:
       +            h.type = c;
       +            state++;
       +            break;
       +
       +        case 1:
       +            h.count0 = c;
       +            state++;
       +            break;
       +
       +        case 2:
       +            h.count1 = c;
       +            count = h.count0|(h.count1<<8);
       +            i = 0;
       +            if(count > DATASIZE){
       +                if(++errs < 5){
       +                    dumperrmsg(count, h.type, h.count0, c);
       +                    state = 0;
       +                    continue;
       +                }
       +                fprintf(stderr, "type %d count %d\n", h.type, count);
       +                panic("count>DATASIZE");
       +            }
       +            if(count == 0)
       +                goto zerocount;
       +            state++;
       +            break;
       +
       +        case 3:
       +            indata[i++] = c;
       +            if(i == count){
       +        zerocount:
       +                indata[i] = 0;
       +                inmesg(h.type, count);
       +                state = count = 0;
       +                continue;
       +            }
       +            break;
       +        }
        }
        
        Text *
        whichtext(int tg)
        {
       -        int i;
       +    int i;
        
       -        for(i=0; i<nname; i++)
       -                if(tag[i] == tg)
       -                        return text[i];
       -        panic("whichtext");
       -        return 0;
       +    for(i=0; i<nname; i++)
       +        if(tag[i] == tg)
       +            return text[i];
       +    panic("whichtext");
       +    return 0;
        }
        
        void
        inmesg(Hmesg type, int count)
        {
       -        Text *t;
       -        int i, m;
       -        long l, l2;
       -        Flayer *lp;
       -
       -        m = inshort(0);
       -        l = inlong(2);
       -        switch(type){
       -        case -1:
       -                panic("rcv error");
       -        default:
       -                fprintf(stderr, "type %d\n", type);
       -                panic("rcv unknown");
       -
       -        case Hversion:
       -                hversion = m;
       +    Text *t;
       +    int i, m;
       +    long l, l2;
       +    Flayer *lp;
       +
       +    m = inshort(0);
       +    l = inlong(2);
       +    switch(type){
       +    case -1:
       +        panic("rcv error");
       +    default:
       +        fprintf(stderr, "type %d\n", type);
       +        panic("rcv unknown");
       +
       +    case Hversion:
       +        hversion = m;
                if (hversion != VERSION)
                    panic("host-terminal version mismatch");
       -                break;
       -
       -        case Hbindname:
       -                l = invlong(2);                /* for 64-bit pointers */
       -                if((i=whichmenu(m)) < 0)
       -                        break;
       -                /* in case of a race, a bindname may already have occurred */
       -                if((t=whichtext(m)) == 0)
       -                        t=(Text *)l;
       -                else        /* let the old one win; clean up the new one */
       -                        while(((Text *)l)->nwin>0)
       -                                closeup(&((Text *)l)->l[((Text *)l)->front]);
       -                text[i] = t;
       -                text[i]->tag = m;
       -                break;
       -
       -        case Hcurrent:
       -                if(whichmenu(m)<0)
       -                        break;
       -                t = whichtext(m);
       -                i = which && ((Text *)which->user1)==&cmd && m!=cmd.tag;
       -                if(t==0 && (t = sweeptext(0, m))==0)
       -                        break;
       -                if(t->l[t->front].textfn==0)
       -                        panic("Hcurrent");
       -                lp = &t->l[t->front];
       -                if(i){
       -                        flupfront(lp);
       -                        flborder(lp, 0);
       -                        work = lp;
       +        break;
       +
       +    case Hbindname:
       +        l = invlong(2);     /* for 64-bit pointers */
       +        if((i=whichmenu(m)) < 0)
       +            break;
       +        /* in case of a race, a bindname may already have occurred */
       +        if((t=whichtext(m)) == 0)
       +            t=(Text *)l;
       +        else    /* let the old one win; clean up the new one */
       +            while(((Text *)l)->nwin>0)
       +                closeup(&((Text *)l)->l[((Text *)l)->front]);
       +        text[i] = t;
       +        text[i]->tag = m;
       +        break;
       +
       +    case Hcurrent:
       +        if(whichmenu(m)<0)
       +            break;
       +        t = whichtext(m);
       +        i = which && ((Text *)which->user1)==&cmd && m!=cmd.tag;
       +        if(t==0 && (t = sweeptext(0, m))==0)
       +            break;
       +        if(t->l[t->front].textfn==0)
       +            panic("Hcurrent");
       +        lp = &t->l[t->front];
       +        if(i){
       +            flupfront(lp);
       +            flborder(lp, 0);
       +            work = lp;
                    flast = lp;
       -                }else
       -                        current(lp);
       -                break;
       -
       -        case Hmovname:
       -                if((m=whichmenu(m)) < 0)
       -                        break;
       -                t = text[m];
       -                l = tag[m];
       -                i = name[m][0];
       -                text[m] = 0;        /* suppress panic in menudel */
       -                menudel(m);
       -                if(t == &cmd)
       -                        m = 0;
       -                else{
       -                        if (nname>0 && text[0]==&cmd)
       -                                m = 1;
       -                        else m = 0;
       -                        for(; m<nname; m++)
       -                                if(strcmp((char*)indata+2, (char*)name[m]+1)<0)
       -                                        break;
       -                }
       -                menuins(m, indata+2, t, i, (int)l);
       -                break;
       -
       -        case Hgrow:
       -                if(whichmenu(m) >= 0)
       -                        hgrow(m, l, inlong(6), 1);
       -                break;
       -
       -        case Hnewname:
       -                menuins(0, (uchar *)"", (Text *)0, ' ', m);
       -                break;
       -
       -        case Hcheck0:
       -                i = whichmenu(m);
       -                if(i>=0) {
       -                        t = text[i];
       -                        if (t)
       -                                t->lock++;
       -                        outTs(Tcheck, m);
       -                }
       -                break;
       -
       -        case Hcheck:
       -                i = whichmenu(m);
       -                if(i>=0) {
       -                        t = text[i];
       -                        if (t && t->lock)
       -                                t->lock--;
       -                        hcheck(m);
       -                }
       -                break;
       -
       -        case Hunlock:
       -                clrlock();
       -                break;
       -
       -        case Hdata:
       -                if(whichmenu(m) >= 0)
       -                        l += hdata(m, l, indata+6, count-6);
       -        Checkscroll:
       -                if(m == cmd.tag){
       -                        for(i=0; i<NL; i++){
       -                                lp = &cmd.l[i];
       -                                if(lp->textfn)
       -                                        center(lp, l>=0? l : lp->p1);
       -                        }
       -                }
       -                break;
       -
       -        case Horigin:
       -                if(whichmenu(m) >= 0){
       +        }else
       +            current(lp);
       +        break;
       +
       +    case Hmovname:
       +        if((m=whichmenu(m)) < 0)
       +            break;
       +        t = text[m];
       +        l = tag[m];
       +        i = name[m][0];
       +        text[m] = 0;    /* suppress panic in menudel */
       +        menudel(m);
       +        if(t == &cmd)
       +            m = 0;
       +        else{
       +            if (nname>0 && text[0]==&cmd)
       +                m = 1;
       +            else m = 0;
       +            for(; m<nname; m++)
       +                if(strcmp((char*)indata+2, (char*)name[m]+1)<0)
       +                    break;
       +        }
       +        menuins(m, indata+2, t, i, (int)l);
       +        break;
       +
       +    case Hgrow:
       +        if(whichmenu(m) >= 0)
       +            hgrow(m, l, inlong(6), 1);
       +        break;
       +
       +    case Hnewname:
       +        menuins(0, (uchar *)"", (Text *)0, ' ', m);
       +        break;
       +
       +    case Hcheck0:
       +        i = whichmenu(m);
       +        if(i>=0) {
       +            t = text[i];
       +            if (t)
       +                t->lock++;
       +            outTs(Tcheck, m);
       +        }
       +        break;
       +
       +    case Hcheck:
       +        i = whichmenu(m);
       +        if(i>=0) {
       +            t = text[i];
       +            if (t && t->lock)
       +                t->lock--;
       +            hcheck(m);
       +        }
       +        break;
       +
       +    case Hunlock:
       +        clrlock();
       +        break;
       +
       +    case Hdata:
       +        if(whichmenu(m) >= 0)
       +            l += hdata(m, l, indata+6, count-6);
       +    Checkscroll:
       +        if(m == cmd.tag){
       +            for(i=0; i<NL; i++){
       +                lp = &cmd.l[i];
       +                if(lp->textfn)
       +                    center(lp, l>=0? l : lp->p1);
       +            }
       +        }
       +        break;
       +
       +    case Horigin:
       +        if(whichmenu(m) >= 0){
                    if (oldcompat)
                        horigin(m, l, NULL);
                    else{
                        Text *t = whichtext(m);
                        l2 = inlong(6);
       -                            horigin(m, l, &t->l[l2]);
       +                horigin(m, l, &t->l[l2]);
                    }
                }
       -                break;
       -
       -        case Hunlockfile:
       -                if(whichmenu(m)>=0 && (t = whichtext(m))->lock){
       -                        --t->lock;
       -                        l = -1;
       -                        goto Checkscroll;
       -                }
       -                break;
       -
       -        case Hsetdot:
       -                if(whichmenu(m) >= 0)
       -                        hsetdot(m, l, inlong(6));
       -                break;
       -
       -        case Hgrowdata:
       -                if(whichmenu(m)<0)
       -                        break;
       -                hgrow(m, l, inlong(6), 0);
       -                whichtext(m)->lock++;        /* fake the request */
       -                l += hdata(m, l, indata+10, count-10);
       -                goto Checkscroll;
       -
       -        case Hmoveto:
       -                if(whichmenu(m)>=0)
       -                        hmoveto(m, l, NULL);
       -                break;
       -
       -        case Hclean:
       -                if((m = whichmenu(m)) >= 0)
       -                        name[m][0] = ' ';
       -                break;
       -
       -        case Hdirty:
       -                if((m = whichmenu(m))>=0)
       -                        name[m][0] = '\'';
       -                break;
       -
       -        case Hdelname:
       -                if((m=whichmenu(m)) >= 0)
       -                        menudel(m);
       -                break;
       -
       -        case Hcut:
       -                if(whichmenu(m) >= 0)
       -                        hcut(m, l, inlong(6));
       -                break;
       -
       -        case Hclose:
       -                if(whichmenu(m)<0 || (t = whichtext(m))==0)
       -                        break;
       -                l = t->nwin;
       -                for(i = 0,lp = t->l; l>0 && i<NL; i++,lp++)
       -                        if(lp->textfn){
       -                                closeup(lp);
       -                                --l;
       -                        }
       -                break;
       -
       -        case Hsetpat:
       -                setpat((char *)indata);
       -                break;
       -
       -        case Hsetsnarf:
       -                hsetsnarf(m);
       -                break;
       -
       -        case Hsnarflen:
       -                snarflen = inlong(0);
       -                break;
       -
       -        case Hack:
       -                outT0(Tack);
       -                break;
       +        break;
       +
       +    case Hunlockfile:
       +        if(whichmenu(m)>=0 && (t = whichtext(m))->lock){
       +            --t->lock;
       +            l = -1;
       +            goto Checkscroll;
       +        }
       +        break;
       +
       +    case Hsetdot:
       +        if(whichmenu(m) >= 0)
       +            hsetdot(m, l, inlong(6));
       +        break;
       +
       +    case Hgrowdata:
       +        if(whichmenu(m)<0)
       +            break;
       +        hgrow(m, l, inlong(6), 0);
       +        whichtext(m)->lock++;   /* fake the request */
       +        l += hdata(m, l, indata+10, count-10);
       +        goto Checkscroll;
       +
       +    case Hmoveto:
       +        if(whichmenu(m)>=0)
       +            hmoveto(m, l, NULL);
       +        break;
       +
       +    case Hclean:
       +        if((m = whichmenu(m)) >= 0)
       +            name[m][0] = ' ';
       +        break;
       +
       +    case Hdirty:
       +        if((m = whichmenu(m))>=0)
       +            name[m][0] = '\'';
       +        break;
       +
       +    case Hdelname:
       +        if((m=whichmenu(m)) >= 0)
       +            menudel(m);
       +        break;
       +
       +    case Hcut:
       +        if(whichmenu(m) >= 0)
       +            hcut(m, l, inlong(6));
       +        break;
       +
       +    case Hclose:
       +        if(whichmenu(m)<0 || (t = whichtext(m))==0)
       +            break;
       +        l = t->nwin;
       +        for(i = 0,lp = t->l; l>0 && i<NL; i++,lp++)
       +            if(lp->textfn){
       +                closeup(lp);
       +                --l;
       +            }
       +        break;
       +
       +    case Hsetpat:
       +        setpat((char *)indata);
       +        break;
       +
       +    case Hsetsnarf:
       +        hsetsnarf(m);
       +        break;
       +
       +    case Hsnarflen:
       +        snarflen = inlong(0);
       +        break;
       +
       +    case Hack:
       +        outT0(Tack);
       +        break;
        
        #ifndef NOFIFO
            case Hextcmd:
       @@ -313,275 +313,275 @@ inmesg(Hmesg type, int count)
                break;
        #endif
        
       -        case Hexit:
       -                outT0(Texit);
       -                mouseexit();
       -                break;
       -        }
       +    case Hexit:
       +        outT0(Texit);
       +        mouseexit();
       +        break;
       +    }
        }
        
        void
        setlock(void)
        {
       -        lock++;
       -        cursorswitch(cursor = LockCursor);
       +    lock++;
       +    cursorswitch(cursor = LockCursor);
        }
        
        void
        clrlock(void)
        {
       -        hasunlocked = 1;
       -        if(lock > 0)
       -                lock--;
       -        if(lock == 0)
       -                cursorswitch(cursor=DefaultCursor);
       +    hasunlocked = 1;
       +    if(lock > 0)
       +        lock--;
       +    if(lock == 0)
       +        cursorswitch(cursor=DefaultCursor);
        }
        
        void
        startfile(Text *t)
        {
       -        outTsv(Tstartfile, t->tag, t);                /* for 64-bit pointers */
       -        setlock();
       +    outTsv(Tstartfile, t->tag, t);      /* for 64-bit pointers */
       +    setlock();
        }
        
        void
        startnewfile(int type, Text *t)
        {
       -        t->tag = Untagged;
       -        outTv(type, t);                                /* for 64-bit pointers */
       +    t->tag = Untagged;
       +    outTv(type, t);             /* for 64-bit pointers */
        }
        
        int
        inshort(int n)
        {
       -        return indata[n]|(indata[n+1]<<8);
       +    return indata[n]|(indata[n+1]<<8);
        }
        
        long
        inlong(int n)
        {
       -        return indata[n]|(indata[n+1]<<8)|
       -                ((long)indata[n+2]<<16)|((long)indata[n+3]<<24);
       +    return indata[n]|(indata[n+1]<<8)|
       +        ((long)indata[n+2]<<16)|((long)indata[n+3]<<24);
        }
        
        long
        invlong(int n)
        {
       -        long l;
       +    long l;
        
       -        l = (indata[n+7]<<24) | (indata[n+6]<<16) | (indata[n+5]<<8) | indata[n+4];
       -        l = (l<<16) | (indata[n+3]<<8) | indata[n+2];
       -        l = (l<<16) | (indata[n+1]<<8) | indata[n];
       -        return l;
       +    l = (indata[n+7]<<24) | (indata[n+6]<<16) | (indata[n+5]<<8) | indata[n+4];
       +    l = (l<<16) | (indata[n+3]<<8) | indata[n+2];
       +    l = (l<<16) | (indata[n+1]<<8) | indata[n];
       +    return l;
        }
        
        void
        outT0(Tmesg type)
        {
       -        outstart(type);
       -        outsend();
       +    outstart(type);
       +    outsend();
        }
        
        void
        outTl(Tmesg type, long l)
        {
       -        outstart(type);
       -        outlong(l);
       -        outsend();
       +    outstart(type);
       +    outlong(l);
       +    outsend();
        }
        
        void
        outTs(Tmesg type, int s)
        {
       -        outstart(type);
       -        outshort(s);
       -        outsend();
       +    outstart(type);
       +    outshort(s);
       +    outsend();
        }
        
        void
        outTss(Tmesg type, int s1, int s2)
        {
       -        outstart(type);
       -        outshort(s1);
       -        outshort(s2);
       -        outsend();
       +    outstart(type);
       +    outshort(s1);
       +    outshort(s2);
       +    outsend();
        }
        
        void
        outTslll(Tmesg type, int s1, long l1, long l2, long l3)
        {
       -        outstart(type);
       -        outshort(s1);
       -        outlong(l1);
       -        outlong(l2);
       -        outlong(l3);
       -        outsend();
       +    outstart(type);
       +    outshort(s1);
       +    outlong(l1);
       +    outlong(l2);
       +    outlong(l3);
       +    outsend();
        }
        
        void
        outTsll(Tmesg type, int s1, long l1, long l2)
        {
       -        outstart(type);
       -        outshort(s1);
       -        outlong(l1);
       -        outlong(l2);
       -        outsend();
       +    outstart(type);
       +    outshort(s1);
       +    outlong(l1);
       +    outlong(l2);
       +    outsend();
        }
        
        void
        outTsl(Tmesg type, int s1, long l1)
        {
       -        outstart(type);
       -        outshort(s1);
       -        outlong(l1);
       -        outsend();
       +    outstart(type);
       +    outshort(s1);
       +    outlong(l1);
       +    outsend();
        }
        
        void
        outTsv(Tmesg type, int s1, void *l1)
        {
       -        outstart(type);
       -        outshort(s1);
       -        outvlong(l1);
       -        outsend();
       +    outstart(type);
       +    outshort(s1);
       +    outvlong(l1);
       +    outsend();
        }
        
        void
        outTv(Tmesg type, void *l1)
        {
       -        outstart(type);
       -        outvlong(l1);
       -        outsend();
       +    outstart(type);
       +    outvlong(l1);
       +    outsend();
        }
        
        void
        outTslS(Tmesg type, int s1, long l1, Rune *s)
        {
       -        char buf[DATASIZE*3+1];
       -        char *c;
       +    char buf[DATASIZE*3+1];
       +    char *c;
        
       -        outstart(type);
       -        outshort(s1);
       -        outlong(l1);
       -        c = buf;
       -        while(*s)
       -                c += runetochar(c, s++);
       -        *c++ = 0;
       -        outcopy(c-buf, (uchar *)buf);
       -        outsend();
       +    outstart(type);
       +    outshort(s1);
       +    outlong(l1);
       +    c = buf;
       +    while(*s)
       +        c += runetochar(c, s++);
       +    *c++ = 0;
       +    outcopy(c-buf, (uchar *)buf);
       +    outsend();
        }
        
        void
        outTsls(Tmesg type, int s1, long l1, int s2)
        {
       -        outstart(type);
       -        outshort(s1);
       -        outlong(l1);
       -        outshort(s2);
       -        outsend();
       +    outstart(type);
       +    outshort(s1);
       +    outlong(l1);
       +    outshort(s2);
       +    outsend();
        }
        
        void
        outstart(Tmesg type)
        {
       -        outdata[0] = type;
       -        outcount = 0;
       +    outdata[0] = type;
       +    outcount = 0;
        }
        
        void
        outcopy(int count, uchar *data)
        {
       -        while(count--)
       -                outdata[HSIZE+outcount++] = *data++;        
       +    while(count--)
       +        outdata[HSIZE+outcount++] = *data++;    
        }
        
        void
        outshort(int s)
        {
       -        uchar buf[2];
       +    uchar buf[2];
        
       -        buf[0]=s;
       -        buf[1]=s>>8;
       -        outcopy(2, buf);
       +    buf[0]=s;
       +    buf[1]=s>>8;
       +    outcopy(2, buf);
        }
        
        void
        outlong(long l)
        {
       -        uchar buf[4];
       +    uchar buf[4];
        
       -        buf[0]=l;
       -        buf[1]=l>>8;
       -        buf[2]=l>>16;
       -        buf[3]=l>>24;
       -        outcopy(4, buf);
       +    buf[0]=l;
       +    buf[1]=l>>8;
       +    buf[2]=l>>16;
       +    buf[3]=l>>24;
       +    outcopy(4, buf);
        }
        
        void
        outvlong(void *v)
        {
       -        int i;
       -        uintptr_t l;
       -        uchar buf[8];
       +    int i;
       +    uintptr_t l;
       +    uchar buf[8];
        
       -        l = (uintptr_t)v;
       -        for(i = 0; i < sizeof(buf); i++, l >>= 8)
       -                buf[i] = l;
       +    l = (uintptr_t)v;
       +    for(i = 0; i < sizeof(buf); i++, l >>= 8)
       +        buf[i] = l;
        
       -        outcopy(8, buf);
       +    outcopy(8, buf);
        }
        
        void
        outsend(void)
        {
       -        if(outcount>DATASIZE-HSIZE)
       -                panic("outcount>sizeof outdata");
       -        outdata[1]=outcount;
       -        outdata[2]=outcount>>8;
       -        if(write(1, (char *)outdata, outcount+HSIZE)!=outcount+HSIZE)
       -                exits("write error");
       +    if(outcount>DATASIZE-HSIZE)
       +        panic("outcount>sizeof outdata");
       +    outdata[1]=outcount;
       +    outdata[2]=outcount>>8;
       +    if(write(1, (char *)outdata, outcount+HSIZE)!=outcount+HSIZE)
       +        exits("write error");
        }
        
        
        void
        hsetdot(int m, long p0, long p1)
        {
       -        Text *t = whichtext(m);
       -        Flayer *l = &t->l[t->front];
       +    Text *t = whichtext(m);
       +    Flayer *l = &t->l[t->front];
        
       -        flushtyping(1);
       -        flsetselect(l, p0, p1);
       +    flushtyping(1);
       +    flsetselect(l, p0, p1);
        }
        
        void
        horigin(int m, long p0, Flayer *l)
        {
       -        Text *t = whichtext(m);
       -        l = l ? l : &t->l[t->front];
       -        long a;
       -        ulong n;
       -        Rune *r;
       +    Text *t = whichtext(m);
       +    l = l ? l : &t->l[t->front];
       +    long a;
       +    ulong n;
       +    Rune *r;
        
            if (getlayer(l, t) < 0)
                return; /* the user managed to close the layer during the round trip with the host */
        
       -        if(!flprepare(l)){
       -                l->origin = p0;
       -                return;
       -        }
       -        a = p0-l->origin;
       -        if(a>=0 && a<l->f.nchars)
       -                frdelete(&l->f, 0, a);
       -        else if(a<0 && -a<l->f.nchars){
       -                r = rload(&t->rasp, p0, l->origin, &n);
       -                frinsert(&l->f, r, r+n, 0);
       -        }else
       -                frdelete(&l->f, 0, l->f.nchars);
       -        l->origin = p0;
       -        scrdraw(l, t->rasp.nrunes);
       -        if(l->visible==Some)
       -                flrefresh(l, l->entire, 0);
       -        hcheck(m);
       +    if(!flprepare(l)){
       +        l->origin = p0;
       +        return;
       +    }
       +    a = p0-l->origin;
       +    if(a>=0 && a<l->f.nchars)
       +        frdelete(&l->f, 0, a);
       +    else if(a<0 && -a<l->f.nchars){
       +        r = rload(&t->rasp, p0, l->origin, &n);
       +        frinsert(&l->f, r, r+n, 0);
       +    }else
       +        frdelete(&l->f, 0, l->f.nchars);
       +    l->origin = p0;
       +    scrdraw(l, t->rasp.nrunes);
       +    if(l->visible==Some)
       +        flrefresh(l, l->entire, 0);
       +    hcheck(m);
        }
        
        void
       @@ -601,220 +601,220 @@ hmoveto(int m, long p0, Flayer *l)
        void
        hcheck(int m)
        {
       -        Flayer *l;
       -        Text *t;
       -        int reqd = 0, i;
       -        long n, nl, a;
       -        Rune *r;
       -
       -        if(m == Untagged)
       -                return;
       -        t = whichtext(m);
       -        if(t == 0)                /* possible in a half-built window */
       -                return;
       -        for(l = &t->l[0], i = 0; i<NL; i++, l++){
       -                if(l->textfn==0 || !flprepare(l))        /* BUG: don't
       -                                                           need this if BUG below
       -                                                           is fixed */
       -                        continue;
       -                a = t->l[i].origin;
       -                n = rcontig(&t->rasp, a, a+l->f.nchars, 1);
       -                if(n<l->f.nchars)        /* text missing in middle of screen */
       -                        a+=n;
       -                else{                        /* text missing at end of screen? */
       +    Flayer *l;
       +    Text *t;
       +    int reqd = 0, i;
       +    long n, nl, a;
       +    Rune *r;
       +
       +    if(m == Untagged)
       +        return;
       +    t = whichtext(m);
       +    if(t == 0)      /* possible in a half-built window */
       +        return;
       +    for(l = &t->l[0], i = 0; i<NL; i++, l++){
       +        if(l->textfn==0 || !flprepare(l))   /* BUG: don't
       +                               need this if BUG below
       +                               is fixed */
       +            continue;
       +        a = t->l[i].origin;
       +        n = rcontig(&t->rasp, a, a+l->f.nchars, 1);
       +        if(n<l->f.nchars)   /* text missing in middle of screen */
       +            a+=n;
       +        else{           /* text missing at end of screen? */
                Again:
       -                         if(l->f.lastlinefull)
       -                                goto Checksel;        /* all's well */
       -                        a = t->l[i].origin+l->f.nchars;
       -                        n = t->rasp.nrunes-a;
       -                        if(n==0)
       -                                goto Checksel;
       -                        if(n>TBLOCKSIZE)
       -                                n = TBLOCKSIZE;
       -                        n = rcontig(&t->rasp, a, a+n, 1);
       -                        if(n>0){
       -                                rload(&t->rasp, a, a+n, 0);
       -                                nl = l->f.nchars;
       -                                r = scratch;
       -                                flinsert(l, r, r+n, l->origin+nl);
       -                                if(nl == l->f.nchars)        /* made no progress */
       -                                        goto Checksel;
       -                                goto Again;
       -                        }
       -                }
       -                if(!reqd){
       -                        n = rcontig(&t->rasp, a, a+TBLOCKSIZE, 0);
       -                        if(n <= 0)
       -                                panic("hcheck request==0");
       -                        outTsls(Trequest, m, a, (int)n);
       -                        outTs(Tcheck, m);
       -                        t->lock++;        /* for the Trequest */
       -                        t->lock++;        /* for the Tcheck */
       -                        reqd++;
       -                }
       -            Checksel:
       -                flsetselect(l, l->p0, l->p1);
       -        }
       +            if(l->f.lastlinefull)
       +                goto Checksel;  /* all's well */
       +            a = t->l[i].origin+l->f.nchars;
       +            n = t->rasp.nrunes-a;
       +            if(n==0)
       +                goto Checksel;
       +            if(n>TBLOCKSIZE)
       +                n = TBLOCKSIZE;
       +            n = rcontig(&t->rasp, a, a+n, 1);
       +            if(n>0){
       +                rload(&t->rasp, a, a+n, 0);
       +                nl = l->f.nchars;
       +                r = scratch;
       +                flinsert(l, r, r+n, l->origin+nl);
       +                if(nl == l->f.nchars)   /* made no progress */
       +                    goto Checksel;
       +                goto Again;
       +            }
       +        }
       +        if(!reqd){
       +            n = rcontig(&t->rasp, a, a+TBLOCKSIZE, 0);
       +            if(n <= 0)
       +                panic("hcheck request==0");
       +            outTsls(Trequest, m, a, (int)n);
       +            outTs(Tcheck, m);
       +            t->lock++;  /* for the Trequest */
       +            t->lock++;  /* for the Tcheck */
       +            reqd++;
       +        }
       +        Checksel:
       +        flsetselect(l, l->p0, l->p1);
       +    }
        }
        
        void
        flnewlyvisible(Flayer *l)
        {
       -        hcheck(((Text *)l->user1)->tag);
       +    hcheck(((Text *)l->user1)->tag);
        }
        
        void
        hsetsnarf(int nc)
        {
       -        char *s2;
       -        char *s1;
       -        int i;
       -        int n;
       -
       -        cursorswitch(DeadCursor);
       -        s2 = alloc(nc+1);
       -        for(i=0; i<nc; i++)
       -                s2[i] = getch();
       -        s2[nc] = 0;
       -        n = snarfswap(s2, nc, &s1);
       -        if(n >= 0){
       -                if(!s1)
       -                        n = 0;
       -                if(n > SNARFSIZE-1)
       -                        n = SNARFSIZE-1;
       -                s1 = realloc(s1, n+1);
       -                if (!s1)
       -                        exits("malloc");
       -                s1[n] = 0;
       -                snarflen = n;
       -                outTs(Tsetsnarf, n);
       -                if(n>0 && write(1, s1, n)!=n)
       -                        exits("write error");
       -                free(s1);
       -        }else
       -                outTs(Tsetsnarf, 0);
       -        free(s2);
       -        cursorswitch(cursor);
       +    char *s2;
       +    char *s1;
       +    int i;
       +    int n;
       +
       +    cursorswitch(DeadCursor);
       +    s2 = alloc(nc+1);
       +    for(i=0; i<nc; i++)
       +        s2[i] = getch();
       +    s2[nc] = 0;
       +    n = snarfswap(s2, nc, &s1);
       +    if(n >= 0){
       +        if(!s1)
       +            n = 0;
       +        if(n > SNARFSIZE-1)
       +            n = SNARFSIZE-1;
       +        s1 = realloc(s1, n+1);
       +        if (!s1)
       +            exits("malloc");
       +        s1[n] = 0;
       +        snarflen = n;
       +        outTs(Tsetsnarf, n);
       +        if(n>0 && write(1, s1, n)!=n)
       +            exits("write error");
       +        free(s1);
       +    }else
       +        outTs(Tsetsnarf, 0);
       +    free(s2);
       +    cursorswitch(cursor);
        }
        
        void
        hgrow(int m, long a, long new, int req)
        {
       -        int i;
       -        Flayer *l;
       -        Text *t = whichtext(m);
       -        long o, b;
       -
       -        if(new <= 0)
       -                panic("hgrow");
       -        rresize(&t->rasp, a, 0L, new);
       -        for(l = &t->l[0], i = 0; i<NL; i++, l++){
       -                if(l->textfn == 0)
       -                        continue;
       -                o = l->origin;
       -                b = a-o-rmissing(&t->rasp, o, a);
       -                if(a < o)
       -                        l->origin+=new;
       -                if(a < l->p0)
       -                        l->p0+=new;
       -                if(a < l->p1)
       -                        l->p1+=new;
       -                /* must prevent b temporarily becoming unsigned */
       -                if(!req || a<o || (b>0 && b>l->f.nchars) ||
       -                    (l->f.nchars==0 && a-o>0))
       -                        continue;
       -                if(new>TBLOCKSIZE)
       -                        new = TBLOCKSIZE;
       -                outTsls(Trequest, m, a, (int)new);
       -                t->lock++;
       -                req = 0;
       -        }
       +    int i;
       +    Flayer *l;
       +    Text *t = whichtext(m);
       +    long o, b;
       +
       +    if(new <= 0)
       +        panic("hgrow");
       +    rresize(&t->rasp, a, 0L, new);
       +    for(l = &t->l[0], i = 0; i<NL; i++, l++){
       +        if(l->textfn == 0)
       +            continue;
       +        o = l->origin;
       +        b = a-o-rmissing(&t->rasp, o, a);
       +        if(a < o)
       +            l->origin+=new;
       +        if(a < l->p0)
       +            l->p0+=new;
       +        if(a < l->p1)
       +            l->p1+=new;
       +        /* must prevent b temporarily becoming unsigned */
       +        if(!req || a<o || (b>0 && b>l->f.nchars) ||
       +            (l->f.nchars==0 && a-o>0))
       +            continue;
       +        if(new>TBLOCKSIZE)
       +            new = TBLOCKSIZE;
       +        outTsls(Trequest, m, a, (int)new);
       +        t->lock++;
       +        req = 0;
       +    }
        }
        
        int
        hdata1(Text *t, long a, Rune *r, int len)
        {
       -        int i;
       -        Flayer *l;
       -        long o, b;
       -
       -        for(l = &t->l[0], i=0; i<NL; i++, l++){
       -                if(l->textfn==0)
       -                        continue;
       -                o = l->origin;
       -                b = a-o-rmissing(&t->rasp, o, a);
       -                /* must prevent b temporarily becoming unsigned */
       -                if(a<o || (b>0 && b>l->f.nchars))
       -                        continue;
       -                flinsert(l, r, r+len, o+b);
       -        }
       -        rdata(&t->rasp, a, a+len, r);
       -        rclean(&t->rasp);
       -        return len;
       +    int i;
       +    Flayer *l;
       +    long o, b;
       +
       +    for(l = &t->l[0], i=0; i<NL; i++, l++){
       +        if(l->textfn==0)
       +            continue;
       +        o = l->origin;
       +        b = a-o-rmissing(&t->rasp, o, a);
       +        /* must prevent b temporarily becoming unsigned */
       +        if(a<o || (b>0 && b>l->f.nchars))
       +            continue;
       +        flinsert(l, r, r+len, o+b);
       +    }
       +    rdata(&t->rasp, a, a+len, r);
       +    rclean(&t->rasp);
       +    return len;
        }
        
        int
        hdata(int m, long a, uchar *s, int len)
        {
       -        int i, w;
       -        Text *t = whichtext(m);
       -        Rune buf[DATASIZE], *r;
       +    int i, w;
       +    Text *t = whichtext(m);
       +    Rune buf[DATASIZE], *r;
        
       -        if(t->lock)
       -                --t->lock;
       -        if(len == 0)
       -                return 0;
       -        r = buf;
       -        for(i=0; i<len; i+=w,s+=w)
       -                w = chartorune(r++, (char*)s);
       -        return hdata1(t, a, buf, r-buf);
       +    if(t->lock)
       +        --t->lock;
       +    if(len == 0)
       +        return 0;
       +    r = buf;
       +    for(i=0; i<len; i+=w,s+=w)
       +        w = chartorune(r++, (char*)s);
       +    return hdata1(t, a, buf, r-buf);
        }
        
        int
        hdatarune(int m, long a, Rune *r, int len)
        {
       -        Text *t = whichtext(m);
       +    Text *t = whichtext(m);
        
       -        if(t->lock)
       -                --t->lock;
       -        if(len == 0)
       -                return 0;
       -        return hdata1(t, a, r, len);
       +    if(t->lock)
       +        --t->lock;
       +    if(len == 0)
       +        return 0;
       +    return hdata1(t, a, r, len);
        }
        
        void
        hcut(int m, long a, long old)
        {
       -        Flayer *l;
       -        Text *t = whichtext(m);
       -        int i;
       -        long o, b;
       -
       -        if(t->lock)
       -                --t->lock;
       -        for(l = &t->l[0], i = 0; i<NL; i++, l++){
       -                if(l->textfn == 0)
       -                        continue;
       -                o = l->origin;
       -                b = a-o-rmissing(&t->rasp, o, a);
       -                /* must prevent b temporarily becoming unsigned */
       -                if((b<0 || b<l->f.nchars) && a+old>=o){
       -                        fldelete(l, b<0? o : o+b,
       -                            a+old-rmissing(&t->rasp, o, a+old));
       -                }
       -                if(a+old<o)
       -                        l->origin-=old;
       -                else if(a<=o)
       -                        l->origin = a;
       -                if(a+old<l->p0)
       -                        l->p0-=old;
       -                else if(a<=l->p0)
       -                        l->p0 = a;
       -                if(a+old<l->p1)
       -                        l->p1-=old;
       -                else if(a<=l->p1)
       -                        l->p1 = a;
       -        }
       -        rresize(&t->rasp, a, old, 0L);
       -        rclean(&t->rasp);
       +    Flayer *l;
       +    Text *t = whichtext(m);
       +    int i;
       +    long o, b;
       +
       +    if(t->lock)
       +        --t->lock;
       +    for(l = &t->l[0], i = 0; i<NL; i++, l++){
       +        if(l->textfn == 0)
       +            continue;
       +        o = l->origin;
       +        b = a-o-rmissing(&t->rasp, o, a);
       +        /* must prevent b temporarily becoming unsigned */
       +        if((b<0 || b<l->f.nchars) && a+old>=o){
       +            fldelete(l, b<0? o : o+b,
       +                a+old-rmissing(&t->rasp, o, a+old));
       +        }
       +        if(a+old<o)
       +            l->origin-=old;
       +        else if(a<=o)
       +            l->origin = a;
       +        if(a+old<l->p0)
       +            l->p0-=old;
       +        else if(a<=l->p0)
       +            l->p0 = a;
       +        if(a+old<l->p1)
       +            l->p1-=old;
       +        else if(a<=l->p1)
       +            l->p1 = a;
       +    }
       +    rresize(&t->rasp, a, old, 0L);
       +    rclean(&t->rasp);
        }
 (DIR) diff --git a/samterm/rasp.c b/samterm/rasp.c
       @@ -9,255 +9,255 @@
        void
        rinit(Rasp *r)
        {
       -        r->nrunes=0;
       -        r->sect=0;
       +    r->nrunes=0;
       +    r->sect=0;
        }
        
        void
        rclear(Rasp *r)
        {
       -        Section *s, *ns;
       +    Section *s, *ns;
        
       -        for(s=r->sect; s; s=ns){
       -                ns = s->next;
       -                free(s->text);
       -                free(s);
       -        }
       -        r->sect = 0;
       +    for(s=r->sect; s; s=ns){
       +        ns = s->next;
       +        free(s->text);
       +        free(s);
       +    }
       +    r->sect = 0;
        }
        
        Section*
       -rsinsert(Rasp *r, Section *s)        /* insert before s */
       +rsinsert(Rasp *r, Section *s)   /* insert before s */
        {
       -        Section *t;
       -        Section *u;
       +    Section *t;
       +    Section *u;
        
       -        t = alloc(sizeof(Section));
       -        if(r->sect == s){        /* includes empty list case: r->sect==s==0 */
       -                r->sect = t;
       -                t->next = s;
       -        }else{
       -                u = r->sect;
       -                if(u == 0)
       -                        panic("rsinsert 1");
       -                do{
       -                        if(u->next == s){
       -                                t->next = s;
       -                                u->next = t;
       -                                goto Return;
       -                        }
       -                        u=u->next;
       -                }while(u);
       -                panic("rsinsert 2");
       -        }
       +    t = alloc(sizeof(Section));
       +    if(r->sect == s){   /* includes empty list case: r->sect==s==0 */
       +        r->sect = t;
       +        t->next = s;
       +    }else{
       +        u = r->sect;
       +        if(u == 0)
       +            panic("rsinsert 1");
       +        do{
       +            if(u->next == s){
       +                t->next = s;
       +                u->next = t;
       +                goto Return;
       +            }
       +            u=u->next;
       +        }while(u);
       +        panic("rsinsert 2");
       +    }
            Return:
       -        return t;
       +    return t;
        }
        
        void
        rsdelete(Rasp *r, Section *s)
        {
       -        Section *t;
       +    Section *t;
        
       -        if(s == 0)
       -                panic("rsdelete");
       -        if(r->sect == s){
       -                r->sect = s->next;
       -                goto Free;
       -        }
       -        for(t=r->sect; t; t=t->next)
       -                if(t->next == s){
       -                        t->next = s->next;
       -        Free:
       -                        if(s->text)
       -                                free(s->text);
       -                        free(s);
       -                        return;
       -                }
       -        panic("rsdelete 2");
       +    if(s == 0)
       +        panic("rsdelete");
       +    if(r->sect == s){
       +        r->sect = s->next;
       +        goto Free;
       +    }
       +    for(t=r->sect; t; t=t->next)
       +        if(t->next == s){
       +            t->next = s->next;
       +    Free:
       +            if(s->text)
       +                free(s->text);
       +            free(s);
       +            return;
       +        }
       +    panic("rsdelete 2");
        }
        
        void
        splitsect(Rasp *r, Section *s, long n0)
        {
       -        if(s == 0)
       -                panic("splitsect");
       -        rsinsert(r, s->next);
       -        if(s->text == 0)
       -                s->next->text = 0;
       -        else{
       -                s->next->text = alloc(RUNESIZE*(TBLOCKSIZE+1));
       -                Strcpy(s->next->text, s->text+n0);
       -                s->text[n0] = 0;
       -        }
       -        s->next->nrunes = s->nrunes-n0;
       -        s->nrunes = n0;
       +    if(s == 0)
       +        panic("splitsect");
       +    rsinsert(r, s->next);
       +    if(s->text == 0)
       +        s->next->text = 0;
       +    else{
       +        s->next->text = alloc(RUNESIZE*(TBLOCKSIZE+1));
       +        Strcpy(s->next->text, s->text+n0);
       +        s->text[n0] = 0;
       +    }
       +    s->next->nrunes = s->nrunes-n0;
       +    s->nrunes = n0;
        }
        
        Section *
       -findsect(Rasp *r, Section *s, long p, long q)        /* find sect containing q and put q on a sect boundary */
       +findsect(Rasp *r, Section *s, long p, long q)   /* find sect containing q and put q on a sect boundary */
        {
       -        if(s==0 && p!=q)
       -                panic("findsect");
       -        for(; s && p+s->nrunes<=q; s=s->next)
       -                p += s->nrunes;
       -        if(p != q){
       -                splitsect(r, s, q-p);
       -                s = s->next;
       -        }
       -        return s;
       +    if(s==0 && p!=q)
       +        panic("findsect");
       +    for(; s && p+s->nrunes<=q; s=s->next)
       +        p += s->nrunes;
       +    if(p != q){
       +        splitsect(r, s, q-p);
       +        s = s->next;
       +    }
       +    return s;
        }
        
        void
        rresize(Rasp *r, long a, long old, long new)
        {
       -        Section *s, *t, *ns;
       +    Section *s, *t, *ns;
        
       -        s = findsect(r, r->sect, 0L, a);
       -        t = findsect(r, s, a, a+old);
       -        for(; s!=t; s=ns){
       -                ns=s->next;
       -                rsdelete(r, s);
       -        }
       -        /* now insert the new piece before t */
       -        if(new > 0){
       -                ns=rsinsert(r, t);
       -                ns->nrunes=new;
       -                ns->text=0;
       -        }
       -        r->nrunes += new-old;
       +    s = findsect(r, r->sect, 0L, a);
       +    t = findsect(r, s, a, a+old);
       +    for(; s!=t; s=ns){
       +        ns=s->next;
       +        rsdelete(r, s);
       +    }
       +    /* now insert the new piece before t */
       +    if(new > 0){
       +        ns=rsinsert(r, t);
       +        ns->nrunes=new;
       +        ns->text=0;
       +    }
       +    r->nrunes += new-old;
        }
        
        void
        rdata(Rasp *r, long p0, long p1, Rune *cp)
        {
       -        Section *s, *t, *ns;
       +    Section *s, *t, *ns;
        
       -        s = findsect(r, r->sect, 0L, p0);
       -        t = findsect(r, s, p0, p1);
       -        for(; s!=t; s=ns){
       -                ns=s->next;
       -                if(s->text)
       -                        panic("rdata");
       -                rsdelete(r, s);
       -        }
       -        p1 -= p0;
       -        s = rsinsert(r, t);
       -        s->text = alloc(RUNESIZE*(TBLOCKSIZE+1));
       -        memmove(s->text, cp, RUNESIZE*p1);
       -        s->text[p1] = 0;
       -        s->nrunes = p1;
       +    s = findsect(r, r->sect, 0L, p0);
       +    t = findsect(r, s, p0, p1);
       +    for(; s!=t; s=ns){
       +        ns=s->next;
       +        if(s->text)
       +            panic("rdata");
       +        rsdelete(r, s);
       +    }
       +    p1 -= p0;
       +    s = rsinsert(r, t);
       +    s->text = alloc(RUNESIZE*(TBLOCKSIZE+1));
       +    memmove(s->text, cp, RUNESIZE*p1);
       +    s->text[p1] = 0;
       +    s->nrunes = p1;
        }
        
        void
        rclean(Rasp *r)
        {
       -        Section *s;
       +    Section *s;
        
       -        for(s=r->sect; s; s=s->next)
       -                while(s->next && (s->text!=0)==(s->next->text!=0)){
       -                        if(s->text){
       -                                if(s->nrunes+s->next->nrunes>TBLOCKSIZE)
       -                                        break;
       -                                Strcpy(s->text+s->nrunes, s->next->text);
       -                        }
       -                        s->nrunes += s->next->nrunes;
       -                        rsdelete(r, s->next);
       -                }
       +    for(s=r->sect; s; s=s->next)
       +        while(s->next && (s->text!=0)==(s->next->text!=0)){
       +            if(s->text){
       +                if(s->nrunes+s->next->nrunes>TBLOCKSIZE)
       +                    break;
       +                Strcpy(s->text+s->nrunes, s->next->text);
       +            }
       +            s->nrunes += s->next->nrunes;
       +            rsdelete(r, s->next);
       +        }
        }
        
        void
        Strcpy(Rune *to, Rune *from)
        {
       -        do; while(*to++ = *from++);
       +    do; while(*to++ = *from++);
        }
        
        Rune*
        rload(Rasp *r, ulong p0, ulong p1, ulong *nrp)
        {
       -        Section *s;
       -        long p;
       -        int n, nb;
       +    Section *s;
       +    long p;
       +    int n, nb;
        
       -        nb = 0;
       -        Strgrow(&scratch, &nscralloc, p1-p0+1);
       -        scratch[0] = 0;
       -        for(p=0,s=r->sect; s && p+s->nrunes<=p0; s=s->next)
       -                p += s->nrunes;
       -        while(p<p1 && s){
       -                /*
       -                 * Subtle and important.  If we are preparing to handle an 'rdata'
       -                 * call, it's because we have an 'rresize' hole here, so the
       -                 * screen doesn't have data for that space anyway (it got cut
       -                 * first).  So pretend it isn't there.
       -                 */
       -                if(s->text){
       -                        n = s->nrunes-(p0-p);
       -                        if(n>p1-p0)        /* all in this section */
       -                                n = p1-p0;
       -                        memmove(scratch+nb, s->text+(p0-p), n*RUNESIZE);
       -                        nb += n;
       -                        scratch[nb] = 0;
       -                }
       -                p += s->nrunes;
       -                p0 = p;
       -                s = s->next;
       -        }
       -        if(nrp)
       -                *nrp = nb;
       -        return scratch;
       +    nb = 0;
       +    Strgrow(&scratch, &nscralloc, p1-p0+1);
       +    scratch[0] = 0;
       +    for(p=0,s=r->sect; s && p+s->nrunes<=p0; s=s->next)
       +        p += s->nrunes;
       +    while(p<p1 && s){
       +        /*
       +         * Subtle and important.  If we are preparing to handle an 'rdata'
       +         * call, it's because we have an 'rresize' hole here, so the
       +         * screen doesn't have data for that space anyway (it got cut
       +         * first).  So pretend it isn't there.
       +         */
       +        if(s->text){
       +            n = s->nrunes-(p0-p);
       +            if(n>p1-p0) /* all in this section */
       +                n = p1-p0;
       +            memmove(scratch+nb, s->text+(p0-p), n*RUNESIZE);
       +            nb += n;
       +            scratch[nb] = 0;
       +        }
       +        p += s->nrunes;
       +        p0 = p;
       +        s = s->next;
       +    }
       +    if(nrp)
       +        *nrp = nb;
       +    return scratch;
        }
        
        int
        rmissing(Rasp *r, ulong p0, ulong p1)
        {
       -        Section *s;
       -        long p;
       -        int n, nm=0;
       +    Section *s;
       +    long p;
       +    int n, nm=0;
        
       -        for(p=0,s=r->sect; s && p+s->nrunes<=p0; s=s->next)
       -                p += s->nrunes;
       -        while(p<p1 && s){
       -                if(s->text == 0){
       -                        n = s->nrunes-(p0-p);
       -                        if(n > p1-p0)        /* all in this section */
       -                                n = p1-p0;
       -                        nm += n;
       -                }
       -                p += s->nrunes;
       -                p0 = p;
       -                s = s->next;
       -        }
       -        return nm;
       +    for(p=0,s=r->sect; s && p+s->nrunes<=p0; s=s->next)
       +        p += s->nrunes;
       +    while(p<p1 && s){
       +        if(s->text == 0){
       +            n = s->nrunes-(p0-p);
       +            if(n > p1-p0)   /* all in this section */
       +                n = p1-p0;
       +            nm += n;
       +        }
       +        p += s->nrunes;
       +        p0 = p;
       +        s = s->next;
       +    }
       +    return nm;
        }
        
        int
        rcontig(Rasp *r, ulong p0, ulong p1, int text)
        {
       -        Section *s;
       -        long p, n;
       -        int np=0;
       +    Section *s;
       +    long p, n;
       +    int np=0;
        
       -        for(p=0,s=r->sect; s && p+s->nrunes<=p0; s=s->next)
       -                p += s->nrunes;
       -        while(p<p1 && s && (text? (s->text!=0) : (s->text==0))){
       -                n = s->nrunes-(p0-p);
       -                if(n > p1-p0)        /* all in this section */
       -                        n = p1-p0;
       -                np += n;
       -                p += s->nrunes;
       -                p0 = p;
       -                s = s->next;
       -        }
       -        return np;
       +    for(p=0,s=r->sect; s && p+s->nrunes<=p0; s=s->next)
       +        p += s->nrunes;
       +    while(p<p1 && s && (text? (s->text!=0) : (s->text==0))){
       +        n = s->nrunes-(p0-p);
       +        if(n > p1-p0)   /* all in this section */
       +            n = p1-p0;
       +        np += n;
       +        p += s->nrunes;
       +        p0 = p;
       +        s = s->next;
       +    }
       +    return np;
        }
        
        void
       -Strgrow(Rune **s, long *n, int want)        /* can always toss the old data when called */
       +Strgrow(Rune **s, long *n, int want)    /* can always toss the old data when called */
        {
       -        if(*n >= want)
       -                return;
       -        free(*s);
       -        *s = alloc(RUNESIZE*want);
       -        *n = want;
       +    if(*n >= want)
       +        return;
       +    free(*s);
       +    *s = alloc(RUNESIZE*want);
       +    *n = want;
        }
 (DIR) diff --git a/samterm/samterm.h b/samterm/samterm.h
       @@ -1,159 +1,159 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
       -#define        SAMTERM
       +#define SAMTERM
        
        #include "../config.h"
        
       -#define        RUNESIZE        sizeof(Rune)
       -#define        MAXFILES        256
       -#define        NL        5
       +#define RUNESIZE    sizeof(Rune)
       +#define MAXFILES    256
       +#define NL  5
        
        enum{
       -        Up,
       -        Down
       +    Up,
       +    Down
        };
        
       -typedef struct Text        Text;
       -typedef struct Section        Section;
       -typedef struct Rasp        Rasp;
       +typedef struct Text Text;
       +typedef struct Section  Section;
       +typedef struct Rasp Rasp;
        
        struct Section
        {
       -        long        nrunes;
       -        Rune        *text;                /* if null, we haven't got it */
       -        Section        *next;
       +    long    nrunes;
       +    Rune    *text;      /* if null, we haven't got it */
       +    Section *next;
        };
        
        struct Rasp
        {
       -        long        nrunes;
       -        Section        *sect;
       +    long    nrunes;
       +    Section *sect;
        };
        
       -#define        Untagged        ((ushort)65535)
       +#define Untagged    ((ushort)65535)
        
        struct Text
        {
       -        Rasp        rasp;
       -        short        nwin;
       -        short        front;                /* input window */
       -        ushort        tag;
       -        char        lock;
       -        Flayer        l[NL];                /* screen storage */
       +    Rasp    rasp;
       +    short   nwin;
       +    short   front;      /* input window */
       +    ushort  tag;
       +    char    lock;
       +    Flayer  l[NL];      /* screen storage */
        };
        
        enum Resource
        {
       -        Eextern                = 0x08,
       -        Ehost                = 0x04,
       -        RHost                = Ehost,
       -        RExtern                = Eextern,
       -        RKeyboard        = Ekeyboard,
       -        RMouse                = Emouse
       +    Eextern     = 0x08,
       +    Ehost       = 0x04,
       +    RHost       = Ehost,
       +    RExtern     = Eextern,
       +    RKeyboard   = Ekeyboard,
       +    RMouse      = Emouse
        };
        
       -extern Text        *text[];
       -extern uchar        *name[];
       -extern ushort        tag[];
       -extern int        nname;
       -extern unsigned int        cursor;
       -extern Flayer        *which;
       -extern Flayer        *work;
       -extern Text        cmd;
       -extern Rune        *scratch;
       -extern long        nscralloc;
       -extern char        lock;
       -extern char        hasunlocked;
       -extern long        snarflen;
       -extern Mouse        mouse;
       -extern long        modified;
       +extern Text *text[];
       +extern uchar    *name[];
       +extern ushort   tag[];
       +extern int  nname;
       +extern unsigned int cursor;
       +extern Flayer   *which;
       +extern Flayer   *work;
       +extern Text cmd;
       +extern Rune *scratch;
       +extern long nscralloc;
       +extern char lock;
       +extern char hasunlocked;
       +extern long snarflen;
       +extern Mouse    mouse;
       +extern long modified;
        
       -Rune        *stgettext(Flayer*, long, ulong*);
       -void        *alloc(ulong n);
       +Rune    *stgettext(Flayer*, long, ulong*);
       +void    *alloc(ulong n);
        
       -void        iconinit(void);
       -void        getscreen(int, char**);
       -void        initio(void);
       -void        setlock(void);
       -void        outcmd(void);
       -void        rinit(Rasp*);
       -void        startnewfile(int, Text*);
       -void        cursorset(Point);
       -void        getmouse(void);
       -void        mouseunblock(void);
       -void        kbdblock(void);
       -void        extstart(void);
       -int        button(int but);
       -int        load(char*, int);
       -int        waitforio(void);
       -int        rcvchar(void);
       -int        getch(void);
       +void    iconinit(void);
       +void    getscreen(int, char**);
       +void    initio(void);
       +void    setlock(void);
       +void    outcmd(void);
       +void    rinit(Rasp*);
       +void    startnewfile(int, Text*);
       +void    cursorset(Point);
       +void    getmouse(void);
       +void    mouseunblock(void);
       +void    kbdblock(void);
       +void    extstart(void);
       +int button(int but);
       +int load(char*, int);
       +int waitforio(void);
       +int rcvchar(void);
       +int getch(void);
        Keystroke qpeekc(void);
       -Keystroke        kbdchar(void);
       -void        mouseexit(void);
       -void        cut(Text*, int, int, int);
       -void        paste(Text*, int);
       -void        snarf(Text*, int);
       -int        center(Flayer*, long);
       -int        xmenuhit(int, Menu*);
       -void        buttons(int);
       -int        getr(Rectangle*);
       -void        current(Flayer*);
       -void        duplicate(Flayer*, Rectangle, XftFont*, int);
       -void        startfile(Text*);
       -void        panic(char*);
       -void        closeup(Flayer*);
       -void        Strgrow(Rune**, long*, int);
       -int        RESHAPED(void);
       -void        reshape(void);
       -void        rcv(void);
       -void        type(Flayer*, int);
       -void        menu2hit(void);
       -void        menu3hit(void);
       -void        scroll(Flayer*, int, int);
       -void        hcheck(int);
       -void        rclear(Rasp*);
       -int        whichmenu(int);
       -void        hcut(int, long, long);
       -void        horigin(int, long, Flayer *);
       -void        hgrow(int, long, long, int);
       -int        hdata(int, long, uchar*, int);
       -int        hdatarune(int, long, Rune*, int);
       -Rune        *rload(Rasp*, ulong, ulong, ulong*);
       -void        menuins(int, uchar*, Text*, int, int);
       -void        menudel(int);
       -Text        *sweeptext(int, int);
       -void        setpat(char*);
       -void        scrdraw(Flayer*, long tot);
       -int        rcontig(Rasp*, ulong, ulong, int);
       -int        rmissing(Rasp*, ulong, ulong);
       -void        rresize(Rasp *, long, long, long);
       -void        rdata(Rasp*, long, long, Rune*);
       -void        rclean(Rasp*);
       -void        scrorigin(Flayer*, int, long);
       -long        scrtotal(Flayer*);
       -void        flnewlyvisible(Flayer*);
       -char        *rcvstring(void);
       -void        Strcpy(Rune*, Rune*);
       -void        Strncpy(Rune*, Rune*, long);
       -void        flushtyping(int);
       -void        dumperrmsg(int, int, int, int);
       -int        screensize(int*,int*);
       +Keystroke   kbdchar(void);
       +void    mouseexit(void);
       +void    cut(Text*, int, int, int);
       +void    paste(Text*, int);
       +void    snarf(Text*, int);
       +int center(Flayer*, long);
       +int xmenuhit(int, Menu*);
       +void    buttons(int);
       +int getr(Rectangle*);
       +void    current(Flayer*);
       +void    duplicate(Flayer*, Rectangle, XftFont*, int);
       +void    startfile(Text*);
       +void    panic(char*);
       +void    closeup(Flayer*);
       +void    Strgrow(Rune**, long*, int);
       +int RESHAPED(void);
       +void    reshape(void);
       +void    rcv(void);
       +void    type(Flayer*, int);
       +void    menu2hit(void);
       +void    menu3hit(void);
       +void    scroll(Flayer*, int, int);
       +void    hcheck(int);
       +void    rclear(Rasp*);
       +int whichmenu(int);
       +void    hcut(int, long, long);
       +void    horigin(int, long, Flayer *);
       +void    hgrow(int, long, long, int);
       +int hdata(int, long, uchar*, int);
       +int hdatarune(int, long, Rune*, int);
       +Rune    *rload(Rasp*, ulong, ulong, ulong*);
       +void    menuins(int, uchar*, Text*, int, int);
       +void    menudel(int);
       +Text    *sweeptext(int, int);
       +void    setpat(char*);
       +void    scrdraw(Flayer*, long tot);
       +int rcontig(Rasp*, ulong, ulong, int);
       +int rmissing(Rasp*, ulong, ulong);
       +void    rresize(Rasp *, long, long, long);
       +void    rdata(Rasp*, long, long, Rune*);
       +void    rclean(Rasp*);
       +void    scrorigin(Flayer*, int, long);
       +long    scrtotal(Flayer*);
       +void    flnewlyvisible(Flayer*);
       +char    *rcvstring(void);
       +void    Strcpy(Rune*, Rune*);
       +void    Strncpy(Rune*, Rune*, long);
       +void    flushtyping(int);
       +void    dumperrmsg(int, int, int, int);
       +int screensize(int*,int*);
        
        #include "../sam/mesg.h"
        
       -void        outTs(Tmesg, int);
       -void        outT0(Tmesg);
       -void        outTl(Tmesg, long);
       -void        outTslS(Tmesg, int, long, Rune*);
       -void        outTslll(Tmesg, int, long, long, long);
       -void        outTsll(Tmesg, int, long, long);
       -void        outTsl(Tmesg, int, long);
       -void        outTsv(Tmesg, int, void*);
       -void        outTv(Tmesg, void*);
       -void        outstart(Tmesg);
       -void        outcopy(int, uchar*);
       -void        outshort(int);
       -void        outlong(long);
       -void        outvlong(void*);
       -void        outsend(void);
       +void    outTs(Tmesg, int);
       +void    outT0(Tmesg);
       +void    outTl(Tmesg, long);
       +void    outTslS(Tmesg, int, long, Rune*);
       +void    outTslll(Tmesg, int, long, long, long);
       +void    outTsll(Tmesg, int, long, long);
       +void    outTsl(Tmesg, int, long);
       +void    outTsv(Tmesg, int, void*);
       +void    outTv(Tmesg, void*);
       +void    outstart(Tmesg);
       +void    outcopy(int, uchar*);
       +void    outshort(int);
       +void    outlong(long);
       +void    outvlong(void*);
       +void    outsend(void);
        int getlayer(const Flayer *l, const Text *t);
 (DIR) diff --git a/samterm/scroll.c b/samterm/scroll.c
       @@ -12,134 +12,134 @@ extern Mouse mouse;
        Rectangle
        scrpos(Rectangle r, long p0, long p1, long tot)
        {
       -        long h;
       -        Rectangle q;
       +    long h;
       +    Rectangle q;
        
       -        q = inset(r, 1);
       -        h = q.max.y-q.min.y;
       -        if(tot == 0)
       -                return q;
       -        if(tot > 1024L*1024L)
       -                tot>>=10, p0>>=10, p1>>=10;
       -        if(p0 > 0)
       -                q.min.y += h*p0/tot;
       -        if(p1 < tot)
       -                q.max.y -= h*(tot-p1)/tot;
       -        if(q.max.y < q.min.y+2){
       -                if(q.min.y+2 <= r.max.y)
       -                        q.max.y = q.min.y+2;
       -                else
       -                        q.min.y = q.max.y-2;
       -        }
       -        return q;
       +    q = inset(r, 1);
       +    h = q.max.y-q.min.y;
       +    if(tot == 0)
       +        return q;
       +    if(tot > 1024L*1024L)
       +        tot>>=10, p0>>=10, p1>>=10;
       +    if(p0 > 0)
       +        q.min.y += h*p0/tot;
       +    if(p1 < tot)
       +        q.max.y -= h*(tot-p1)/tot;
       +    if(q.max.y < q.min.y+2){
       +        if(q.min.y+2 <= r.max.y)
       +            q.max.y = q.min.y+2;
       +        else
       +            q.min.y = q.max.y-2;
       +    }
       +    return q;
        }
        
        void
        scrflip(Flayer *l, Rectangle r)
        {
       -        if(rectclip(&r, l->scroll))
       -                bitblt2(l->f.b, r.min, l->f.b, r, F&~D, 0, l->bg);
       +    if(rectclip(&r, l->scroll))
       +        bitblt2(l->f.b, r.min, l->f.b, r, F&~D, 0, l->bg);
        }
        
        void
        scrdraw(Flayer *l, long tot)
        {
       -        Rectangle r, r1, r2;
       -        Bitmap *b;
       -        static Bitmap *x;
       -        int h;
       +    Rectangle r, r1, r2;
       +    Bitmap *b;
       +    static Bitmap *x;
       +    int h;
        
       -        if(l->f.b == 0)
       -                panic("scrdraw");
       -        r = l->scroll;
       -        r.min.x += 1;        /* border between margin and bar */
       -        r1 = r;
       -        if(l->visible == All){
       -                if(x == 0){
       -                        if (screensize(0, &h) == 0)
       -                                h = 2048;
       -                        x = balloc(Rect(0, 0, 32, h), l->f.b->ldepth);
       -                        if(x == 0)
       -                                panic("scrdraw balloc");
       -                }
       -                b = x;
       -                r1.min.x = 0;
       -                r1.max.x = Dx(r);
       -        }else
       -                b = l->f.b;
       -        bitblt2(b, r1.min, b, r1, F, 0, l->bg);
       -        texture(b, inset(r1, 1), darkgrey, S);
       -        r2 = scrpos(r1, l->origin, l->origin+l->f.nchars, tot);
       -        bitblt2(b, r2.min, b, r2, 0, 0, l->bg);
       -        if(b!=l->f.b)
       -                bitblt2(l->f.b, r.min, b, r1, S, 0, l->bg);
       +    if(l->f.b == 0)
       +        panic("scrdraw");
       +    r = l->scroll;
       +    r.min.x += 1;   /* border between margin and bar */
       +    r1 = r;
       +    if(l->visible == All){
       +        if(x == 0){
       +            if (screensize(0, &h) == 0)
       +                h = 2048;
       +            x = balloc(Rect(0, 0, 32, h), l->f.b->ldepth);
       +            if(x == 0)
       +                panic("scrdraw balloc");
       +        }
       +        b = x;
       +        r1.min.x = 0;
       +        r1.max.x = Dx(r);
       +    }else
       +        b = l->f.b;
       +    bitblt2(b, r1.min, b, r1, F, 0, l->bg);
       +    texture(b, inset(r1, 1), darkgrey, S);
       +    r2 = scrpos(r1, l->origin, l->origin+l->f.nchars, tot);
       +    bitblt2(b, r2.min, b, r2, 0, 0, l->bg);
       +    if(b!=l->f.b)
       +        bitblt2(l->f.b, r.min, b, r1, S, 0, l->bg);
        }
        
        void
        scroll(Flayer *l, int pbut, int but)
        {
       -        int in = 0, oin;
       -        long tot = scrtotal(l);
       -        Rectangle scr, r, s, rt;
       -        int x, y, my, oy, h;
       -        long p0;
       +    int in = 0, oin;
       +    long tot = scrtotal(l);
       +    Rectangle scr, r, s, rt;
       +    int x, y, my, oy, h;
       +    long p0;
        
       -        s = inset(l->scroll, 1);
       -        x = s.min.x+FLSCROLLWID/2;
       -        scr = scrpos(l->scroll, l->origin, l->origin+l->f.nchars, tot);
       -        r = scr;
       -        y = scr.min.y;
       -        my = mouse.xy.y;
       -        do{
       -                oin = in;
       -                in = abs(x-mouse.xy.x)<=FLSCROLLWID/2;
       -                if(oin != in)
       -                        scrflip(l, r);
       -                if(in){
       -                        oy = y;
       -                        my = mouse.xy.y;
       -                        if(my < s.min.y)
       -                                my = s.min.y;
       -                        if(my >= s.max.y)
       -                                my = s.max.y;
       -                        if(!eqpt(mouse.xy, Pt(x, my)))
       -                                cursorset(Pt(x, my));
       -                        if(but == 1){
       -                                p0 = l->origin-frcharofpt(&l->f, Pt(s.max.x, my));
       -                                rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
       -                                y = rt.min.y;
       -                        }else if(but == 2){
       -                                y = my;
       -                                if(y > s.max.y-2)
       -                                        y = s.max.y-2;
       -                        }else if(but == 3){
       -                                p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
       -                                rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
       -                                y = rt.min.y;
       -                        }
       -                        if(y != oy){
       -                                scrflip(l, r);
       -                                r = raddp(scr, Pt(0, y-scr.min.y));
       -                                scrflip(l, r);
       -                        }
       -                }
       -        }while(button(pbut));
       -        if(in){
       -                h = s.max.y-s.min.y;
       -                scrflip(l, r);
       -                p0 = 0;
       -                if(but == 1)
       -                        p0 = (long)(my-s.min.y)/l->f.fheight+1;
       -                else if(but == 2){
       -                        if(tot > 1024L*1024L)
       -                                p0 = ((tot>>10)*(y-s.min.y)/h)<<10;
       -                        else
       -                                p0 = tot*(y-s.min.y)/h;
       -                }else if(but == 3){
       -                        p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
       -                        if(p0 > tot)
       -                                p0 = tot;
       -                }
       -                scrorigin(l, but, p0);
       -        }
       +    s = inset(l->scroll, 1);
       +    x = s.min.x+FLSCROLLWID/2;
       +    scr = scrpos(l->scroll, l->origin, l->origin+l->f.nchars, tot);
       +    r = scr;
       +    y = scr.min.y;
       +    my = mouse.xy.y;
       +    do{
       +        oin = in;
       +        in = abs(x-mouse.xy.x)<=FLSCROLLWID/2;
       +        if(oin != in)
       +            scrflip(l, r);
       +        if(in){
       +            oy = y;
       +            my = mouse.xy.y;
       +            if(my < s.min.y)
       +                my = s.min.y;
       +            if(my >= s.max.y)
       +                my = s.max.y;
       +            if(!eqpt(mouse.xy, Pt(x, my)))
       +                cursorset(Pt(x, my));
       +            if(but == 1){
       +                p0 = l->origin-frcharofpt(&l->f, Pt(s.max.x, my));
       +                rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
       +                y = rt.min.y;
       +            }else if(but == 2){
       +                y = my;
       +                if(y > s.max.y-2)
       +                    y = s.max.y-2;
       +            }else if(but == 3){
       +                p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
       +                rt = scrpos(l->scroll, p0, p0+l->f.nchars, tot);
       +                y = rt.min.y;
       +            }
       +            if(y != oy){
       +                scrflip(l, r);
       +                r = raddp(scr, Pt(0, y-scr.min.y));
       +                scrflip(l, r);
       +            }
       +        }
       +    }while(button(pbut));
       +    if(in){
       +        h = s.max.y-s.min.y;
       +        scrflip(l, r);
       +        p0 = 0;
       +        if(but == 1)
       +            p0 = (long)(my-s.min.y)/l->f.fheight+1;
       +        else if(but == 2){
       +            if(tot > 1024L*1024L)
       +                p0 = ((tot>>10)*(y-s.min.y)/h)<<10;
       +            else
       +                p0 = tot*(y-s.min.y)/h;
       +        }else if(but == 3){
       +            p0 = l->origin+frcharofpt(&l->f, Pt(s.max.x, my));
       +            if(p0 > tot)
       +                p0 = tot;
       +        }
       +        scrorigin(l, but, p0);
       +    }
        }
 (DIR) diff --git a/samterm/unix.c b/samterm/unix.c
       @@ -11,18 +11,18 @@
        #include <signal.h>
        
        #ifdef APOLLO
       -#define        O_NONBLOCK        O_NDELAY
       +#define O_NONBLOCK  O_NDELAY
        #endif
        
       -#if        defined(UMIPS) || defined(SUNOS)
       -#define        atexit(p)                /* sigh */
       +#if defined(UMIPS) || defined(SUNOS)
       +#define atexit(p)       /* sigh */
        #endif
        
        char *exname = NULL;
        static char *fallbacks[] = {
       -        "*scrollForwardR: true",
       -        "*geometry: 740x780",
       -        NULL
       +    "*scrollForwardR: true",
       +    "*geometry: 740x780",
       +    NULL
        };
        
        extern int nofifo;
       @@ -30,55 +30,55 @@ extern int nofifo;
        void
        getscreen(int argc, char **argv)
        {
       -        int fd;
       -        Rectangle r;
       +    int fd;
       +    Rectangle r;
        
       -        signal(SIGINT, SIG_IGN);
       -        xtbinit(0, "Sam", &argc, argv, fallbacks);
       -        r = inset(screen.r, 4);
       -        bitblt(&screen, r.min, &screen, r, 0);
       +    signal(SIGINT, SIG_IGN);
       +    xtbinit(0, "Sam", &argc, argv, fallbacks);
       +    r = inset(screen.r, 4);
       +    bitblt(&screen, r.min, &screen, r, 0);
        }
        
        int
        screensize(int *w, int *h)
        {
       -        return scrpix(w,h);
       +    return scrpix(w,h);
        }
        
        void
        dumperrmsg(int count, int type, int count0, int c)
        {
       -        uchar *cp;
       -        int i;
       -
       -        cp = (uchar *) rcvstring();
       -        fprintf(stderr, "samterm: host mesg: count %d %ux %ux %ux %s...ignored\n",
       -                count, type, count0, c, cp);
       -        i = 0;
       -        while (*cp) {
       -                fprintf(stderr, "%x ", *cp);
       -                if (i++ >= 20) {
       -                        fprintf(stderr, "\n");
       -                        i = 0;
       -                }
       -                cp++;
       -        }
       +    uchar *cp;
       +    int i;
       +
       +    cp = (uchar *) rcvstring();
       +    fprintf(stderr, "samterm: host mesg: count %d %ux %ux %ux %s...ignored\n",
       +        count, type, count0, c, cp);
       +    i = 0;
       +    while (*cp) {
       +        fprintf(stderr, "%x ", *cp);
       +        if (i++ >= 20) {
       +            fprintf(stderr, "\n");
       +            i = 0;
       +        }
       +        cp++;
       +    }
        }
        
        void
        removeextern(void)
        {
       -        if (exname) {
       -                (void)unlink(exname);
       -                exname = 0;
       -        }
       +    if (exname) {
       +        (void)unlink(exname);
       +        exname = 0;
       +    }
        }
        /*
       - *        some systems do not support non-blocking i/o on named pipes
       - *        or do not provide working POSIX interfaces to the pipes.
       - *        in that case, add the name of the system to the 'ifdef' that
       - *        disables the code at the beginning of the function.
       - *        The external 'B' command will not work.
       + *  some systems do not support non-blocking i/o on named pipes
       + *  or do not provide working POSIX interfaces to the pipes.
       + *  in that case, add the name of the system to the 'ifdef' that
       + *  disables the code at the beginning of the function.
       + *  The external 'B' command will not work.
         */
        
        void
       @@ -87,76 +87,76 @@ extstart(void)
            if (nofifo)
                return;
        
       -#ifndef        NOFIFO
       -        extern char *machine;
       -        char        *user;
       -        char        *home;
       -        int        fd;
       -        int        flags;
       -
       -        user = getuser();
       -        home = getenv("HOME");
       -
       -        if (home == NULL)
       -        {
       -                return;
       -        }
       -
       -        exname = (char *)alloc(4 + 6 + strlen(home) + 1 + strlen(user) + 1 + strlen(machine) + 100);
       -        sprint(exname, "%s/.sam.%s", home, machine);
       -
       -        /* Make the named pipe. */
       -        if (mkfifo(exname, 0600) == -1) {
       -                struct stat        statb;
       -                extern int        errno;
       -
       -                if (errno != EEXIST || stat(exname, &statb) == -1)
       -                        return;
       -
       -                if (!S_ISFIFO(statb.st_mode)) {
       -                        removeextern();
       -                        if (mkfifo(exname, 0600) == -1)
       -                                return;
       -                }
       -        }
       -
       -        fd = open(exname, O_RDONLY | O_NONBLOCK);
       -        if (fd == -1) {
       -                removeextern();
       -                return;
       -        }
       -
       -        /*
       -         * Turn off no-delay and provide ourselves as a lingering
       -         * writer so as not to get end of file on read.
       +#ifndef NOFIFO
       +    extern char *machine;
       +    char    *user;
       +    char    *home;
       +    int fd;
       +    int flags;
       +
       +    user = getuser();
       +    home = getenv("HOME");
       +
       +    if (home == NULL)
       +    {
       +        return;
       +    }
       +
       +    exname = (char *)alloc(4 + 6 + strlen(home) + 1 + strlen(user) + 1 + strlen(machine) + 100);
       +    sprint(exname, "%s/.sam.%s", home, machine);
       +
       +    /* Make the named pipe. */
       +    if (mkfifo(exname, 0600) == -1) {
       +        struct stat statb;
       +        extern int  errno;
       +
       +        if (errno != EEXIST || stat(exname, &statb) == -1)
       +            return;
       +
       +        if (!S_ISFIFO(statb.st_mode)) {
       +            removeextern();
       +            if (mkfifo(exname, 0600) == -1)
       +                return;
       +        }
       +    }
       +
       +    fd = open(exname, O_RDONLY | O_NONBLOCK);
       +    if (fd == -1) {
       +        removeextern();
       +        return;
       +    }
       +
       +    /*
       +     * Turn off no-delay and provide ourselves as a lingering
       +     * writer so as not to get end of file on read.
                 */ 
       -        flags = fcntl(fd, F_GETFL, 0);
       -        if (flags == -1 || fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1
       -                        || open(exname, O_WRONLY) == -1) {
       -                (void)close(fd);
       -                removeextern();
       -                return;
       -        }
       -
       -        estart(Eextern, fd, 8192);
       -        atexit(removeextern);
       +    flags = fcntl(fd, F_GETFL, 0);
       +    if (flags == -1 || fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1
       +            || open(exname, O_WRONLY) == -1) {
       +        (void)close(fd);
       +        removeextern();
       +        return;
       +    }
       +
       +    estart(Eextern, fd, 8192);
       +    atexit(removeextern);
        #endif
        }
        
        /*
       - *        we have to supply a dummy exit function, because some vendors can't be
       - *        bothered to provide atexit().  we clean up the named pipes on a normal
       - *        exit, but leave them laying around on abnormal exits.
       + *  we have to supply a dummy exit function, because some vendors can't be
       + *  bothered to provide atexit().  we clean up the named pipes on a normal
       + *  exit, but leave them laying around on abnormal exits.
         */
        void
        exits(char *message)
        {
       -        if (exname) {
       -                unlink(exname);
       -                exname = 0;
       -        }
       -        if (message == 0)
       -                exit (0);
       -        else
       -                exit(1);
       +    if (exname) {
       +        unlink(exname);
       +        exname = 0;
       +    }
       +    if (message == 0)
       +        exit (0);
       +    else
       +        exit(1);
        }