aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/vi.c
diff options
context:
space:
mode:
Diffstat (limited to 'toys/pending/vi.c')
-rw-r--r--toys/pending/vi.c517
1 files changed, 251 insertions, 266 deletions
diff --git a/toys/pending/vi.c b/toys/pending/vi.c
index 2fb6d442..9c47a4c5 100644
--- a/toys/pending/vi.c
+++ b/toys/pending/vi.c
@@ -36,28 +36,26 @@ GLOBALS(
char *last_search;
int tabstop;
int list;
+ struct str_line {
+ int alloc;
+ int len;
+ char *data;
+ } *il;
+ struct linelist {
+ struct linelist *up;//next
+ struct linelist *down;//prev
+ struct str_line *line;
+ } *text, *screen, *c_r;
+ //yank buffer
+ struct yank_buf {
+ char reg;
+ int alloc;
+ char* data;
+ } yank;
)
-struct str_line {
- int alloc;
- int len;
- char *data;
-};
-//yank buffer
-struct yank_buf {
- char reg;
- int alloc;
- char* data;
-};
-//lib dllist uses next and prev kinda opposite what im used to so I just
-//renamed both ends to up and down
-struct linelist {
- struct linelist *up;//next
- struct linelist *down;//prev
- struct str_line *line;
-};
static void draw_page();
@@ -79,13 +77,7 @@ static int search_str(char *s);
static int vi_yank(char reg, struct linelist *row, int col, int flags);
static int vi_delete(char reg, struct linelist *row, int col, int flags);
-//inserted line not yet pushed to buffer
-struct str_line *il;
-struct linelist *text; //file loaded into buffer
-struct linelist *screen;//current screen coord 0 row
-struct linelist *c_r;//cursor position row
-struct yank_buf yank; //single yank
// TT.vi_mov_flag is used for special cases when certain move
// acts differently depending is there DELETE/YANK or NOP
@@ -128,15 +120,15 @@ void linelist_free(void *node)
void linelist_unload()
{
void* list = 0;
- for (;text->down; text = text->down);
- list = (void*)text;
- text = screen = c_r = 0;
+ for (;TT.text->down; TT.text = TT.text->down);
+ list = (void*)TT.text;
+ TT.text = TT.screen = TT.c_r = 0;
llist_traverse(list, linelist_free);
}
void write_file(char *filename)
{
- struct linelist *lst = text;
+ struct linelist *lst = TT.text;
FILE *fp = 0;
if (!filename) filename = (char*)*toys.optargs;
if (!(fp = fopen(filename, "w")) ) return;
@@ -149,7 +141,7 @@ void write_file(char *filename)
int linelist_load(char *filename)
{
- struct linelist *lst = c_r;//cursor position or 0
+ struct linelist *lst = TT.c_r;//cursor position or 0
FILE *fp = 0;
if (!filename)
filename = (char*)*toys.optargs;
@@ -163,8 +155,8 @@ int linelist_load(char *filename)
lst->line->alloc = alc;
lst->line->len = 0;
lst->line->data = line;
- text = lst;
- dlist_terminate(text->up);
+ TT.text = lst;
+ dlist_terminate(TT.text->up);
return 1;
}
@@ -189,10 +181,10 @@ int linelist_load(char *filename)
lst->line->data[len-1] = 0;
lst->line->len--;
}
- if (text == 0) text = lst;
+ if (TT.text == 0) TT.text = lst;
}
- if (text) dlist_terminate(text->up);
+ if (TT.text) dlist_terminate(TT.text->up);
fclose(fp);
return 1;
@@ -201,7 +193,7 @@ int linelist_load(char *filename)
int vi_yy(char reg, int count0, int count1)
{
- struct linelist *pos = c_r;
+ struct linelist *pos = TT.c_r;
int col = TT.cur_col;
TT.cur_col = 0;
TT.vi_mov_flag |= 0x4;
@@ -210,13 +202,13 @@ int vi_yy(char reg, int count0, int count1)
vi_yank(reg, pos, 0, 0);
- TT.cur_col = col, c_r = pos;
+ TT.cur_col = col, TT.c_r = pos;
return 1;
}
int vi_dd(char reg, int count0, int count1)
{
- struct linelist *pos = c_r;
+ struct linelist *pos = TT.c_r;
TT.cur_col = 0;
TT.vi_mov_flag |= 0x4;
if (count0>1) cur_down(count0-1, 1, 0);
@@ -230,12 +222,12 @@ static int vi_x(char reg, int count0, int count1)
{
char *last = 0, *cpos = 0, *start = 0;
int len = 0;
- struct linelist *pos = c_r;
+ struct linelist *pos = TT.c_r;
int col = TT.cur_col;
- if (!c_r) return 0;
+ if (!TT.c_r) return 0;
- start = c_r->line->data;
- len = c_r->line->len;
+ start = TT.c_r->line->data;
+ len = TT.c_r->line->len;
last = utf8_last(start, len);
cpos = start+TT.cur_col;
@@ -262,35 +254,35 @@ int vi_movw(int count0, int count1, char* unused)
const char *empties = " \t\n\r";
const char *specials = ",.=-+*/(){}<>[]";
// char *current = 0;
- if (!c_r)
+ if (!TT.c_r)
return 0;
- if (TT.cur_col == c_r->line->len-1 || !c_r->line->len)
+ if (TT.cur_col == TT.c_r->line->len-1 || !TT.c_r->line->len)
goto next_line;
- if (strchr(empties, c_r->line->data[TT.cur_col]))
+ if (strchr(empties, TT.c_r->line->data[TT.cur_col]))
goto find_non_empty;
- if (strchr(specials, c_r->line->data[TT.cur_col])) {
- for (;strchr(specials, c_r->line->data[TT.cur_col]); ) {
+ if (strchr(specials, TT.c_r->line->data[TT.cur_col])) {
+ for (;strchr(specials, TT.c_r->line->data[TT.cur_col]); ) {
TT.cur_col++;
- if (TT.cur_col == c_r->line->len-1)
+ if (TT.cur_col == TT.c_r->line->len-1)
goto next_line;
}
- } else for (;!strchr(specials, c_r->line->data[TT.cur_col]) &&
- !strchr(empties, c_r->line->data[TT.cur_col]);) {
+ } else for (;!strchr(specials, TT.c_r->line->data[TT.cur_col]) &&
+ !strchr(empties, TT.c_r->line->data[TT.cur_col]);) {
TT.cur_col++;
- if (TT.cur_col == c_r->line->len-1)
+ if (TT.cur_col == TT.c_r->line->len-1)
goto next_line;
}
- for (;strchr(empties, c_r->line->data[TT.cur_col]); ) {
+ for (;strchr(empties, TT.c_r->line->data[TT.cur_col]); ) {
TT.cur_col++;
find_non_empty:
- if (TT.cur_col == c_r->line->len-1) {
+ if (TT.cur_col == TT.c_r->line->len-1) {
next_line:
//we could call j and g0
- if (!c_r->down) return 0;
- c_r = c_r->down;
+ if (!TT.c_r->down) return 0;
+ TT.c_r = TT.c_r->down;
TT.cur_col = 0;
- if (!c_r->line->len) break;
+ if (!TT.c_r->line->len) break;
}
}
count--;
@@ -304,21 +296,21 @@ next_line:
static int vi_movb(int count0, int count1, char* unused)
{
int count = count0*count1;
- if (!c_r)
+ if (!TT.c_r)
return 0;
if (!TT.cur_col) {
- if (!c_r->up) return 0;
- c_r = c_r->up;
- TT.cur_col = (c_r->line->len) ? c_r->line->len-1 : 0;
+ if (!TT.c_r->up) return 0;
+ TT.c_r = TT.c_r->up;
+ TT.cur_col = (TT.c_r->line->len) ? TT.c_r->line->len-1 : 0;
goto exit_function;
}
if (TT.cur_col)
TT.cur_col--;
- while (c_r->line->data[TT.cur_col] <= ' ') {
+ while (TT.c_r->line->data[TT.cur_col] <= ' ') {
if (TT.cur_col) TT.cur_col--;
else goto exit_function;
}
- while (c_r->line->data[TT.cur_col] > ' ') {
+ while (TT.c_r->line->data[TT.cur_col] > ' ') {
if (TT.cur_col)TT.cur_col--;
else goto exit_function;
}
@@ -335,13 +327,13 @@ exit_function:
static int vi_move(int count0, int count1, char *unused)
{
int count = count0*count1;
- if (!c_r)
+ if (!TT.c_r)
return 0;
- if (TT.cur_col < c_r->line->len)
+ if (TT.cur_col < TT.c_r->line->len)
TT.cur_col++;
- if (c_r->line->data[TT.cur_col] <= ' ' || count > 1)
+ if (TT.c_r->line->data[TT.cur_col] <= ' ' || count > 1)
vi_movw(count, 1, 0); //find next word;
- while (c_r->line->data[TT.cur_col] > ' ')
+ while (TT.c_r->line->data[TT.cur_col] > ' ')
TT.cur_col++;
if (TT.cur_col) TT.cur_col--;
@@ -353,27 +345,27 @@ static int vi_move(int count0, int count1, char *unused)
static void i_insert(char* str, int len)
{
- char *t = xzalloc(c_r->line->alloc);
- char *s = c_r->line->data;
- int sel = c_r->line->len-TT.cur_col;
+ char *t = xzalloc(TT.c_r->line->alloc);
+ char *s = TT.c_r->line->data;
+ int sel = TT.c_r->line->len-TT.cur_col;
strncpy(t, &s[TT.cur_col], sel);
t[sel+1] = 0;
- if (c_r->line->alloc < c_r->line->len+len+5) {
- c_r->line->data = xrealloc(c_r->line->data,
- (c_r->line->alloc+len)<<1);
+ if (TT.c_r->line->alloc < TT.c_r->line->len+len+5) {
+ TT.c_r->line->data = xrealloc(TT.c_r->line->data,
+ (TT.c_r->line->alloc+len)<<1);
- c_r->line->alloc = (c_r->line->alloc+len)<<1;
- memset(&c_r->line->data[c_r->line->len], 0,
- c_r->line->alloc-c_r->line->len);
+ TT.c_r->line->alloc = (TT.c_r->line->alloc+len)<<1;
+ memset(&TT.c_r->line->data[TT.c_r->line->len], 0,
+ TT.c_r->line->alloc-TT.c_r->line->len);
- s = c_r->line->data;
+ s = TT.c_r->line->data;
}
strncpy(&s[TT.cur_col], str, len);
strcpy(&s[TT.cur_col+len], t);
TT.cur_col += len;
if (TT.cur_col) TT.cur_col--;
- c_r->line->len += len;
+ TT.c_r->line->len += len;
free(t);
TT.vi_mov_flag |= 0x30000000;
@@ -384,28 +376,28 @@ void i_split()
{
int alloc = 0, len = 0, idx = 0;
struct str_line *l = xmalloc(sizeof(struct str_line));
- alloc = c_r->line->alloc;
+ alloc = TT.c_r->line->alloc;
- if (TT.cur_col) len = c_r->line->len-TT.cur_col-1;
- else len = c_r->line->len;
+ if (TT.cur_col) len = TT.c_r->line->len-TT.cur_col-1;
+ else len = TT.c_r->line->len;
if (len < 0) len = 0;
l->data = xzalloc(alloc);
l->alloc = alloc;
l->len = len;
- idx = c_r->line->len - len;
+ idx = TT.c_r->line->len - len;
- strncpy(l->data, &c_r->line->data[idx], len);
+ strncpy(l->data, &TT.c_r->line->data[idx], len);
memset(&l->data[len], 0, alloc-len);
- c_r->line->len -= len;
- if (c_r->line->len <= 0) c_r->line->len = 0;
+ TT.c_r->line->len -= len;
+ if (TT.c_r->line->len <= 0) TT.c_r->line->len = 0;
- len = c_r->line->len;
+ len = TT.c_r->line->len;
- memset(&c_r->line->data[len], 0, alloc-len);
- c_r = (struct linelist*)dlist_insert((struct double_list**)&c_r, (char*)l);
- c_r->line = l;
+ memset(&TT.c_r->line->data[len], 0, alloc-len);
+ TT.c_r = (struct linelist*)dlist_insert((struct double_list**)&TT.c_r, (char*)l);
+ TT.c_r->line = l;
TT.cur_col = 0;
}
@@ -420,11 +412,11 @@ static int vi_zero(int count0, int count1, char *unused)
static int vi_eol(int count0, int count1, char *unused)
{
int count = count0*count1;
- for (;count > 1 && c_r->down; count--)
- c_r = c_r->down;
+ for (;count > 1 && TT.c_r->down; count--)
+ TT.c_r = TT.c_r->down;
- if (c_r && c_r->line->len)
- TT.cur_col = c_r->line->len-1;
+ if (TT.c_r && TT.c_r->line->len)
+ TT.cur_col = TT.c_r->line->len-1;
TT.vi_mov_flag |= 2;
check_cursor_bounds();
return 1;
@@ -433,14 +425,14 @@ static int vi_eol(int count0, int count1, char *unused)
//TODO check register where to push from
static int vi_push(char reg, int count0, int count1)
{
- char *start = yank.data, *end = yank.data+strlen(yank.data);
- struct linelist *cursor = c_r;
+ char *start = TT.yank.data, *end = TT.yank.data+strlen(TT.yank.data);
+ struct linelist *cursor = TT.c_r;
int col = TT.cur_col;
//insert into new lines
if (*(end-1) == '\n') for (;start != end;) {
TT.vi_mov_flag |= 0x10000000;
char *next = strchr(start, '\n');
- TT.cur_col = (c_r->line->len) ? c_r->line->len-1: 0;
+ TT.cur_col = (TT.c_r->line->len) ? TT.c_r->line->len-1: 0;
i_split();
if (next) {
i_insert(start, next-start);
@@ -463,7 +455,7 @@ static int vi_push(char reg, int count0, int count1)
}
//if row changes during push original cursor position is kept
//vi inconsistancy
- if (c_r != cursor) c_r = cursor, TT.cur_col = col;
+ if (TT.c_r != cursor) TT.c_r = cursor, TT.cur_col = col;
return 1;
}
@@ -471,11 +463,11 @@ static int vi_push(char reg, int count0, int count1)
static int vi_find_c(int count0, int count1, char *symbol)
{
int count = count0*count1;
- if (c_r && c_r->line->len) {
+ if (TT.c_r && TT.c_r->line->len) {
while (count--) {
- char* pos = strstr(&c_r->line->data[TT.cur_col], symbol);
+ char* pos = strstr(&TT.c_r->line->data[TT.cur_col], symbol);
if (pos) {
- TT.cur_col = pos-c_r->line->data;
+ TT.cur_col = pos-TT.c_r->line->data;
return 1;
}
}
@@ -493,10 +485,10 @@ static int vi_find_cb(int count0, int count1, char *symbol)
static int vi_go(int count0, int count1, char *symbol)
{
int prev_row = TT.cur_row;
- c_r = text;
+ TT.c_r = TT.text;
- if (TT.vi_mov_flag&0x40000000) for (;c_r && c_r->down; c_r = c_r->down);
- else for (;c_r && c_r->down && --count0; c_r = c_r->down);
+ if (TT.vi_mov_flag&0x40000000) for (;TT.c_r && TT.c_r->down; TT.c_r = TT.c_r->down);
+ else for (;TT.c_r && TT.c_r->down && --count0; TT.c_r = TT.c_r->down);
TT.cur_col = 0;
check_cursor_bounds(); //adjusts cursor column
@@ -505,7 +497,6 @@ static int vi_go(int count0, int count1, char *symbol)
return 1;
}
-//need to refactor when implementing yank buffers
static int vi_delete(char reg, struct linelist *row, int col, int flags)
{
struct linelist *start = 0, *end = 0;
@@ -514,51 +505,62 @@ static int vi_delete(char reg, struct linelist *row, int col, int flags)
vi_yank(reg, row, col, flags);
if (TT.vi_mov_flag&0x80000000) {
- start = c_r, end = row;
+ start = TT.c_r, end = row;
col_s = TT.cur_col, col_e = col;
} else {
- start = row, end = c_r;
+ start = row, end = TT.c_r;
col_s = col, col_e = TT.cur_col;
}
- if (start == end) goto last_line_delete;
- if (!col_s) goto full_line_delete;
-
- memset(start->line->data+col_s, 0, start->line->len-col_s);
- row->line->len = col_s;
- col_s = 0;
- start = start->down;
-
-full_line_delete:
- TT.vi_mov_flag |= 0x10000000;
- for (;start != end;) {
- struct linelist* lst = start;
- //struct linelist *lst = dlist_pop(&start);
- start = start->down;
- if (lst->down) lst->down->up = lst->up;
- if (lst->up) lst->up->down = lst->down;
- if (screen == lst) screen = lst->down ? lst->down : lst->up;
- if (text == lst) text = lst->down;
- free(lst->line->data);
- free(lst->line);
- free(lst);
+ if (TT.vi_mov_flag&2) {
+ int len, width;
+ char *s = end->line->data;
+ len = utf8_lnw(&width, s+col_e, strlen(s+col_e));
+ for (;;) {
+ col_e += len;
+ len = utf8_lnw(&width, s+col_e, strlen(s+col_e));
+ if (len<1 || width || !(*(s+col_e))) break;
+ }
}
-last_line_delete:
+ if (start != end) { //goto last_line_delete;
+ if (col_s) {
+ memset(start->line->data+col_s, 0, start->line->len-col_s);
+ row->line->len = col_s;
+ col_s = 0;
+ start = start->down;
+ }
+ //full_line_delete:
+ TT.vi_mov_flag |= 0x10000000;
+ for (;start != end;) {
+ struct linelist *lst = start;
+ start = start->down;
+ lst = dlist_pop(&lst);
+ if (TT.screen == lst) TT.screen = start;
+ if (TT.text == lst) TT.text = start;
+ free(lst->line->data);
+ free(lst->line);
+ free(lst);
+ }
+ }
+//last_line_delete:
TT.vi_mov_flag |= 0x10000000;
- if (TT.vi_mov_flag&2) col_e = start->line->len;
+ //if (TT.vi_mov_flag&2) col_e = start->line->len;
if (TT.vi_mov_flag&4) {
if (!end->down && !end->up)
col_e = start->line->len;
else {
+ struct linelist *lst = 0;
col_e = 0, col_s = 0;
- if (end->down) end->down->up = end->up;
- if (end->up) end->up->down = end->down;
- if (screen == end) screen = end->down ? end->down : end->up;
- //if (text == end) text = end->down;
- start = end->down ? end->down : end->up;
- free(end->line->data);
- free(end->line);
- free(end);
-
+ if (!start->down) lst = dlist_pop(&start);
+ else {
+ lst = start;
+ start = start->down;
+ lst = dlist_pop(&lst);
+ }
+ if (TT.screen == lst) TT.screen = start;
+ if (TT.text == lst) TT.text = start;
+ free(lst->line->data);
+ free(lst->line);
+ free(lst);
}
}
if (col_s < col_e) {
@@ -568,7 +570,7 @@ last_line_delete:
memset(start->line->data+bytes, 0, start->line->len-bytes);
start->line->len = bytes;
}
- c_r = start;
+ TT.c_r = start;
TT.cur_col = col_s;
return 1;
}
@@ -576,13 +578,13 @@ last_line_delete:
static int vi_D(char reg, int count0, int count1)
{
int prev_col = TT.cur_col;
- struct linelist *pos = c_r;
+ struct linelist *pos = TT.c_r;
if (!count0) return 1;
vi_eol(1, 1, 0);
vi_delete(reg, pos, prev_col, 0);
count0--;
- if (count0 && c_r->down) {
- c_r = c_r->down;
+ if (count0 && TT.c_r->down) {
+ TT.c_r = TT.c_r->down;
vi_dd(reg, count0, 1);
}
check_cursor_bounds();
@@ -592,34 +594,34 @@ static int vi_D(char reg, int count0, int count1)
static int vi_join(char reg, int count0, int count1)
{
while (count0--) {
- if (c_r && c_r->down) {
- int size = c_r->line->len+c_r->down->line->len;
- if (size > c_r->line->alloc) {
- if (size > c_r->down->line->alloc) {
- c_r->line->data = xrealloc(c_r->line->data,
- c_r->line->alloc*2+il->alloc*2);
- memmove(&c_r->line->data[c_r->line->len],
- c_r->down->line->data,c_r->down->line->len);
- c_r->line->len = size;
- c_r = c_r->down;
- c_r->line->alloc = c_r->line->alloc*2+2*il->alloc;
+ if (TT.c_r && TT.c_r->down) {
+ int size = TT.c_r->line->len+TT.c_r->down->line->len;
+ if (size > TT.c_r->line->alloc) {
+ if (size > TT.c_r->down->line->alloc) {
+ TT.c_r->line->data = xrealloc(TT.c_r->line->data,
+ TT.c_r->line->alloc*2+TT.il->alloc*2);
+ memmove(&TT.c_r->line->data[TT.c_r->line->len],
+ TT.c_r->down->line->data,TT.c_r->down->line->len);
+ TT.c_r->line->len = size;
+ TT.c_r = TT.c_r->down;
+ TT.c_r->line->alloc = TT.c_r->line->alloc*2+2*TT.il->alloc;
vi_dd(0,1,1);
} else {
- memmove(&c_r->down->line->data[c_r->line->len],
- c_r->down->line->data,c_r->down->line->len);
- memmove(c_r->down->line->data,c_r->line->data,
- c_r->line->len);
- c_r->down->line->len = size;
+ memmove(&TT.c_r->down->line->data[TT.c_r->line->len],
+ TT.c_r->down->line->data,TT.c_r->down->line->len);
+ memmove(TT.c_r->down->line->data,TT.c_r->line->data,
+ TT.c_r->line->len);
+ TT.c_r->down->line->len = size;
vi_dd(0,1,1);
}
} else {
- memmove(&c_r->line->data[c_r->line->len],
- c_r->down->line->data,c_r->down->line->len);
- c_r->line->len = size;
- c_r = c_r->down;
+ memmove(&TT.c_r->line->data[TT.c_r->line->len],
+ TT.c_r->down->line->data,TT.c_r->down->line->len);
+ TT.c_r->line->len = size;
+ TT.c_r = TT.c_r->down;
vi_dd(0,1,1);
}
- c_r = c_r->up;
+ TT.c_r = TT.c_r->up;
}
}
@@ -646,45 +648,45 @@ static int vi_yank(char reg, struct linelist *row, int col, int flags)
struct linelist *start = 0, *end = 0;
int col_s = 0, col_e = 0, bytes = 0;
- memset(yank.data, 0, yank.alloc);
+ memset(TT.yank.data, 0, TT.yank.alloc);
if (TT.vi_mov_flag&0x80000000) {
- start = c_r, end = row;
+ start = TT.c_r, end = row;
col_s = TT.cur_col, col_e = col;
} else {
- start = row, end = c_r;
+ start = row, end = TT.c_r;
col_s = col, col_e = TT.cur_col;
}
if (start == end) goto last_line_yank;
if (!col_s) goto full_line_yank;
- if (yank.alloc < start->line->alloc) {
- yank.data = xrealloc(yank.data, start->line->alloc*2);
- yank.alloc = start->line->alloc*2;
+ if (TT.yank.alloc < start->line->alloc) {
+ TT.yank.data = xrealloc(TT.yank.data, start->line->alloc*2);
+ TT.yank.alloc = start->line->alloc*2;
}
- sprintf(yank.data, "%s\n", start->line->data+col_s);
+ sprintf(TT.yank.data, "%s\n", start->line->data+col_s);
col_s = 0;
start = start->down;
full_line_yank:
for (;start != end;) {
- while (yank.alloc-1 < strlen(yank.data)+start->line->len)
- yank.data = xrealloc(yank.data, yank.alloc*2), yank.alloc *= 2;
+ while (TT.yank.alloc-1 < strlen(TT.yank.data)+start->line->len)
+ TT.yank.data = xrealloc(TT.yank.data, TT.yank.alloc*2), TT.yank.alloc *= 2;
- sprintf(yank.data+strlen(yank.data), "%s\n", start->line->data);
+ sprintf(TT.yank.data+strlen(TT.yank.data), "%s\n", start->line->data);
start = start->down;
}
last_line_yank:
- while (yank.alloc-1 < strlen(yank.data)+end->line->len)
- yank.data = xrealloc(yank.data, yank.alloc*2), yank.alloc *= 2;
+ while (TT.yank.alloc-1 < strlen(TT.yank.data)+end->line->len)
+ TT.yank.data = xrealloc(TT.yank.data, TT.yank.alloc*2), TT.yank.alloc *= 2;
if (TT.vi_mov_flag & 0x4)
- sprintf(yank.data+strlen(yank.data), "%s\n", start->line->data);
+ sprintf(TT.yank.data+strlen(TT.yank.data), "%s\n", start->line->data);
else {
- bytes = strlen(yank.data)+col_e-col_s;
- strncpy(yank.data+strlen(yank.data), end->line->data+col_s, col_e-col_s);
- yank.data[bytes] = 0;
+ bytes = strlen(TT.yank.data)+col_e-col_s;
+ strncpy(TT.yank.data+strlen(TT.yank.data), end->line->data+col_s, col_e-col_s);
+ TT.yank.data[bytes] = 0;
}
return 1;
}
@@ -810,7 +812,7 @@ int run_vi_cmd(char *cmd)
}
if (vi_mov) {
int prev_col = TT.cur_col;
- struct linelist *pos = c_r;
+ struct linelist *pos = TT.c_r;
if (vi_mov(TT.count0, TT.count1, cmd)) {
if (vi_cmd) return (vi_cmd(TT.vi_reg, pos, prev_col, TT.vi_mov_flag));
else return 1;
@@ -821,8 +823,8 @@ int run_vi_cmd(char *cmd)
static int search_str(char *s)
{
- struct linelist *lst = c_r;
- char *c = strstr(&c_r->line->data[TT.cur_col+1], s);
+ struct linelist *lst = TT.c_r;
+ char *c = strstr(&TT.c_r->line->data[TT.cur_col+1], s);
if (TT.last_search != s) {
free(TT.last_search);
@@ -830,14 +832,14 @@ static int search_str(char *s)
}
if (c) {
- TT.cur_col = c-c_r->line->data;
+ TT.cur_col = c-TT.c_r->line->data;
} else for (; !c;) {
lst = lst->down;
if (!lst) return 1;
c = strstr(lst->line->data, s);
}
- c_r = lst;
- TT.cur_col = c-c_r->line->data;
+ TT.c_r = lst;
+ TT.cur_col = c-TT.c_r->line->data;
check_cursor_bounds();
return 0;
}
@@ -884,21 +886,21 @@ void vi_main(void)
char utf8_code[8] = {0};
int utf8_dec_p = 0, vi_buf_pos = 0;
- il = xzalloc(sizeof(struct str_line));
- il->data = xzalloc(80);
- yank.data = xzalloc(128);
+ TT.il = xzalloc(sizeof(struct str_line));
+ TT.il->data = xzalloc(80);
+ TT.yank.data = xzalloc(128);
- il->alloc = 80, yank.alloc = 128;
+ TT.il->alloc = 80, TT.yank.alloc = 128;
linelist_load(0);
- screen = c_r = text;
+ TT.screen = TT.c_r = TT.text;
TT.vi_mov_flag = 0x20000000;
TT.vi_mode = 1, TT.tabstop = 8;
TT.screen_width = 80, TT.screen_height = 24;
terminal_size(&TT.screen_width, &TT.screen_height);
- TT.screen_height -= 2; //TODO this is hack fix visual alignment
+ TT.screen_height -= 1;
set_terminal(0, 1, 0, 0);
//writes stdout into different xterm buffer so when we exit
@@ -915,7 +917,7 @@ void vi_main(void)
if (key == -1) goto cleanup_vi;
terminal_size(&TT.screen_width, &TT.screen_height);
- TT.screen_height -= 2; //TODO this is hack fix visual alignment
+ TT.screen_height -= 1; //TODO this is hack fix visual alignment
// TODO: support cursor keys in ex mode too.
if (TT.vi_mode && key>=256) {
@@ -934,14 +936,14 @@ void vi_main(void)
case '?':
case ':':
TT.vi_mode = 0;
- il->data[0]=key;
- il->len++;
+ TT.il->data[0]=key;
+ TT.il->len++;
break;
case 'A':
vi_eol(1, 1, 0);
// FALLTHROUGH
case 'a':
- if (c_r && c_r->line->len) TT.cur_col++;
+ if (TT.c_r && TT.c_r->line->len) TT.cur_col++;
// FALLTHROUGH
case 'i':
TT.vi_mode = 2;
@@ -971,69 +973,69 @@ void vi_main(void)
switch (key) {
case 0x7F:
case 0x08:
- if (il->len > 1) {
- il->data[--il->len] = 0;
+ if (TT.il->len > 1) {
+ TT.il->data[--TT.il->len] = 0;
break;
}
// FALLTHROUGH
case 27:
TT.vi_mode = 1;
- il->len = 0;
- memset(il->data, 0, il->alloc);
+ TT.il->len = 0;
+ memset(TT.il->data, 0, TT.il->alloc);
break;
case 0x0D:
- if (run_ex_cmd(il->data) == -1)
+ if (run_ex_cmd(TT.il->data) == -1)
goto cleanup_vi;
TT.vi_mode = 1;
- il->len = 0;
- memset(il->data, 0, il->alloc);
+ TT.il->len = 0;
+ memset(TT.il->data, 0, TT.il->alloc);
break;
default: //add chars to ex command until ENTER
if (key >= 0x20 && key < 0x7F) { //might be utf?
- if (il->len == il->alloc) {
- il->data = realloc(il->data, il->alloc*2);
- il->alloc *= 2;
+ if (TT.il->len == TT.il->alloc) {
+ TT.il->data = realloc(TT.il->data, TT.il->alloc*2);
+ TT.il->alloc *= 2;
}
- il->data[il->len] = key;
- il->len++;
+ TT.il->data[TT.il->len] = key;
+ TT.il->len++;
}
break;
}
} else if (TT.vi_mode == 2) {//INSERT MODE
switch (key) {
case 27:
- i_insert(il->data, il->len);
+ i_insert(TT.il->data, TT.il->len);
TT.vi_mode = 1;
- il->len = 0;
- memset(il->data, 0, il->alloc);
+ TT.il->len = 0;
+ memset(TT.il->data, 0, TT.il->alloc);
break;
case 0x7F:
case 0x08:
- if (il->len) {
- char *last = utf8_last(il->data, il->len);
+ if (TT.il->len) {
+ char *last = utf8_last(TT.il->data, TT.il->len);
int shrink = strlen(last);
memset(last, 0, shrink);
- il->len -= shrink;
+ TT.il->len -= shrink;
}
break;
case 0x0D:
//insert newline
//
- i_insert(il->data, il->len);
- il->len = 0;
- memset(il->data, 0, il->alloc);
+ i_insert(TT.il->data, TT.il->len);
+ TT.il->len = 0;
+ memset(TT.il->data, 0, TT.il->alloc);
i_split();
break;
default:
if ((key >= 0x20 || key == 0x09) &&
utf8_dec(key, utf8_code, &utf8_dec_p)) {
- if (il->len+utf8_dec_p+1 >= il->alloc) {
- il->data = realloc(il->data, il->alloc*2);
- il->alloc *= 2;
+ if (TT.il->len+utf8_dec_p+1 >= TT.il->alloc) {
+ TT.il->data = realloc(TT.il->data, TT.il->alloc*2);
+ TT.il->alloc *= 2;
}
- strcpy(il->data+il->len, utf8_code);
- il->len += utf8_dec_p;
+ strcpy(TT.il->data+TT.il->len, utf8_code);
+ TT.il->len += utf8_dec_p;
utf8_dec_p = 0;
*utf8_code = 0;
@@ -1047,7 +1049,7 @@ void vi_main(void)
}
cleanup_vi:
linelist_unload();
- free(il->data), free(il), free(yank.data);
+ free(TT.il->data), free(TT.il), free(TT.yank.data);
tty_reset();
tty_esc("?1049l");
}
@@ -1109,10 +1111,9 @@ static void draw_page()
struct linelist *scr_buf = 0;
unsigned y = 0;
int x = 0;
- wchar_t wc;
char *line = 0, *end = 0;
- int utf_l = 0, bytes = 0;
+ int bytes = 0;
//screen coordinates for cursor
int cy_scr = 0, cx_scr = 0;
@@ -1123,7 +1124,7 @@ static void draw_page()
int scroll = 0, redraw = 0;
adjust_screen_buffer();
- scr_buf = screen;
+ scr_buf = TT.screen;
redraw = (TT.vi_mov_flag & 0x30000000)>>28;
scroll = TT.drawn_row-TT.scr_row;
@@ -1137,7 +1138,7 @@ static void draw_page()
//jump until cursor
for (; y < TT.screen_height; y++ ) {
- if (scr_buf == c_r) break;
+ if (scr_buf == TT.c_r) break;
scr_buf = scr_buf->down;
}
//draw cursor row
@@ -1154,8 +1155,8 @@ static void draw_page()
aw = crunch_nstr(&end, 1024, bytes, 0, "\t", vi_crunch);
//if we need to render text that is not inserted to buffer yet
- if (TT.vi_mode == 2 && il->len) {
- char* iend = il->data; //input end
+ if (TT.vi_mode == 2 && TT.il->len) {
+ char* iend = TT.il->data; //input end
x = 0;
//find insert end position
iw = crunch_str(&iend, 1024, 0, "\t", vi_crunch);
@@ -1164,12 +1165,12 @@ static void draw_page()
//if clipped area is bigger than text before insert
if (clip > aw) {
clip -= aw;
- iend = il->data;
+ iend = TT.il->data;
iw -= crunch_str(&iend, clip, 0, "\t", vi_crunch);
x = crunch_str(&iend, iw, stdout, "\t", vi_crunch);
} else {
- iend = il->data;
+ iend = TT.il->data;
end = line;
//if clipped area is substring from cursor row start
@@ -1202,14 +1203,14 @@ static void draw_page()
//start drawing all other rows that needs update
///////////////////////////////////////////////////////////////////
- y = 0, scr_buf = screen;
+ y = 0, scr_buf = TT.screen;
//if we moved around in long line might need to redraw everything
if (clip != TT.drawn_col) redraw = 3;
for (; y < TT.screen_height; y++ ) {
int draw_line = 0;
- if (scr_buf == c_r) {
+ if (scr_buf == TT.c_r) {
scr_buf = scr_buf->down;
continue;
} else if (redraw) draw_line++;
@@ -1243,29 +1244,13 @@ static void draw_page()
tty_jump(0, TT.screen_height);
tty_esc("2K");
if (TT.vi_mode == 2) printf("\x1b[1m-- INSERT --\x1b[m");
-
- //DEBUG
- utf_l=utf8towc(&wc, &c_r->line->data[TT.cur_col], c_r->line->len-TT.cur_col);
- if (utf_l > 1) {
- char t[5] = {0, 0, 0, 0, 0};
- strncpy(t, &c_r->line->data[TT.cur_col], utf_l);
- printf(" (utf: %d %s)", utf_l, t);
- }
- //DEBUG
+ if (!TT.vi_mode) printf("\x1b[1m%s \x1b[m",TT.il->data);
tty_jump(TT.screen_width-12, TT.screen_height);
printf("%d,%d", TT.cur_row+1, TT.cur_col+1);
if (TT.cur_col != cx_scr) printf("-%d", cx_scr+1);
- putchar('\n');
- tty_esc("m");
- tty_jump(0, TT.screen_height+1);
- tty_esc("2K");
- if (!TT.vi_mode) {
- tty_esc("1m");
- printf("%s", il->data);
- tty_esc("m");
- } else tty_jump(cx_scr, cy_scr);
+ if (TT.vi_mode) tty_jump(cx_scr, cy_scr);
xflush(1);
@@ -1273,35 +1258,35 @@ static void draw_page()
static void check_cursor_bounds()
{
- if (c_r->line->len == 0) {
+ if (TT.c_r->line->len == 0) {
TT.cur_col = 0;
return;
- } else if (c_r->line->len-1 < TT.cur_col) TT.cur_col = c_r->line->len-1;
+ } else if (TT.c_r->line->len-1 < TT.cur_col) TT.cur_col = TT.c_r->line->len-1;
- if (TT.cur_col && utf8_width(&c_r->line->data[TT.cur_col],
- c_r->line->len-TT.cur_col) <= 0)
+ if (TT.cur_col && utf8_width(&TT.c_r->line->data[TT.cur_col],
+ TT.c_r->line->len-TT.cur_col) <= 0)
TT.cur_col--, check_cursor_bounds();
}
static void adjust_screen_buffer()
{
//search cursor and screen
- struct linelist *t = text;
+ struct linelist *t = TT.text;
int c = -1, s = -1, i = 0;
//searching cursor and screen line numbers
for (;((c == -1) || (s == -1)) && t != 0; i++, t = t->down) {
- if (t == c_r) c = i;
- if (t == screen) s = i;
+ if (t == TT.c_r) c = i;
+ if (t == TT.screen) s = i;
}
//adjust screen buffer so cursor is on drawing area
- if (c <= s) screen = c_r, s = c; //scroll up
+ if (c <= s) TT.screen = TT.c_r, s = c; //scroll up
else {
//drawing does not have wrapping so no need to check width
int distance = c-s+1;
if (distance > (int)TT.screen_height) {
int adj = distance-TT.screen_height;
- for (;adj; adj--) screen = screen->down, s++; //scroll down
+ for (;adj; adj--) TT.screen = TT.screen->down, s++; //scroll down
}
}
@@ -1392,15 +1377,15 @@ static int cur_right(int count0, int count1, char* unused)
{
int count = count0*count1;
for (;count--;) {
- if (c_r->line->len <= 1) return 1;
- if (TT.cur_col >= c_r->line->len-1) {
- TT.cur_col = utf8_last(c_r->line->data, c_r->line->len)
- - c_r->line->data;
+ if (TT.c_r->line->len <= 1) return 1;
+ if (TT.cur_col >= TT.c_r->line->len-1) {
+ TT.cur_col = utf8_last(TT.c_r->line->data, TT.c_r->line->len)
+ - TT.c_r->line->data;
return 1;
}
TT.cur_col++;
- if (utf8_width(&c_r->line->data[TT.cur_col],
- c_r->line->len-TT.cur_col) <= 0)
+ if (utf8_width(&TT.c_r->line->data[TT.cur_col],
+ TT.c_r->line->len-TT.cur_col) <= 0)
cur_right(1, 1, 0);
}
return 1;
@@ -1409,8 +1394,8 @@ static int cur_right(int count0, int count1, char* unused)
static int cur_up(int count0, int count1, char* unused)
{
int count = count0*count1;
- for (;count-- && c_r->up;)
- c_r = c_r->up;
+ for (;count-- && TT.c_r->up;)
+ TT.c_r = TT.c_r->up;
TT.vi_mov_flag |= 0x80000000;
check_cursor_bounds();
@@ -1420,8 +1405,8 @@ static int cur_up(int count0, int count1, char* unused)
static int cur_down(int count0, int count1, char* unused)
{
int count = count0*count1;
- for (;count-- && c_r->down;)
- c_r = c_r->down;
+ for (;count-- && TT.c_r->down;)
+ TT.c_r = TT.c_r->down;
check_cursor_bounds();
return 1;