added tabs to spaces code from mkhl

This commit is contained in:
Iris Lightshard 2020-11-17 14:26:25 -05:00
parent 770f1906a3
commit 110ffde515
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
7 changed files with 170 additions and 108 deletions

135
acme.c
View file

@ -78,7 +78,7 @@ threadmain(int argc, char *argv[])
}
break;
case 'a':
globalautoindent = TRUE;
globalindent[AUTOINDENT] = TRUE;
break;
case 'b':
bartflag = TRUE;
@ -101,6 +101,9 @@ threadmain(int argc, char *argv[])
if(fontnames[1] == nil)
goto Usage;
break;
case 'i':
globalindent[SPACESINDENT] = TRUE;
break;
case 'l':
loadfile = ARGF();
if(loadfile == nil)
@ -121,7 +124,7 @@ threadmain(int argc, char *argv[])
break;
default:
Usage:
fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
fprint(2, "usage: acme -aib -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
threadexitsall("usage");
}ARGEND
@ -968,69 +971,69 @@ Cursor boxcursor = {
Cursor2 boxcursor2 = {
{-15, -15},
{0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xC0, 0x03, 0xFF,
0xFF, 0xC0, 0x03, 0xFF,
0xFF, 0xC0, 0x03, 0xFF,
0xFF, 0xC0, 0x03, 0xFF,
0xFF, 0xC0, 0x03, 0xFF,
0xFF, 0xC0, 0x03, 0xFF,
0xFF, 0xC0, 0x03, 0xFF,
0xFF, 0xC0, 0x03, 0xFF,
0xFF, 0xC0, 0x03, 0xFF,
0xFF, 0xC0, 0x03, 0xFF,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 0x00, 0xFC,
0x3F, 0x00, 0x00, 0xFC,
0x3F, 0x00, 0x00, 0xFC,
0x3F, 0x00, 0x00, 0xFC,
0x3F, 0x00, 0x00, 0xFC,
0x3F, 0x00, 0x00, 0xFC,
0x3F, 0x00, 0x00, 0xFC,
0x3F, 0x00, 0x00, 0xFC,
0x3F, 0x00, 0x00, 0xFC,
0x3F, 0x00, 0x00, 0xFC,
0x3F, 0xFF, 0xFF, 0xFC,
0x3F, 0xFF, 0xFF, 0xFC,
0x3F, 0xFF, 0xFF, 0xFC,
0x3F, 0xFF, 0xFF, 0xFC,
0x3F, 0xFF, 0xFF, 0xFC,
0x3F, 0xFF, 0xFF, 0xFC,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00}
};
@ -1047,7 +1050,7 @@ iconinit(void)
tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x797979FF);
tagcols[TEXT] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x797979FF);
tagcols[HTEXT] = display->black;
/* Blue */
textcols[BACK] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x000F19FF);
textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x1F9B92FF);
@ -1055,7 +1058,7 @@ iconinit(void)
textcols[TEXT] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x93A1A1FF);
textcols[HTEXT] = display->black;
}
r = Rect(0, 0, Scrollwid+ButtonBorder, font->height+1);
if(button && eqrect(r, button->r))
return;

12
dat.h
View file

@ -229,6 +229,14 @@ void textsetselect(Text*, uint, uint);
void textshow(Text*, uint, uint, int);
void texttype(Text*, Rune);
enum
{
SPACESINDENT = 0,
AUTOINDENT,
NINDENT,
};
struct Window
{
QLock lk;
@ -240,7 +248,7 @@ struct Window
uchar isscratch;
uchar filemenu;
uchar dirty;
uchar autoindent;
uchar indent[NINDENT];
uchar showdel;
int id;
Range addr;
@ -552,7 +560,7 @@ extern char wdir[]; /* must use extern because no dimension given */
int editing;
int erroutfd;
int messagesize; /* negotiated in 9P version setup */
int globalautoindent;
int globalindent[NINDENT];
int dodollarsigns;
char* mtpt;

55
exec.c
View file

@ -92,6 +92,7 @@ static Rune LRedo[] = { 'R', 'e', 'd', 'o', 0 };
static Rune LSend[] = { 'S', 'e', 'n', 'd', 0 };
static Rune LSnarf[] = { 'S', 'n', 'a', 'r', 'f', 0 };
static Rune LSort[] = { 'S', 'o', 'r', 't', 0 };
static Rune LSpaces[] = { 'S', 'p', 'a', 'c', 'e', 's', 0 };
static Rune LTab[] = { 'T', 'a', 'b', 0 };
static Rune LUndo[] = { 'U', 'n', 'd', 'o', 0 };
static Rune LZerox[] = { 'Z', 'e', 'r', 'o', 'x', 0 };
@ -109,7 +110,7 @@ Exectab exectab[] = {
{ LGet, get, FALSE, TRUE, XXX },
{ LID, id, FALSE, XXX, XXX },
{ LIncl, incl, FALSE, XXX, XXX },
{ LIndent, indent, FALSE, XXX, XXX },
{ LIndent, indent, FALSE, AUTOINDENT, XXX },
{ LKill, xkill, FALSE, XXX, XXX },
{ LLoad, dump, FALSE, FALSE, XXX },
{ LLocal, local, FALSE, XXX, XXX },
@ -123,6 +124,7 @@ Exectab exectab[] = {
{ LSend, sendx, TRUE, XXX, XXX },
{ LSnarf, cut, FALSE, TRUE, FALSE },
{ LSort, sort, FALSE, XXX, XXX },
{ LSpaces, indent, FALSE, SPACESINDENT, XXX },
{ LTab, tab, FALSE, XXX, XXX },
{ LUndo, undo, FALSE, TRUE, XXX },
{ LZerox, zeroxx, FALSE, XXX, XXX },
@ -911,7 +913,7 @@ put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
warning(nil, "no file name\n");
return;
}
if(w->autoindent)
if(w->indent[NINDENT])
trimspaces(et);
namer = bytetorune(name, &nname);
putfile(f, 0, f->b.nc, namer, nname);
@ -1383,66 +1385,75 @@ incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
static Rune LON[] = { 'O', 'N', 0 };
static Rune LOFF[] = { 'O', 'F', 'F', 0 };
static Rune Lon[] = { 'o', 'n', 0 };
static Rune Loff[] = { 'o', 'f', 'f', 0 };
enum {
IGlobal = -2,
IError = -1,
Ion = 0,
Ioff = 1
};
static int
indentval(Rune *s, int n)
indentval(Rune *s, int n, int type)
{
static char *strs[] = {
[SPACESINDENT] = "Spaces",
[AUTOINDENT] = "Indent",
};
if(n < 2)
return IError;
if(runestrncmp(s, LON, n) == 0){
globalautoindent = TRUE;
warning(nil, "Indent ON\n");
globalindent[type] = TRUE;
warning(nil, "%s ON\n", strs[type]);
return IGlobal;
}
if(runestrncmp(s, LOFF, n) == 0){
globalautoindent = FALSE;
warning(nil, "Indent OFF\n");
globalindent[type] = FALSE;
warning(nil, "%s OFF\n", strs[type]);
return IGlobal;
}
return runestrncmp(s, Lon, n) == 0;
if(runestrncmp(s, Lon, n) == 0)
return TRUE;
if(runestrncmp(s, Loff, n) == 0)
return FALSE;
return IError;
}
static void
fixindent(Window *w, void *arg)
{
USED(arg);
w->autoindent = globalautoindent;
int t;
t = (int)arg;
w->indent[t] = globalindent[t];
}
void
indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
indent(Text *et, Text* _0, Text *argt, int type, int _1, Rune *arg, int narg)
{
Rune *a, *r;
Window *w;
int na, len, autoindent;
int na, len, ival;
USED(_0);
USED(_1);
USED(_2);
w = nil;
if(et!=nil && et->w!=nil)
w = et->w;
autoindent = IError;
ival = IError;
getarg(argt, FALSE, TRUE, &r, &len);
if(r!=nil && len>0)
autoindent = indentval(r, len);
ival = indentval(r, len, type);
else{
a = findbl(arg, narg, &na);
if(a != arg)
autoindent = indentval(arg, narg-na);
ival = indentval(arg, narg-na, type);
}
if(autoindent == IGlobal)
allwindows(fixindent, nil);
else if(w != nil && autoindent >= 0)
w->autoindent = autoindent;
if(ival == IGlobal)
allwindows(fixindent, (void*)type);
else if(w != nil && ival >= 0)
w->indent[type] = ival;
}
void

6
look.c
View file

@ -799,9 +799,11 @@ openfile(Text *t, Expand *e)
runemove(rp, ow->incl[i], n);
winaddincl(w, rp, n);
}
w->autoindent = ow->autoindent;
for(i=0; i < NINDENT; i++)
w->indent[i] = ow->indent[i];
}else
w->autoindent = globalautoindent;
for(i=0; i < NINDENT; i++)
w->indent[i] = globalindent[i];
xfidlog(w, "new");
}
if(e->a1 == e->a0)

39
text.c
View file

@ -532,6 +532,27 @@ textreadc(Text *t, uint q)
return r;
}
static int
spacesindentbswidth(Text *t)
{
uint q, col;
Rune r;
col = textbswidth(t, 0x15);
q = t->q0;
while(q > 0){
r = textreadc(t, q-1);
if(r != ' ')
break;
q--;
if(--col % t->tabstop == 0)
break;
}
if(t->q0 == q)
return 1;
return t->q0-q;
}
int
textbswidth(Text *t, Rune c)
{
@ -540,8 +561,11 @@ textbswidth(Text *t, Rune c)
int skipping;
/* there is known to be at least one character to erase */
if(c == 0x08) /* ^H: erase character */
if(c == 0x08){ /* ^H: erase character */
if(t->what == Body && t->w->indent[SPACESINDENT])
return spacesindentbswidth(t);
return 1;
}
q = t->q0;
skipping = TRUE;
while(q > 0){
@ -910,8 +934,19 @@ texttype(Text *t, Rune r)
textfill(t->file->text[i]);
t->iq1 = t->q0;
return;
case '\t':
if(t->what == Body && t->w->indent[SPACESINDENT]){
nnb = textbswidth(t, 0x15);
if(nnb == 1 && textreadc(t, t->q0-1) == '\n')
nnb = 0;
nnb = t->tabstop - nnb % t->tabstop;
rp = runemalloc(nnb);
for(nr = 0; nr < nnb; nr++)
rp[nr] = ' ';
}
break;
case '\n':
if(t->w->autoindent){
if(t->what == Body && t->w->indent[AUTOINDENT]){
/* find beginning of previous line using backspace code */
nnb = textbswidth(t, 0x15); /* ^U case */
rp = runemalloc(nnb + 1);

7
util.c
View file

@ -107,7 +107,8 @@ errorwin1(Rune *dir, int ndir, Rune **incl, int nincl)
runemove(r, incl[i], n);
winaddincl(w, r, n);
}
w->autoindent = globalautoindent;
for(i=0; i<NINDENT; i++)
w->indent[i] = globalindent[i];
return w;
}
@ -132,7 +133,7 @@ errorwin(Mntdir *md, int owner)
}
/*
* Incoming window should be locked.
* Incoming window should be locked.
* It will be unlocked and returned window
* will be locked in its place.
*/
@ -189,7 +190,7 @@ void
addwarningtext(Mntdir *md, Rune *r, int nr)
{
Warning *warn;
for(warn = warnings; warn; warn=warn->next){
if(warn->md == md){
bufinsert(&warn->buf, warn->buf.nc, r, nr);

24
wind.c
View file

@ -21,7 +21,7 @@ wininit(Window *w, Window *clone, Rectangle r)
File *f;
Reffont *rf;
Rune *rp;
int nc;
int nc, i;
w->tag.w = w;
w->taglines = 1;
@ -80,10 +80,12 @@ wininit(Window *w, Window *clone, Rectangle r)
draw(screen, br, button, nil, button->r.min);
w->filemenu = TRUE;
w->maxlines = w->body.fr.maxlines;
w->autoindent = globalautoindent;
for(i=0; i<NINDENT; i++)
w->indent[i] = globalindent[i];
if(clone){
w->dirty = clone->dirty;
w->autoindent = clone->autoindent;
for(i=0; i<NINDENT; i++)
w->indent[i] = clone->indent[i];
textsetselect(&w->body, clone->body.q0, clone->body.q1);
winsettag(w);
}
@ -97,7 +99,7 @@ windrawbutton(Window *w)
{
Image *b;
Rectangle br;
b = button;
if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
b = modbutton;
@ -112,7 +114,7 @@ delrunepos(Window *w)
{
int n;
Rune rune;
for(n=0; n<w->tag.file->b.nc; n++) {
bufread(&w->tag.file->b, n, &rune, 1);
if(rune == ' ')
@ -128,7 +130,7 @@ void
movetodel(Window *w)
{
int n;
n = delrunepos(w);
if(n < 0)
return;
@ -153,7 +155,7 @@ wintaglines(Window *w, Rectangle r)
textresize(&w->tag, r, TRUE);
w->tag.fr.noredraw = 0;
w->tagsafe = FALSE;
if(!w->tagexpand) {
/* use just as many lines as needed to show the Del */
n = delrunepos(w);
@ -162,7 +164,7 @@ wintaglines(Window *w, Rectangle r)
p = subpt(frptofchar(&w->tag.fr, n), w->tag.fr.r.min);
return 1 + p.y / w->tag.fr.font->height;
}
/* can't use more than we have */
if(w->tag.fr.nlines >= w->tag.fr.maxlines)
return w->tag.fr.maxlines;
@ -224,7 +226,7 @@ winresize(Window *w, Rectangle r, int safe, int keepextra)
moveto(mousectl, p);
}
}
/* If needed, resize & redraw body. */
r1 = r;
r1.min.y = y;
@ -293,7 +295,7 @@ winunlock(Window *w)
void
winmousebut(Window *w)
{
moveto(mousectl, addpt(w->tag.scrollr.min,
moveto(mousectl, addpt(w->tag.scrollr.min,
divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
}
@ -672,7 +674,7 @@ winctlprint(Window *w, char *buf, int fonts)
sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
w->body.file->b.nc, w->isdir, w->dirty);
if(fonts)
return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
w->body.reffont->f->name, w->body.fr.maxtab);
return buf;
}