[comp.editors] Simple Editor -- bug fixes

ant@mks.com (Anthony Howe) (06/07/91)

<< ae >>  Bug Fixes
===================

Hmmm, looks like I made a booboo or two.

1)

watmath!presto.bos.Camex.COM!ayk (Andrew Kobayashi) --
> It doesn't quite handle going from left-to-right over 
> the gap correctly.  The fixes, alas, make the program
> six bytes longer.  Without the fixes, whenever i did
> an "l" over the gap the cursor would move straight
> down.  A second "l" moved the cursor back up and over
> two characters, that is, to the correct position.

I figured out how to generate this.  The following keys strokes
demonstrate the bug.

ae foo
i					---- insert some text
some text on line 1^M			---- line 1
even more text on the next line^L	---- line 2
tll					---- sit on the 'm' in line 1
i^L					---- move the gap to just before 'm'
t					---- move point to top of file
ll					---- cursor ends up on 'v'
l					---- cursor ends up on 'e' in "some"

> Here's the fix:
> 
> R(){p=p-g?p:h;p<c&&++p;}
> 
> becomes
> 
> R(){p=p-(g-1)?p:h-1;p<c&&++p;}

I figured out what was wrong.  The point (p) should never really equal 
gap (g) except in transition from gap to egap (h).  What I did wrong
was to check for the gap before I moved the point.  It should actually be
the other way around.  My fix is

R(){p<c&&++p;p=p-g?p:h;}

which has the same number of characters as the orginal :)


2)

I found this bug when I unobfuscated the source for several people.
This visual bug occurs when you create an empty file.

ae foo

The cursor will end up sitting on the "<< EOF >>" string until the
first insert command.


Again thanks for everyone's kind remarks and inquiries.  Below is an
updated version of the source.

- ant
------->8--------
#include <curses.h>
#define Z(x) (x==' '||x=='\t'||x=='\n')
int a,d,i,q=1,x,y;char b[BUF],*c,*g=b,*h,*p,*m=b,*n,*f,
k[]="hjklHJKL[]tbixWRQ";S(){p=b<g?b:h;}T(){p=c;}Q(){q=0;}P(){clear();Y();}
V(){while(p<g)*--h= *--g;while(h<p)*g++= *h++;p=h;}
char*M(s)char*s;{while(b<(s=s-h?s:g)&&*--s!='\n');return b<s?s+1:b;}
char*N(s)char*s;{while((s=s-g?s:h)<c&&*s++!='\n');return s<c?s:c;}
A(){for(i=0,p=p-g?p:h;p<c&&*p!='\n'&&i<x;i+= *p-'\t'?1:8-(i&7),R());}
L(){p=h-p?p:g;b<p&&--p;--y;}R(){p<c&&++p;p=p-g?p:h;}U(){p=M(M(p)-1);A();--y;}
D(){p=N(p);A();}H(){p=M(p);}E(){p=N(p);L();}
B(){while(!Z(*p)&&b<p)L();while(Z(*p)&&b<p)L();}
J(){m=p=M(M(n)-1);while(y--)p=N(p);A();n=c;}
K(){for(i=d;--i;m=M(M(m)-1),p=M(M(p)-1));A();}
W(){while(!Z(*p)&&p<c)R();while(Z(*p)&&p<c)R();}
I(){V();while((a=getch())-'\f'){if(a-'\b')h-g&&(*g++=a-'\r'?a:'\n');
else b<g&&--g;Y();}}X(){V();p=h<c?++h:p;}
F(){p=b;V();write(i=creat(f,MODE),h,(int)(c-h));close(i);}
(*z[])()={L,D,U,R,B,J,K,W,H,E,S,T,I,X,F,P,Q,V};
Y(){if(p<m)m=M(p);if(n<=p||++y==d)for(m=N(p),i=m-c?d:d-2;i--;m=M(M(m)-1));
move(0,0);for(i=a=0,n=m;;){n=n-g?n:h;n-p||(y=i,x=a);if(d<=i||c<=n)break;
*n-'\r'&&(addch(*n),a+= *n-'\t'?1:8-(a&7));if(*n++=='\n'||COLS<=a)++i,a=0;}
clrtobot();++i<d&&mvaddstr(i,0,"<< EOF >>");move(y,x);refresh();}
main(u,v)char**v;{h=n=c=b+BUF;if(u<2)return 2;initscr();d=LINES;raw();
noecho();idlok(stdscr,1);
if(0<(i=open(f= *++v,0)))g+=read(i,b,BUF),g=g<b?b:g,close(i);T();while(q){
Y();for(a=getch(),i=0;k[i]&&a!=k[i];++i);(*z[i])();}endwin();return 0;}
-- 
ant@mks.com                                                   Anthony C Howe 
Mortice Kern Systems Inc. 35 King St. N., Waterloo, Ontario, Canada, N2J 6W9
"Fate favors fools, small children, and ships named Enterprise" - Riker

ant@mks.com (Anthony Howe) (06/07/91)

watmath!sscds1.ssc.gov!kmdamra (Ken Damrau)
> In your corrections, shouldn't it be an S() before the while in main
> instead of the T()?

You are correct.  This is a perfect example of the obfuscated version being
out of sync with the unobfuscated, which is way I eventually did all
the fine tuning with the obfuscated version for the contest.

Thanks for catching it.

-ant
-- 
ant@mks.com                                                   Anthony C Howe 
Mortice Kern Systems Inc. 35 King St. N., Waterloo, Ontario, Canada, N2J 6W9
"Fate favors fools, small children, and ships named Enterprise" - Riker