sam, samterm: add -i flag to make tab insert spaces up to the next tabstop and backspace to delete spaces until the previous tabstop

This commit is contained in:
ppatience0 2013-04-04 16:38:36 -04:00
parent 6bef56f037
commit 02f60df0f3
4 changed files with 45 additions and 3 deletions

View file

@ -64,6 +64,9 @@ void main(int argc, char *argv[])
case 'a':
*ap++ = "-a";
break;
case 'i':
*ap++ = "-i";
break;
}ARGEND
*ap = nil;

View file

@ -24,6 +24,7 @@ char hostlock = 1;
char hasunlocked = 0;
int maxtab = 8;
int autoindent;
int spacesindent;
void
threadmain(int argc, char *argv[])
@ -348,6 +349,33 @@ raspc(Rasp *r, long p)
return 0;
}
int
getcol(Rasp *r, long p)
{
int col;
for(col = 0; p > 0 && raspc(r, p-1)!='\n'; p--, col++)
;
return col;
}
long
del(Rasp *r, long o, long p)
{
int i, col, n;
if(--p < o)
return o;
if(!spacesindent || raspc(r, p)!=' ')
return p;
col = getcol(r, p) + 1;
if((n = col % maxtab) == 0)
n = maxtab;
for(i = 0; p-1>=o && raspc(r, p-1)==' ' && i<n-1; --p, i++)
;
return p>=o? p : o;
}
long
ctlw(Rasp *r, long o, long p)
{
@ -506,7 +534,14 @@ type(Flayer *l, int res) /* what a bloody mess this is */
break;
}
}
*p++ = c;
if(spacesindent && c == '\t'){
int i, col, n;
col = getcol(&t->rasp, a);
n = maxtab - col % maxtab;
for(i = 0; i < n && p < buf+nelem(buf); i++)
*p++ = ' ';
} else
*p++ = c;
if(autoindent)
if(c == '\n'){
/* autoindent */
@ -587,7 +622,7 @@ type(Flayer *l, int res) /* what a bloody mess this is */
switch(c){
case '\b':
case 0x7F: /* del */
l->p0 = a-1;
l->p0 = del(&t->rasp, l->origin, a);
break;
case 0x15: /* ctrl-u */
l->p0 = ctlu(&t->rasp, l->origin, a);

View file

@ -14,7 +14,7 @@ static char exname[64];
void
usage(void)
{
fprint(2, "usage: samterm [-a]\n");
fprint(2, "usage: samterm [-ai]\n");
threadexitsall("usage");
}
@ -27,6 +27,9 @@ getscreen(int argc, char **argv)
case 'a':
autoindent = 1;
break;
case 'i':
spacesindent = 1;
break;
default:
usage();
}ARGEND

View file

@ -86,6 +86,7 @@ extern int hversion;
extern int plumbfd;
extern int exiting;
extern int autoindent;
extern int spacesindent;
Rune *gettext(Flayer*, long, ulong*);
void *alloc(ulong n);