diff --git a/blocks.c b/blocks.c index 02c6c18..11c1f69 100644 --- a/blocks.c +++ b/blocks.c @@ -102,8 +102,10 @@ int blocks__Iterator(buf_t *buf, int (*(func))(), int result) t = buf->blocks; while (t != NULL) { - if ((*(func))(&(t->item)) == result) { - return 0; + if (t->item.id != 0) { + if ((*(func))(&(t->item)) == result) { + return 0; + } } t = t->next; } diff --git a/bscript.c b/bscript.c index 7791af2..bdcf44f 100644 --- a/bscript.c +++ b/bscript.c @@ -40,8 +40,8 @@ lua_State *lstate; -extern core_t core; -extern state_t state; +/* !!!!! JUST TEMPORARY HACK !!!! */ +extern core_t *coretmp; extern struct MARKERS_ markers[MARK_COUNT]; extern WINDOW *tools_win; @@ -54,19 +54,20 @@ extern WINDOW *tools_win; static int bvim_lua_error_handler(lua_State *L) { char* msg = NULL; + buf_t* buf = coretmp->curbuf; - if (state.mode != BVI_MODE_REPL) { + if (buf->state.mode != BVI_MODE_REPL) { msg = (char*)lua_tostring(L, 1); if (msg != NULL) - ui__ErrorMsg(msg); + bvim_error(coretmp, buf, msg); else - ui__ErrorMsg("Unknown error"); + bvim_error(coretmp, buf, "Unknown error"); } else { msg = (char*)lua_tostring(L, 1); if (msg != NULL) - ui__REPLWin_print(msg); + ui__REPLWin_print(coretmp, buf, msg); else - ui__REPLWin_print("Unknown error"); + ui__REPLWin_print(coretmp, buf, "Unknown error"); } return 0; } @@ -75,6 +76,7 @@ static int bvim_lua_error_raise(lua_State *L, char* fmt, ...) { char msg[1024]; va_list ap; + buf_t* buf = coretmp->curbuf; msg[0] = '\0'; @@ -90,7 +92,7 @@ static int bvim_lua_error_raise(lua_State *L, char* fmt, ...) } */ if (msg[0] != '\0') - ui__ErrorMsg(msg); + bvim_error(coretmp, buf, msg); return 0; } @@ -103,6 +105,7 @@ static int bvim_command_add(lua_State *L) { int handler_type = 0; struct command new_cmd; + if (lua_gettop(L) == 4) { new_cmd.id = 1; new_cmd.name = (char *)lua_tostring(L, 1); @@ -118,7 +121,7 @@ static int bvim_command_add(lua_State *L) } new_cmd.size1 = strlen(new_cmd.name); new_cmd.size2 = 2; - commands__Cmd_Add(&new_cmd); + commands__Cmd_Add(coretmp, &new_cmd); } else { bvim_lua_error_raise(L, "Error in lua command_add function! Wrong format!"); } @@ -132,7 +135,7 @@ static int bvim_command_del(lua_State *L) char* name; if (lua_gettop(L) == 1) { name = (char *)lua_tostring(L, 1); - commands__Cmd_Del(name); + commands__Cmd_Del(coretmp, name); } else { bvim_lua_error_raise(L, "Error in lua command_del function! Wrong format!"); } @@ -148,14 +151,16 @@ static int bvim_save(lua_State * L) { char *filename; int flags = O_RDWR; + buf_t *buf = coretmp->curbuf; int n = lua_gettop(L); + if (n == 1) { filename = (char*)lua_tostring(L, 1); - save(filename, core.editor.mem, core.editor.maxpos, flags); + save(coretmp, buf, filename, flags); } else if (n == 2) { filename = (char*)lua_tostring(L, 1); flags = (int)lua_tonumber(L, 2); - save(filename, core.editor.mem, core.editor.maxpos, flags); + save(coretmp, buf, filename, flags); } return 0; } @@ -166,18 +171,19 @@ static int bvim_load(lua_State * L) { char *filename; int mode; + buf_t* buf = coretmp->curbuf; int n = lua_gettop(L); if (n == 1) { filename = (char *)lua_tostring(L, 1); - load(filename); + load(coretmp, buf, filename); } else if (n == 2) { filename = (char *)lua_tostring(L, 1); mode = (int)lua_tonumber(L, 2); if (mode == 1) { - addfile(filename); + addfile(coretmp, buf, filename); } else { - load(filename); + load(coretmp, buf, filename); } } return 0; @@ -200,7 +206,7 @@ static int bvim_plugin_load(lua_State * L) if (n == 1) { filename = (char *)lua_tostring(L, 1); - plugin__Load(filename); + plugin__Load(coretmp, filename); } else { bvim_lua_error_raise(L, "Fail to load plugin - wrong format"); } @@ -218,26 +224,27 @@ static int bvim_plugin_info(lua_State *L) char* name; char tmpstr[1024]; plugin_t* plg; + buf_t *buf = coretmp->curbuf; int n = lua_gettop(L); if (n == 1) { name = (char *)lua_tostring(L, 1); - plg = plugins__GetByName(name); + plg = plugins__GetByName(coretmp, name); if (plg != NULL) { - if (state.mode == BVI_MODE_REPL) { + if (buf->state.mode == BVI_MODE_REPL) { tmpstr[0] = '\0'; sprintf(tmpstr, "Name: %s", plg->name); - ui__REPLWin_print(tmpstr); + ui__REPLWin_print(coretmp, buf, tmpstr); sprintf(tmpstr, "Author: %s", plg->author); - ui__REPLWin_print(tmpstr); + ui__REPLWin_print(coretmp, buf, tmpstr); sprintf(tmpstr, "License: %s", plg->license); - ui__REPLWin_print(tmpstr); + ui__REPLWin_print(coretmp, buf, tmpstr); sprintf(tmpstr, "Version: %d.%d", plg->version.major, plg->version.minor); - ui__REPLWin_print(tmpstr); + ui__REPLWin_print(coretmp, buf, tmpstr); sprintf(tmpstr, "Description: %s", plg->description); - ui__REPLWin_print(tmpstr); + ui__REPLWin_print(coretmp, buf, tmpstr); } else { - bvim_info(state.mode, "%s %d.%d - %s", plg->name, plg->version.major, plg->version.minor, plg->description); + bvim_info(coretmp, buf, "%s %d.%d - %s", plg->name, plg->version.major, plg->version.minor, plg->description); } } else { bvim_lua_error_raise(L, "Haven't such plugin"); @@ -254,18 +261,20 @@ static int bvim_plugin_info(lua_State *L) /* lua: block_select(block_number, start, end, pattern) */ static int bvim_block_select(lua_State * L) { + buf_t *buf = coretmp->curbuf; struct block_item tmp_blk; + if (lua_gettop(L) == 4) { tmp_blk.id = (unsigned int)lua_tonumber(L, 1); - if (blocks__GetByID(tmp_blk.id) == NULL) { + if (blocks__GetByID(buf, tmp_blk.id) == NULL) { tmp_blk.pos_start = (unsigned long)lua_tonumber(L, 2); tmp_blk.pos_end = (unsigned long)lua_tonumber(L, 3); tmp_blk.palette = (unsigned int)lua_tonumber(L, 4); tmp_blk.hl_toggle = 1; - blocks__Add(tmp_blk); + blocks__Add(buf, tmp_blk); ui__BlockHighlightAdd(&tmp_blk); - if (state.mode != BVI_MODE_REPL) - ui__Screen_Repaint(); + if (buf->state.mode != BVI_MODE_REPL) + ui__Screen_Repaint(coretmp, coretmp->curbuf); } else { bvim_lua_error_raise(L, "Wrong block ID! It already exist!"); } @@ -281,15 +290,17 @@ static int bvim_block_select(lua_State * L) static int bvim_block_fold(lua_State * L) { + buf_t *buf = coretmp->curbuf; struct block_item *tmp_blk; unsigned int id = 0; + if (lua_gettop(L) == 1) { id = (int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if (tmp_blk != NULL) { tmp_blk->folding = 1; - if (state.mode != BVI_MODE_REPL) - ui__Screen_Repaint(); + if (buf->state.mode != BVI_MODE_REPL) + ui__Screen_Repaint(coretmp, coretmp->curbuf); } else { bvim_lua_error_raise(L, "Wrong block number!"); } @@ -304,16 +315,18 @@ static int bvim_block_fold(lua_State * L) /* lua: block_read(block_id) */ static int bvim_block_read(lua_State * L) { + buf_t *buf = coretmp->curbuf; struct block_item *tmp_blk; unsigned int id = 0; char *blck = NULL; + if (lua_gettop(L) == 1) { id = (int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if (tmp_blk != NULL) { if (tmp_blk->pos_end > tmp_blk->pos_start) { blck = (char *)malloc(tmp_blk->pos_end - tmp_blk->pos_start + 1); - memcpy(blck, core.editor.mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); + memcpy(blck, buf->mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); lua_pushstring(L, blck); } else { bvim_lua_error_raise(L, "You need select valid block before read!"); @@ -329,14 +342,15 @@ static int bvim_block_read(lua_State * L) /* lua: block_and(block_number, mask) */ static int bvim_block_and(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; struct block_item *tmp_blk; if (lua_gettop(L) == 2) { id = (int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { - math__logic_block(AND, (char *)lua_tostring(L, 2), id); + math__logic_block(coretmp, buf, AND, (char *)lua_tostring(L, 2), id); } else { bvim_lua_error_raise(L, "You need select valid block before and operation!"); } @@ -350,14 +364,15 @@ static int bvim_block_and(lua_State * L) /* lua: block_or(block_number, mask) */ static int bvim_block_or(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; struct block_item *tmp_blk; if (lua_gettop(L) == 2) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { - math__logic_block(OR, (char *)lua_tostring(L, 2), id); + math__logic_block(coretmp, buf, OR, (char *)lua_tostring(L, 2), id); } else { bvim_lua_error_raise (L, "You need select valid block before or operation!"); @@ -372,14 +387,15 @@ static int bvim_block_or(lua_State * L) /* lua: block_xor(block_number, mask) */ static int bvim_block_xor(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; struct block_item *tmp_blk; if (lua_gettop(L) == 2) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { - math__logic_block(XOR, (char *)lua_tostring(L, 2), id); + math__logic_block(coretmp, buf, XOR, (char *)lua_tostring(L, 2), id); } else { bvim_lua_error_raise (L, "You need select valid block before xor operation!"); @@ -394,14 +410,15 @@ static int bvim_block_xor(lua_State * L) /* lua: block_lshift(block_number, count) */ static int bvim_block_lshift(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; struct block_item *tmp_blk; if (lua_gettop(L) == 2) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { - math__logic_block(LSHIFT, (char *)lua_tostring(L, 2), id); + math__logic_block(coretmp, buf, LSHIFT, (char *)lua_tostring(L, 2), id); } else { bvim_lua_error_raise (L, "You need select valid block before lshift operation!"); @@ -416,14 +433,15 @@ static int bvim_block_lshift(lua_State * L) /* lua: block_rshift(block_number, count) */ static int bvim_block_rshift(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; struct block_item *tmp_blk; if (lua_gettop(L) == 2) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { - math__logic_block(RSHIFT, (char *)lua_tostring(L, 2), id); + math__logic_block(coretmp, buf, RSHIFT, (char *)lua_tostring(L, 2), id); } else { bvim_lua_error_raise (L, "You need select valid block before rshift operation!"); @@ -438,14 +456,15 @@ static int bvim_block_rshift(lua_State * L) /* lua: block_lrotate(block_number, count) */ static int bvim_block_lrotate(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; struct block_item *tmp_blk; if (lua_gettop(L) == 2) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { - math__logic_block(LROTATE, (char *)lua_tostring(L, 2), id); + math__logic_block(coretmp, buf, LROTATE, (char *)lua_tostring(L, 2), id); } else { bvim_lua_error_raise (L, "You need select valid block before lrotate operation!"); @@ -460,14 +479,15 @@ static int bvim_block_lrotate(lua_State * L) /* lua: block_rrotate(block_number, count) */ static int bvim_block_rrotate(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; struct block_item *tmp_blk; if (lua_gettop(L) == 2) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { - math__logic_block(RROTATE, (char *)lua_tostring(L, 2), id); + math__logic_block(coretmp, buf, RROTATE, (char *)lua_tostring(L, 2), id); } else { bvim_lua_error_raise (L, "You need select valid block before rrotate operation!"); @@ -482,6 +502,7 @@ static int bvim_block_rrotate(lua_State * L) /* lua: entropy(block_number) */ static int bvim_entropy(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; struct block_item *tmp_blk; double entropy = 0; @@ -489,9 +510,9 @@ static int bvim_entropy(lua_State * L) if (lua_gettop(L) == 1) { if (lua_type(L, 1) == LUA_TNUMBER) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if (tmp_blk != NULL) { - entropy = math__entropy(id); + entropy = math__entropy(coretmp, buf, id); lua_pushnumber(L, entropy); return 1; } @@ -510,17 +531,19 @@ static int bvim_entropy(lua_State * L) /* lua: crc16("string") */ static int bvim_crc16(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; char *blck = NULL; struct block_item *tmp_blk; unsigned int crc = 0; + if (lua_gettop(L) == 1) { if (lua_type(L, 1) == LUA_TNUMBER) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { blck = (char *)malloc(tmp_blk->pos_end - tmp_blk->pos_start + 1); - memcpy(blck, core.editor.mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); + memcpy(blck, buf->mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); crc = crc16(blck, tmp_blk->pos_end - tmp_blk->pos_start + 1, crc); lua_pushnumber(L, crc); return 1; @@ -543,17 +566,19 @@ static int bvim_crc16(lua_State * L) /* lua: crc32("string") */ static int bvim_crc32(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; char *blck = NULL; struct block_item *tmp_blk; unsigned int crc = 0; + if (lua_gettop(L) == 1) { if (lua_type(L, 1) == LUA_TNUMBER) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { blck = (char *)malloc(tmp_blk->pos_end - tmp_blk->pos_start + 1); - memcpy(blck, core.editor.mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); + memcpy(blck, buf->mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); crc = crc32(blck, tmp_blk->pos_end - tmp_blk->pos_start + 1, crc); lua_pushnumber(L, crc); return 1; @@ -576,18 +601,20 @@ static int bvim_crc32(lua_State * L) /* lua: md4_hash("string") */ static int bvim_md4_hash(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; char *blck = NULL; struct block_item *tmp_blk; char hash[65]; + hash[0] = '\0'; if (lua_gettop(L) == 1) { if (lua_type(L, 1) == LUA_TNUMBER) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { blck = (char *)malloc(tmp_blk->pos_end - tmp_blk->pos_start + 1); - memcpy(blck, core.editor.mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); + memcpy(blck, buf->mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); math__md4_hash_string((unsigned char *)blck, tmp_blk->pos_end - tmp_blk->pos_start + 1, hash); lua_pushstring(L, hash); return 1; @@ -613,18 +640,20 @@ static int bvim_md4_hash(lua_State * L) /* lua: md5_hash("string") */ static int bvim_md5_hash(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; char *blck = NULL; struct block_item *tmp_blk; char hash[65]; + hash[0] = '\0'; if (lua_gettop(L) == 1) { if (lua_type(L, 1) == LUA_TNUMBER) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { blck = (char *)malloc(tmp_blk->pos_end - tmp_blk->pos_start + 1); - memcpy(blck, core.editor.mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); + memcpy(blck, buf->mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); math__md5_hash_string((unsigned char *)blck, tmp_blk->pos_end - tmp_blk->pos_start + 1, hash); lua_pushstring(L, hash); return 1; @@ -650,18 +679,20 @@ static int bvim_md5_hash(lua_State * L) /* lua: sha1_hash("string") */ static int bvim_sha1_hash(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; char *blck = NULL; struct block_item *tmp_blk; char hash[65]; + hash[0] = '\0'; if (lua_gettop(L) == 1) { if (lua_type(L, 1) == LUA_TNUMBER) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { blck = (char *)malloc(tmp_blk->pos_end - tmp_blk->pos_start + 1); - memcpy(blck, core.editor.mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); + memcpy(blck, buf->mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); math__sha1_hash_string((unsigned char *)blck, tmp_blk->pos_end - tmp_blk->pos_start + 1, hash); lua_pushstring(L, hash); return 1; @@ -687,24 +718,25 @@ static int bvim_sha1_hash(lua_State * L) /* lua: sha256_hash("string") */ static int bvim_sha256_hash(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; char *blck = NULL; struct block_item *tmp_blk; char hash[65]; + hash[0] = '\0'; if (lua_gettop(L) == 1) { if (lua_type(L, 1) == LUA_TNUMBER) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { blck = (char *)malloc(tmp_blk->pos_end - tmp_blk->pos_start + 1); - memcpy(blck, core.editor.mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); + memcpy(blck, buf->mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); math__sha256_hash_string((unsigned char *)blck, tmp_blk->pos_end - tmp_blk->pos_start + 1, hash); lua_pushstring(L, hash); return 1; } else { - bvim_lua_error_raise - (L, "You need select valid block before SHA256 hash calculation!"); + bvim_lua_error_raise(L, "You need select valid block before SHA256 hash calculation!"); } } else if (lua_type(L, 1) == LUA_TSTRING) { blck = (char *)malloc(strlen((char *)lua_tostring(L, 1))); @@ -714,8 +746,7 @@ static int bvim_sha256_hash(lua_State * L) return 1; } } else { - bvim_lua_error_raise - (L, "Error in lua sha256_hash function! Wrong format!"); + bvim_lua_error_raise(L, "Error in lua sha256_hash function! Wrong format!"); } return 0; } @@ -725,18 +756,20 @@ static int bvim_sha256_hash(lua_State * L) /* lua: sha512_hash("string") */ static int bvim_sha512_hash(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; char *blck = NULL; struct block_item *tmp_blk; char hash[129]; + hash[0] = '\0'; if (lua_gettop(L) == 1) { if (lua_type(L, 1) == LUA_TNUMBER) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { blck = (char *)malloc(tmp_blk->pos_end - tmp_blk->pos_start + 1); - memcpy(blck, core.editor.mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); + memcpy(blck, buf->mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); math__sha512_hash_string((unsigned char *)blck, tmp_blk->pos_end - tmp_blk->pos_start + 1, hash); lua_pushstring(L, hash); return 1; @@ -763,18 +796,20 @@ static int bvim_sha512_hash(lua_State * L) /* lua: ripemd160_hash("string") */ static int bvim_ripemd160_hash(lua_State * L) { + buf_t *buf = coretmp->curbuf; unsigned int id = 0; char *blck = NULL; struct block_item *tmp_blk; char hash[65]; + hash[0] = '\0'; if (lua_gettop(L) == 1) { if (lua_type(L, 1) == LUA_TNUMBER) { id = (unsigned int)lua_tonumber(L, 1); - tmp_blk = blocks__GetByID(id); + tmp_blk = blocks__GetByID(buf, id); if ((tmp_blk != NULL) & (tmp_blk->pos_end > tmp_blk->pos_start)) { blck = (char *)malloc(tmp_blk->pos_end - tmp_blk->pos_start + 1); - memcpy(blck, core.editor.mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); + memcpy(blck, buf->mem + tmp_blk->pos_start, tmp_blk->pos_end - tmp_blk->pos_start + 1); math__ripemd160_hash_string((unsigned char *)blck, tmp_blk->pos_end - tmp_blk->pos_start + 1, hash); lua_pushstring(L, hash); return 1; @@ -802,34 +837,25 @@ static int bvim_ripemd160_hash(lua_State * L) /* or search_bytes(bytes, return_all) where return_all = [0|1] */ static int bvim_search_bytes(lua_State * L) { + buf_t *buf = coretmp->curbuf; PTR result; PTR start_addr = NULL; char *bytes; - char msgbuf[256]; int i = 0; - msgbuf[0] = '\0'; - start_addr = core.editor.mem; + start_addr = buf->mem; if (lua_gettop(L) == 1) { bytes = (char *)lua_tostring(L, -1); - result = - searching('\\', bytes, start_addr, core.editor.maxpos - 1, - FALSE | S_GLOBAL); - /* - sprintf(msgbuf, "Found [%s] at 0x%08x position", bytes, (long)(result - core.editor.mem)); - wmsg(msgbuf, 3, strlen(msgbuf) + 4); - */ - lua_pushnumber(L, (long)(result - core.editor.mem)); + result = searching(coretmp, buf, '\\', bytes, start_addr, buf->maxpos - 1, FALSE | S_GLOBAL); + lua_pushnumber(L, (long)(result - buf->mem)); return 1; } else if (lua_gettop(L) == 2) { bytes = (char *)lua_tostring(L, 1); result = start_addr; if ((int)lua_tonumber(L, 2)) { - while (result < core.editor.maxpos) { - result = - searching('\\', bytes, start_addr, - core.editor.maxpos - 1, FALSE | S_GLOBAL); - lua_pushnumber(L, (long)(result - core.editor.mem)); + while (result < buf->maxpos) { + result = searching(coretmp, buf, '\\', bytes, start_addr, buf->maxpos - 1, FALSE | S_GLOBAL); + lua_pushnumber(L, (long)(result - buf->mem)); i++; } } @@ -843,19 +869,20 @@ static int bvim_search_bytes(lua_State * L) /* lua: replace_bytes(original, target) */ static int bvim_replace_bytes(lua_State * L) { + buf_t *buf = coretmp->curbuf; int result = 0; PTR start_addr = NULL; char *bytes; char *target; char bytesbuf[256]; + bytesbuf[0] = '\0'; - start_addr = core.editor.mem; + start_addr = buf->mem; if (lua_gettop(L) == 2) { bytes = (char *)lua_tostring(L, 1); target = (char *)lua_tostring(L, 2); sprintf(bytesbuf, "/%s/%s/", bytes, target); - result = - do_substitution('\\', bytesbuf, start_addr, core.editor.maxpos - 1); + result = do_substitution(coretmp, buf, '\\', bytesbuf, start_addr, buf->maxpos - 1); lua_pushnumber(L, result); } return 1; @@ -865,10 +892,11 @@ static int bvim_replace_bytes(lua_State * L) /* lua: exec(command) */ static int bvim_exec(lua_State * L) { + buf_t *buf = coretmp->curbuf; char *cmdline; if (lua_gettop(L) != 0) { cmdline = (char *)lua_tostring(L, -1); - docmdline(cmdline); + docmdline(coretmp, buf, cmdline); } return 0; } @@ -877,10 +905,11 @@ static int bvim_exec(lua_State * L) /* lua: display_error(message) */ static int bvim_display_error(lua_State * L) { + buf_t *buf = coretmp->curbuf; char *message; if (lua_gettop(L) != 0) { message = (char *)lua_tostring(L, -1); - bvim_error(state.mode, message); + bvim_error(coretmp, buf, message); } return 0; } @@ -889,10 +918,11 @@ static int bvim_display_error(lua_State * L) /* lua: status_line_msg(message) */ static int bvim_status_line_msg(lua_State * L) { + buf_t *buf = coretmp->curbuf; char *message; if (lua_gettop(L) != 0) { message = (char *)lua_tostring(L, -1); - bvim_info(state.mode, message); + bvim_info(coretmp, buf, message); } return 0; } @@ -903,15 +933,16 @@ static int bvim_msg_window(lua_State * L) { char *message; int height, width; + buf_t *buf = coretmp->curbuf; int n = lua_gettop(L); if (n == 1) { message = (char *)lua_tostring(L, -1); - ui__MsgWin_Show(message, 3, strlen(message) + 4); + ui__MsgWin_Show(coretmp, buf, message, 3, strlen(message) + 4); } else if (n == 3) { message = (char *)lua_tostring(L, 1); height = (int)lua_tonumber(L, 2); width = (int)lua_tonumber(L, 3); - ui__MsgWin_Show(message, height, width); + ui__MsgWin_Show(coretmp, buf, message, height, width); } return 0; } @@ -920,9 +951,10 @@ static int bvim_msg_window(lua_State * L) /* lua: tools_window() */ static int bvim_tools_window(lua_State * L) { + buf_t *buf = coretmp->curbuf; int n = lua_gettop(L); if (n == 1) { - ui__ToolWin_Show((int)lua_tonumber(L, -1)); + ui__ToolWin_Show(coretmp, buf, (int)lua_tonumber(L, -1)); } return 0; } @@ -931,12 +963,12 @@ static int bvim_tools_window(lua_State * L) /* lua: print_tools_window() */ static int bvim_print_tools_window(lua_State * L) { + buf_t *buf = coretmp->curbuf; int n = lua_gettop(L); if (n == 1) { - ui__ToolWin_Print((char *)lua_tostring(L, -1), 1); + ui__ToolWin_Print(coretmp, buf, (char *)lua_tostring(L, -1), 1); } else if (n == 2) { - ui__ToolWin_Print((char *)lua_tostring(L, 1), - (int)lua_tonumber(L, 2)); + ui__ToolWin_Print(coretmp, buf, (char *)lua_tostring(L, 1), (int)lua_tonumber(L, 2)); } return 0; } @@ -965,7 +997,9 @@ static int bvim_print(lua_State * L) /* Undo */ static int bvim_undo(lua_State * L) { - do_undo(); + buf_t *buf = coretmp->curbuf; + + do_undo(coretmp, buf); return 0; } @@ -983,23 +1017,27 @@ static int bvim_set(lua_State * L) static int bvim_scrolldown(lua_State * L) { + buf_t *buf = coretmp->curbuf; int lines; + if (lua_gettop(L) > 0) { lines = (int)lua_tonumber(L, 1); } else lines = 1; - scrolldown(lines); + scrolldown(coretmp, buf, lines); return 0; } static int bvim_scrollup(lua_State * L) { + buf_t *buf = coretmp->curbuf; int lines; + if (lua_gettop(L) > 0) { lines = (int)lua_tonumber(L, 1); } else lines = 1; - scrollup(lines); + scrollup(coretmp, buf, lines); return 0; } @@ -1024,16 +1062,18 @@ static int bvim_cursor(lua_State * L) /* Adds marker to show in address */ static int bvim_set_marker(lua_State * L) { + buf_t *buf = coretmp->curbuf; long address; int i = 0; + if (lua_gettop(L) > 0) { address = (long)lua_tonumber(L, 1); while ((markers[i].address != 0) & (i < MARK_COUNT)) i++; markers[i].address = address; markers[i].marker = '+'; - if (state.mode != BVI_MODE_REPL) - ui__Screen_Repaint(); + if (buf->state.mode != BVI_MODE_REPL) + ui__Screen_Repaint(coretmp, buf); } return 0; } @@ -1041,8 +1081,10 @@ static int bvim_set_marker(lua_State * L) /* Remove marker */ static int bvim_unset_marker(lua_State * L) { + buf_t *buf = coretmp->curbuf; long address; int i = 0; + if (lua_gettop(L) > 0) { address = (long)lua_tonumber(L, 1); while (i < MARK_COUNT) { @@ -1052,8 +1094,8 @@ static int bvim_unset_marker(lua_State * L) if (markers[i].address == address) { markers[i].address = 0; markers[i].marker = ' '; - if (state.mode != BVI_MODE_REPL) - ui__Screen_Repaint(); + if (buf->state.mode != BVI_MODE_REPL) + ui__Screen_Repaint(coretmp, buf); } else bvim_lua_error_raise(L, "Cant found mark on this address!"); } @@ -1063,12 +1105,14 @@ static int bvim_unset_marker(lua_State * L) /* Display arbitrary address on the screen */ static int bvim_setpage(lua_State * L) { + buf_t *buf = coretmp->curbuf; int address; + if (lua_gettop(L) > 0) { address = (int)lua_tonumber(L, 1); } else address = 0; - setpage(core.editor.mem + address); + setpage(coretmp, buf, buf->mem + address); return 0; } @@ -1080,6 +1124,7 @@ static int bvim_setpage(lua_State * L) // insert io__BufferAdd, io__RecordInsert, io__RecordGet static int bvim_repl_print(lua_State *L) { + buf_t *buf = coretmp->curbuf; iorecord_t rec; const char *s; @@ -1100,9 +1145,10 @@ static int bvim_repl_print(lua_State *L) rec.output = s; io__RecordInsert(BVI_BUFFER_REPL, rec); - if (state.mode == BVI_MODE_REPL) { - if (i > 1) ui__REPLWin_print("\t"); - ui__REPLWin_print(s); + if (buf->state.mode == BVI_MODE_REPL) { + if (i > 1) + ui__REPLWin_print(coretmp, buf, "\t"); + ui__REPLWin_print(coretmp, buf, s); } lua_pop(L, 1); // pop result @@ -1115,7 +1161,9 @@ static int bvim_repl_print(lua_State *L) static int bvim_repl_clear(lua_State *L) { - ui__REPLWin_clear(); + buf_t *buf = coretmp->curbuf; + + ui__REPLWin_clear(coretmp, buf); return 0; } @@ -1129,29 +1177,29 @@ static int bvim_repl_clear(lua_State *L) /* =============== Lua functions list/buffer abstractions =============== */ /* Preallocate some memory for future lua functions allocation */ -int InitLuaFunctionsList(int N) +int InitLuaFunctionsList(core_t *core, int N) { int i = 0; - core.luaF_list = (luaF_link)malloc((N + 1)*(sizeof *(core.luaF_list))); + core->luaF_list = (luaF_link)malloc((N + 1)*(sizeof *(core->luaF_list))); for (i = 0; i < N + 1; i++) { - core.luaF_list[i].next = &(core.luaF_list[i + 1]); + core->luaF_list[i].next = &(core->luaF_list[i + 1]); } - core.luaF_list[N].next = NULL; + core->luaF_list[N].next = NULL; return 0; } -int LuaFunctionAdd(struct luaF_item i) +int LuaFunctionAdd(core_t *core, struct luaF_item i) { luaF_link t = NULL; t = (luaF_link)malloc(sizeof(*t)); if (t != NULL) { t->item = i; - if (core.luaF_list != NULL) { - t->next = core.luaF_list->next; - core.luaF_list->next = t; + if (core->luaF_list != NULL) { + t->next = core->luaF_list->next; + core->luaF_list->next = t; } else { - core.luaF_list = t; - core.luaF_list->next = NULL; + core->luaF_list = t; + core->luaF_list->next = NULL; } } else { return -1; @@ -1165,9 +1213,9 @@ void LuaFunctionInsertNext(luaF_link x, luaF_link t) x->next = t; } -void LuaFunctionFree(luaF_link x) +void LuaFunctionFree(core_t *core, luaF_link x) { - LuaFunctionInsertNext(core.luaF_list, x); + LuaFunctionInsertNext(core->luaF_list, x); } luaF_link LuaFunctionDeleteNext(luaF_link x) @@ -1195,10 +1243,10 @@ struct luaF_item LuaFunctionGet(luaF_link x) /* Iterator of any functions on functions list, * where result - expected result for function * All blocks are unique */ -int luaF_Iterator(int (*(func))(), int result) +int luaF_Iterator(core_t *core, int (*(func))(), int result) { luaF_link t; - t = core.luaF_list; + t = core->luaF_list; while (t != NULL) { if ((*(func))(&(t->item)) == result) { @@ -1209,25 +1257,25 @@ int luaF_Iterator(int (*(func))(), int result) return -1; } -int luaF_Add(struct luaF_item b) { +int luaF_Add(core_t *core, struct luaF_item b) { - LuaFunctionAdd(b); + LuaFunctionAdd(core, b); lua_register(lstate, b.name, b.handler.func); return 0; } -int luaF_DelByID(int id) { +int luaF_DelByID(core_t *core, int id) { return 0; } -int luaF_DelByName(char* name) { +int luaF_DelByName(core_t *core, char* name) { return 0; } -struct luaF_item* luaF_GetByID(unsigned int id) { +struct luaF_item* luaF_GetByID(core_t *core, unsigned int id) { luaF_link t; - t = core.luaF_list; + t = core->luaF_list; while (t != NULL) { @@ -1237,10 +1285,10 @@ struct luaF_item* luaF_GetByID(unsigned int id) { return NULL; } -struct luaF_item* luaF_GetByName(char* name) { +struct luaF_item* luaF_GetByName(core_t *core, char* name) { luaF_link t; - t = core.luaF_list; + t = core->luaF_list; while (t != NULL) { @@ -1250,16 +1298,16 @@ struct luaF_item* luaF_GetByName(char* name) { return NULL; } -int luaF_Init() +int luaF_Init(core_t *core) { //InitBlocksList(10); return 0; } -int luaF_Destroy() +int luaF_Destroy(core_t *core) { luaF_link t; - t = core.luaF_list; + t = core->luaF_list; while (t != NULL) { free(t); @@ -1268,7 +1316,7 @@ int luaF_Destroy() return 0; } -void bvim_lua_init() +void bvim_lua_init(core_t *core) { struct luaL_reg std_methods[] = { @@ -1352,13 +1400,13 @@ void bvim_lua_init() lua_setConst(lstate, BVI_MODE_EDIT); lua_setConst(lstate, BVI_MODE_VISUAL); lua_setConst(lstate, BVI_MODE_REPL); - luaF_Init(); + luaF_Init(core); /* loading init.lua script */ - bvim_run_lua_script("init"); + bvim_run_lua_script(core, core->curbuf, "init"); } // TODO: Add support of full path, multiple plugins directories -int bvim_run_lua_script(char *name) +int bvim_run_lua_script(core_t *core, buf_t *buf, char *name) { char filename[256]; int err; @@ -1369,7 +1417,8 @@ int bvim_run_lua_script(char *name) strcat(filename, ".lua"); err = luaL_loadfile(lstate, filename); if (err) { - bvim_error(BVI_MODE_EDIT, "Can't open lua script: %s", lua_tostring(lstate, -1)); + buf->state.mode = BVI_MODE_EDIT; + bvim_error(core, buf, "Can't open lua script: %s", lua_tostring(lstate, -1)); lua_pop(lstate, 1); } else { lua_pcall(lstate, 0, LUA_MULTRET, 0); @@ -1377,11 +1426,12 @@ int bvim_run_lua_script(char *name) return 0; } -int bvim_run_lua_string(char *string) +int bvim_run_lua_string(core_t *core, buf_t *buf, char *string) { int err = luaL_loadstring(lstate, string); if (err) { - bvim_error(BVI_MODE_EDIT, "Can't run lua command: %s", lua_tostring(lstate, -1)); + buf->state.mode = BVI_MODE_EDIT; + bvim_error(core, buf, "Can't run lua command: %s", lua_tostring(lstate, -1)); lua_pop(lstate, 1); } else { lua_pcall(lstate, 0, LUA_MULTRET, 0); @@ -1415,9 +1465,9 @@ int bvim_repl_eval(char *line) return 0; } -void bvim_lua_finish() +void bvim_lua_finish(core_t *core) { - luaF_Destroy(); + luaF_Destroy(core); lua_close(lstate); io__BufferDestroy(BVI_BUFFER_REPL); } diff --git a/bvim.c b/bvim.c index 4fd6b9a..cd4af7a 100644 --- a/bvim.c +++ b/bvim.c @@ -48,7 +48,8 @@ Copyright (C) 2011-2012 by Anton Kochkov"; jmp_buf env; /* context for `longjmp' function */ -//core_t core; +core_t *coretmp; +core_t *coreslot; //state_t state; //api_t api; @@ -130,7 +131,7 @@ int handler__goto_ASCII(core_t *core, buf_t *buf) // "Tab" key int handler__toggle(core_t *core, buf_t *buf) { - toggle(); + toggle(core, buf); return 0; } @@ -154,7 +155,7 @@ int handler__goto_home(core_t *core, buf_t *buf) if (precount > 0) { y = --precount; if (y > core->screen.maxy - 1) { - scrolldown(y - core->screen.maxy + 1); + scrolldown(core, buf, y - core->screen.maxy + 1); y = core->screen.maxy - 1; } } else @@ -265,7 +266,7 @@ int handler__goto_up(core_t *core, buf_t *buf) if (y > 0) y--; else - scrollup(1); + scrollup(core, buf, 1); } while (--precount > 0); return 0; } @@ -294,7 +295,7 @@ int handler__goto_down(core_t *core, buf_t *buf) if (y < (core->screen.maxy - 1)) y++; else - scrolldown(1); + scrolldown(core, buf, 1); } while (--precount > 0); return 0; } @@ -322,10 +323,10 @@ int handler__line(core_t *core, buf_t *buf) //case 'S': int handler__toolwin_toggle(core_t *core, buf_t *buf) { - if (!ui__ToolWin_Exist()) { - ui__ToolWin_Show(10); + if (!ui__ToolWin_Exist(core, buf)) { + ui__ToolWin_Show(core, buf, 10); } else { - ui__ToolWin_Hide(); + ui__ToolWin_Hide(core, buf); } return 0; } @@ -333,7 +334,7 @@ int handler__toolwin_toggle(core_t *core, buf_t *buf) // ":" key int handler__cmdstring(core_t *core, buf_t *buf) { - clearstr(); + ui__clearstr(core, buf); addch(':'); refresh(); /* TODO: Add autocompletion */ @@ -351,7 +352,7 @@ int handler__previous_page(core_t *core, buf_t *buf) buf->state.pagepos -= buf->state.screen; else buf->state.pagepos = buf->mem; - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); return 0; } @@ -360,7 +361,7 @@ int handler__scrolldown(core_t *core, buf_t *buf) { if (precount > 1) buf->state.scrolly = precount; - scrolldown(buf->state.scrolly); + scrolldown(core, buf, buf->state.scrolly); return 0; } @@ -369,7 +370,7 @@ int handler__scrollup(core_t *core, buf_t *buf) { if (precount > 1) buf->state.scrolly = precount; - scrollup(buf->state.scrolly); + scrollup(core, buf, buf->state.scrolly); return 0; } @@ -378,7 +379,7 @@ int handler__linescroll_down(core_t *core, buf_t *buf) { if (y > 0) y--; - scrolldown(1); + scrolldown(core, buf, 1); return 0; } @@ -391,9 +392,9 @@ int handler__nextpage(core_t *core, buf_t *buf) buf->state.current += buf->state.screen; if (buf->state.current - buf->mem >= filesize) { buf->state.current = buf->mem + filesize; - setpage((PTR) (buf->mem + filesize - 1L)); + setpage(core, buf, (PTR) (buf->mem + filesize - 1L)); } - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } return 0; } @@ -401,7 +402,7 @@ int handler__nextpage(core_t *core, buf_t *buf) // "Ctrl-G" key int handler__fileinfo(core_t *core, buf_t *buf) { - fileinfo(name); + fileinfo(core, buf, name); wrstat = 0; return 0; } @@ -409,7 +410,7 @@ int handler__fileinfo(core_t *core, buf_t *buf) // Redraw screen int handler__screen_redraw(core_t *core, buf_t *buf) { - ui__Screen_New(); + ui__Screen_New(core, buf); return 0; } @@ -418,14 +419,14 @@ int handler__linescroll_up(core_t *core, buf_t *buf) { if (y < core->screen.maxy) y++; - scrollup(1); + scrollup(core, buf, 1); return 0; } // "Ctrl-R" key - Open Lua REPL window int handler__luarepl(core_t *core, buf_t *buf) { - ui__REPL_Main(); + ui__REPL_Main(core, buf); return 0; } @@ -443,69 +444,69 @@ int handler__toggle_selection(core_t *core, buf_t *buf) // "A" key int handler__append_mode(core_t *core, buf_t *buf) { - smsg("APPEND MODE"); + ui__smsg(core, buf, "APPEND MODE"); buf->state.current = (PTR) (buf->mem + filesize - 1L); - setpage(buf->state.current++); - cur_forw(0); + setpage(core, buf, buf->state.current++); + cur_forw(core, buf, 0); setcur(); undosize = filesize; - undo_count = edit('A'); + undo_count = edit(core, buf, 'A'); return 0; } // "B" or "b" keys int handler__backsearch(core_t *core, buf_t *buf) { - setpage(backsearch(core, buf, buf->state.current, 'b')); + setpage(core, buf, backsearch(core, buf, buf->state.current, 'b')); return 0; } // "e" key int handler__setpage(core_t *core, buf_t *buf) { - setpage(end_word(core, buf, buf->state.current)); + setpage(core, buf, end_word(core, buf, buf->state.current)); return 0; } // "," key int handler__doft1(core_t *core, buf_t *buf) { - do_ft(-1, 0); + do_ft(core, buf, -1, 0); return 0; } // ";" key int handler__doft2(core_t *core, buf_t *buf) { - do_ft(0, 0); + do_ft(core, buf, 0, 0); return 0; } //case 'F': int handler__doft3_F(core_t *core, buf_t *buf) { - do_ft('F', 0); + do_ft(core, buf, 'F', 0); return 0; } // "f" key int handler__doft3_f(core_t *core, buf_t *buf) { - do_ft('f', 0); + do_ft(core, buf, 'f', 0); return 0; } // "t" key int handler__doft3_t(core_t *core, buf_t *buf) { - do_ft('t', 0); + do_ft(core, buf, 't', 0); return 0; } // "T" key int handler__doft3_T(core_t *core, buf_t *buf) { - do_ft('T', 0); + do_ft(core, buf, 'T', 0); return 0; } @@ -518,10 +519,10 @@ int handler__goto1(core_t *core, buf_t *buf) || (precount - P(P_OF)) > (filesize - 1L)) { beep(); } else { - setpage((PTR) (buf->mem + precount - P(P_OF))); + setpage(core, buf, (PTR) (buf->mem + precount - P(P_OF))); } } else { - setpage((PTR) (buf->mem + filesize - 1L)); + setpage(core, buf, (PTR) (buf->mem + filesize - 1L)); } return 0; } @@ -532,7 +533,7 @@ int handler__goto2(core_t *core, buf_t *buf) off_t inaddr; last_motion = buf->state.current; - ui__StatusMsg("Goto Hex Address: "); + bvim_info(core, buf, "Goto Hex Address: "); refresh(); getcmdstr(core, cmdstr, 19); if (cmdstr[0] == '^') { @@ -548,13 +549,13 @@ int handler__goto2(core_t *core, buf_t *buf) return -1; inaddr -= P(P_OF); if (inaddr < filesize) { - setpage(buf->mem + inaddr); + setpage(core, buf, buf->mem + inaddr); } else { if (filesize == 0L) return -1; sprintf(string, "Max. address of current file : %06lX", (long)(filesize - 1L + P(P_OF))); - ui__ErrorMsg(string); + bvim_error(core, buf, string); } return 0; } @@ -564,7 +565,7 @@ int handler__search_string1(core_t *core, buf_t *buf) { char ch = '?'; - clearstr(); + ui__clearstr(core, buf); addch(ch); refresh(); if (getcmdstr(core, line, 1)) @@ -579,7 +580,7 @@ int handler__search_string2(core_t *core, buf_t *buf) { char ch = '/'; - clearstr(); + ui__clearstr(core, buf); addch(ch); refresh(); if (getcmdstr(core, line, 1)) @@ -594,7 +595,7 @@ int handler__search_string3(core_t *core, buf_t *buf) { char ch = '#'; - clearstr(); + ui__clearstr(core, buf); addch(ch); refresh(); if (getcmdstr(core, line, 1)) @@ -609,7 +610,7 @@ int handler__search_string4(core_t *core, buf_t *buf) { char ch = '\\'; - clearstr(); + ui__clearstr(core, buf); addch(ch); refresh(); if (getcmdstr(core, line, 1)) @@ -632,7 +633,7 @@ int handler__search_next(core_t *core, buf_t *buf) //case 'm': int handler__mark(core_t *core, buf_t *buf) { - do_mark(vgetc(), buf->state.current); + do_mark(buf, vgetc(), buf->state.current); return 0; } @@ -644,7 +645,7 @@ int handler__goto_mark(core_t *core, buf_t *buf) // toggle(); mark = vgetc(); if (mark == '`' || mark == '\'') { - setpage(last_motion); + setpage(core, buf, last_motion); last_motion = buf->state.current; } else { if (mark < 'a' || mark > 'z') { @@ -654,7 +655,7 @@ int handler__goto_mark(core_t *core, buf_t *buf) beep(); return -1; } - setpage(markbuf[mark - 'a']); + setpage(core, buf, markbuf[mark - 'a']); } return 0; } @@ -685,7 +686,7 @@ int handler__paste(core_t *core, buf_t *buf) { if (precount < 1) precount = 1; - if ((undo_count = alloc_buf(yanked, &undo_buf)) == 0L) + if ((undo_count = alloc_buf(core, buf, yanked, &undo_buf)) == 0L) return -1; sprintf(rep_buf, "%ldP", precount); if (do_append(core, buf, yanked, yank_buf)) @@ -693,7 +694,7 @@ int handler__paste(core_t *core, buf_t *buf) /* we save it not for undo but for the dot command * memcpy(undo_buf, yank_buf, yanked); */ - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); return 0; } @@ -706,7 +707,7 @@ int handler__redo(core_t *core, buf_t *buf) if (precount < 1) precount = 1; sprintf(rep_buf, "%ld%c", precount, 'r'); - undo_count = edit('r'); + undo_count = edit(core, buf, 'r'); lflag++; return 0; } @@ -729,14 +730,14 @@ int handler__visual(core_t *core, buf_t *buf) buf->state.mode = BVI_MODE_VISUAL; // start selection buf->state.selection.start = get_cursor_position(); - bvim_info(buf->state.mode, "Started selection from %ld", buf->state.selection.start); + bvim_info(core, buf, "Started selection from %ld", buf->state.selection.start); return 0; } if (buf->state.mode == BVI_MODE_VISUAL) { buf->state.mode = BVI_MODE_EDIT; // end selection buf->state.selection.end = get_cursor_position(); - bvim_info(buf->state.mode, "Selected block [%ld, %ld]", buf->state.selection.start, buf->state.selection.end); + bvim_info(core, buf, "Selected block [%ld, %ld]", buf->state.selection.start, buf->state.selection.end); tmpblk.pos_start = buf->state.selection.start; tmpblk.pos_end = buf->state.selection.end; tmpblk.palette = 1; // TODO: something more nice @@ -744,7 +745,7 @@ int handler__visual(core_t *core, buf_t *buf) tmpblk.id = BVI_VISUAL_SELECTION_ID; // TODO: do something! blocks__Add(buf, tmpblk); ui__BlockHighlightAdd(&tmpblk); - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); return 0; } return 0; @@ -755,7 +756,7 @@ int handler__visual(core_t *core, buf_t *buf) int handler__wordsearch(core_t *core, buf_t *buf) { buf->state.loc = ASCII; - setpage(wordsearch(core, buf, buf->state.current, 'w')); + setpage(core, buf, wordsearch(core, buf, buf->state.current, 'w')); return 0; } @@ -767,12 +768,12 @@ int handler__yank(core_t *core, buf_t *buf) count = range(core, buf, 'y'); if (count > 0) { - if ((yanked = alloc_buf(count, &yank_buf)) == 0L) { + if ((yanked = alloc_buf(core, buf, count, &yank_buf)) == 0L) { return -1; } memcpy(yank_buf, buf->state.current, yanked); } else if (count < 0) { - if ((yanked = alloc_buf(-count, &yank_buf)) == 0L) { + if ((yanked = alloc_buf(core, buf, -count, &yank_buf)) == 0L) { return -1; } memcpy(yank_buf, buf->state.current + count, yanked); @@ -788,7 +789,7 @@ int handler__yank(core_t *core, buf_t *buf) //case 'z': int handler__doz(core_t *core, buf_t *buf) { - do_z(vgetc()); + do_z(core, buf, vgetc()); return 0; } @@ -826,9 +827,9 @@ int handler__insert(core_t *core, buf_t *buf) { sprintf(rep_buf, "%ldI", precount); buf->state.current = buf->mem; - setpage(buf->mem); - ui__Screen_Repaint(); - undo_count = edit('i'); + setpage(core, buf, buf->mem); + ui__Screen_Repaint(core, buf); + undo_count = edit(core, buf, 'i'); lflag++; return 0; } @@ -838,10 +839,10 @@ int handler__insert(core_t *core, buf_t *buf) int handler__s(core_t *core, buf_t *buf) { sprintf(rep_buf, "%lds", precount); - if (do_delete((off_t) precount, buf->state.current)) + if (do_delete(core, buf, (off_t) precount, buf->state.current)) return 0; precount = 1; - undo_count = edit('i'); + undo_count = edit(core, buf, 'i'); lflag++; return 0; } @@ -849,7 +850,7 @@ int handler__s(core_t *core, buf_t *buf) //case 'a': int handler__append2(core_t *core, buf_t *buf) { - if (cur_forw(1)) + if (cur_forw(core, buf, 1)) return 0; buf->state.current++; return 0; @@ -861,7 +862,7 @@ int handler__insert2(core_t *core, buf_t *buf) char ch = 'i'; sprintf(rep_buf, "%ld%c", precount, ch); - undo_count = edit(ch); + undo_count = edit(core, buf, ch); lflag++; return 0; } @@ -882,12 +883,12 @@ int handler__c_or_d(core_t *core, buf_t *buf) int count = range(core, buf, ch); if (count > 0) - do_delete((off_t) count, buf->state.current); + do_delete(core, buf, (off_t) count, buf->state.current); else if (count < 0) - do_back(-(off_t) count, buf->state.current); + do_back(core, buf, -(off_t) count, buf->state.current); if (ch == 'c') { precount = 1; - undo_count = edit('i'); + undo_count = edit(core, buf, 'i'); lflag++; // //} else if (count) { @@ -902,7 +903,7 @@ int handler__c_or_d(core_t *core, buf_t *buf) int handler__x(core_t *core, buf_t *buf) { sprintf(rep_buf, "%ldx", precount); - do_delete((off_t) precount, buf->state.current); + do_delete(core, buf, (off_t) precount, buf->state.current); return 0; } @@ -910,7 +911,7 @@ int handler__x(core_t *core, buf_t *buf) int handler__x2(core_t *core, buf_t *buf) { sprintf(rep_buf, "%ldX", precount); - do_back((off_t) precount, buf->state.current); + do_back(core, buf, (off_t) precount, buf->state.current); return 0; } @@ -974,40 +975,46 @@ void record_cmd(core_t* core, char* cmdline) * --------------------------------------------------------------- */ -void bvim_error(int mode, char* fmt, ...) +void bvim_error(core_t *core, buf_t *buf, char* fmt, ...) { char msg[1024]; va_list ap; + int mode = 0; msg[0] = '\0'; + mode = buf->state.mode; va_start(ap, fmt); vsprintf(msg, fmt, ap); va_end(ap); - ui__ErrorMsg(msg); + ui__ErrorMsg(core, buf, msg); } -void bvim_info(int mode, char* fmt, ...) +void bvim_info(core_t *core, buf_t *buf, char* fmt, ...) { char msg[1024]; va_list ap; + int mode = 0; msg[0] = '\0'; + mode = buf->state.mode; va_start(ap, fmt); vsprintf(msg, fmt, ap); va_end(ap); - ui__StatusMsg(msg); + ui__StatusMsg(core, buf, msg); } -void bvim_debug(int mode, char* fmt, ...) +void bvim_debug(core_t *core, buf_t *buf, char* fmt, ...) { char msg[1024]; va_list ap; + int mode = 0; msg[0] = '\0'; + mode = buf->state.mode; va_start(ap, fmt); vsprintf(msg, fmt, ap); @@ -1029,6 +1036,17 @@ void usage() exit(1); } +void set_core(core_t *core) +{ + coreslot = core; +} + +core_t* get_core() +{ + return coreslot; +} + + int main(int argc, char* argv[]) { int ch; @@ -1042,6 +1060,7 @@ int main(int argc, char* argv[]) // FIXME: Make normal buffer allocation function core.curbuf = &buf; + set_core(&core); /* Hotkeys and keymap initialization */ keys__Init(&core); @@ -1052,7 +1071,7 @@ int main(int argc, char* argv[]) #ifdef HAVE_LUA_H /* Lua subsystem initialization */ - bvim_lua_init(); + bvim_lua_init(&core); #endif /* Commands parser initialization */ @@ -1066,6 +1085,9 @@ int main(int argc, char* argv[]) core.curbuf->state.mode = BVI_MODE_EDIT; // default mode from start + /* !!!! JUST TEMPORARY HACK !!!!*/ + coretmp = &core; + poi = strrchr(argv[0], DELIM); if (poi) @@ -1182,12 +1204,12 @@ int main(int argc, char* argv[]) curfile = 0; /* UI initialization */ - ui__Init(); + ui__Init(&core, &buf); signal(SIGINT, SIG_IGN); - filesize = load(name); + filesize = load(&core, &buf, name); - bvim_init(argv[0]); + bvim_init(&core, argv[0]); params[P_TT].svalue = terminal; if (block_flag && (P(P_MM) == TRUE)) { P(P_MM) = FALSE; @@ -1201,9 +1223,9 @@ int main(int argc, char* argv[]) // Main loop do { setjmp(env); - buf.state.current = (PTR) (buf.state.pagepos + y * core.params.COLUMNS_DATA + xpos()); + buf.state.current = (PTR) (buf.state.pagepos + y * core.params.COLUMNS_DATA + xpos(&core, &buf)); if (wrstat) - statpos(); + statpos(&core, &buf); wrstat = 1; setcur(); ch = vgetc(); @@ -1320,7 +1342,7 @@ int main(int argc, char* argv[]) } */ if (lflag) - ui__lineout(); + ui__lineout(&core, &buf); } while (1); } @@ -1355,10 +1377,12 @@ off_t calc_size(char* arg) /* argument sig not used, because only SIGINT will be catched */ void jmpproc(int sig) { + core_t *core = NULL; + core = get_core(); if (P(P_EB)) beep(); - ui__Screen_Repaint(); - clearstr(); + ui__Screen_Repaint(core, core->curbuf); + ui__clearstr(core, core->curbuf); signal(SIGINT, SIG_IGN); longjmp(env, 0); } @@ -1384,7 +1408,7 @@ off_t range(core_t *core, buf_t *buf, int ch) case '/': /**** Search String ****/ case '\\': strcat(rep_buf, "\n"); - clearstr(); + ui__clearstr(core, buf); addch(ch1); refresh(); if (getcmdstr(core, line, 1)) @@ -1398,7 +1422,7 @@ off_t range(core_t *core, buf_t *buf, int ch) case '?': case '#': strcat(rep_buf, "\n"); - clearstr(); + ui__clearstr(core, buf); addch(ch1); refresh(); if (getcmdstr(core, line, 1)) @@ -1412,7 +1436,7 @@ off_t range(core_t *core, buf_t *buf, int ch) case 'f': case 't': precount = count; - end_addr = do_ft(ch1, 1); + end_addr = do_ft(core, buf, ch1, 1); if (!end_addr) { beep(); return 0; @@ -1421,7 +1445,7 @@ off_t range(core_t *core, buf_t *buf, int ch) case 'F': case 'T': precount = count; - start_addr = do_ft(ch1, 1); + start_addr = do_ft(core, buf, ch1, 1); if (!start_addr) { beep(); return 0; diff --git a/commands.c b/commands.c index 05c15a3..ff59a15 100644 --- a/commands.c +++ b/commands.c @@ -213,13 +213,13 @@ static char* tmp_cmd; int command__help(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { int i = 0; if (c_argc == 0) { - bvim_error(buf->state.mode, "What?"); + bvim_error(core, buf, "What?"); } else if (c_argc == 1) { while (i < core->cmdmap.items) { if (strncmp(c_argv[0], core->cmdmap.arr[i].name, strlen(core->cmdmap.arr[i].name))) { - bvim_error(buf->state.mode, "Command not exist!"); + bvim_error(core, buf, "Command not exist!"); } else { - bvim_info(buf->state.mode, core->cmdmap.arr[i].description); + bvim_info(core, buf, core->cmdmap.arr[i].description); return 0; } i++; @@ -234,10 +234,10 @@ int command__map(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv bkey_t *key_tmp; if (c_argc == 0) { - bvim_error(buf->state.mode, "Error: empty mapping definition!"); + bvim_error(core, buf, "Error: empty mapping definition!"); } else if (c_argc == 1) { if (strncmp(c_argv[0], "all", 3)) { - bvim_error(buf->state.mode, "Map what?"); + bvim_error(core, buf, "Map what?"); } else { keys__KeyMaps_Show(core); } @@ -249,7 +249,7 @@ int command__map(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv } key_tmp = keys__KeyString_Parse(core, c_argv[0]); if (!keys__Key_Map(core, key_tmp)) - bvim_error(buf->state.mode, "Error: can't set new key mapping!"); + bvim_error(core, buf, "Error: can't set new key mapping!"); return 0; } return -1; @@ -260,11 +260,11 @@ int command__unmap(core_t *core, buf_t *buf, char flags, int c_argc, char **c_ar bkey_t *key_tmp; if (c_argc != 1) { - bvim_error(buf->state.mode, "Error: empty mapping definition!"); + bvim_error(core, buf, "Error: empty mapping definition!"); } else { key_tmp = keys__KeyString_Parse(core, c_argv[0]); if (!keys__Key_Unmap(core, key_tmp)) - bvim_error(buf->state.mode, "Error: can't remove key mapping!"); + bvim_error(core, buf, "Error: can't remove key mapping!"); return 0; } return -1; @@ -274,7 +274,7 @@ int command__unmap(core_t *core, buf_t *buf, char flags, int c_argc, char **c_ar // :set int command__set(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { int n = 0; - if (chk_comm(buf, NO_ADDR)) + if (chk_comm(core, buf, NO_ADDR)) return -1; if (c_argc == 0) { doset(core, NULL); @@ -333,15 +333,15 @@ int command__block(core_t *core, buf_t *buf, char flags, int c_argc, char **c_ar ui__BlockHighlightAdd(&tmp_blk); // this we need to remove too blkblk = blocks__GetByID(buf, tmp_blk.id); - bvim_info(buf->state.mode, "Added block: start %ld end %ld", blkblk->pos_start, blkblk->pos_end); + bvim_info(core, buf, "Added block: start %ld end %ld", blkblk->pos_start, blkblk->pos_end); - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } else { - bvim_error(buf->state.mode, "Wrong block start and end values!"); + bvim_error(core, buf, "Wrong block start and end values!"); return -1; } } else { - bvim_error(buf->state.mode, "Wrong :block command format!"); + bvim_error(core, buf, "Wrong :block command format!"); return -1; } /* :block del */ @@ -351,13 +351,13 @@ int command__block(core_t *core, buf_t *buf, char flags, int c_argc, char **c_ar if (c_argc == 2) { blkblk = blocks__GetByID(buf, (unsigned int)atoi(c_argv[1])); // block id if (blkblk != NULL) { - bvim_info(buf->state.mode, "Block #%d [%ld, %ld], %s", blkblk->id, blkblk->pos_start, blkblk->pos_end, blkblk->annotation); + bvim_info(core, buf, "Block #%d [%ld, %ld], %s", blkblk->id, blkblk->pos_start, blkblk->pos_end, blkblk->annotation); } else { - bvim_error(buf->state.mode, "Can't find block with %d id!", (unsigned int)atoi(c_argv[1])); + bvim_error(core, buf, "Can't find block with %d id!", (unsigned int)atoi(c_argv[1])); return -1; } } else { - bvim_error(buf->state.mode, "Wrong :block command format!"); + bvim_error(core, buf, "Wrong :block command format!"); return -1; } /* :block list */ @@ -372,7 +372,7 @@ int command__block(core_t *core, buf_t *buf, char flags, int c_argc, char **c_ar blkblk = blocks__GetByID(buf, (unsigned int)atoi(c_argv[1])); // block id if (blkblk != NULL) { blkblk->folding = 1; - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } } /* :block annotation */ @@ -382,15 +382,15 @@ int command__block(core_t *core, buf_t *buf, char flags, int c_argc, char **c_ar if (blkblk != NULL) { blkblk->annotation = c_argv[2]; // FIXME: Use valid string copy function ! } else { - bvim_error(buf->state.mode, "Can't find block with %d id!", (unsigned int)atoi(c_argv[1])); + bvim_error(core, buf, "Can't find block with %d id!", (unsigned int)atoi(c_argv[1])); return -1; } } else { - bvim_error(buf->state.mode, "Wrong :block command format!"); + bvim_error(core, buf, "Wrong :block command format!"); return -1; } } else { - bvim_error(buf->state.mode, "Wrong :block command format!"); + bvim_error(core, buf, "Wrong :block command format!"); return -1; } } @@ -402,14 +402,14 @@ int command__lua(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv int n = 0; if (c_argc == 0) { - bvim_error(buf->state.mode, "Error: empty lua command!"); + bvim_error(core, buf, "Error: empty lua command!"); } else { luacmdbuf[0] = '\0'; for (n = 0; n < c_argc; n++) { strcat(luacmdbuf, " "); strcat(luacmdbuf, c_argv[n]); } - bvim_run_lua_string(luacmdbuf); + bvim_run_lua_string(core, buf, luacmdbuf); } return 0; } @@ -418,7 +418,7 @@ int command__lua(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv int command__args(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { int n = 0; - if (chk_comm(buf, NO_ADDR | NO_ARG)) + if (chk_comm(core, buf, NO_ADDR | NO_ARG)) return -1; string[0] = '\0'; for (n = 0; n < numfiles; n++) { @@ -429,16 +429,16 @@ int command__args(core_t *core, buf_t *buf, char flags, int c_argc, char **c_arg strcat(string, "]"); strcat(string, " "); } - bvim_info(buf->state.mode, string); + bvim_info(core, buf, string); return 0; } // :source int command__source(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | ONE_FILE)) + if (chk_comm(core, buf, NO_ADDR | ONE_FILE)) return -1; if (read_rc(core, c_argv[0])) - ui__SystemErrorMsg(c_argv[0]); + bvim_error(core, buf, c_argv[0]); refresh(); return 0; } @@ -446,24 +446,24 @@ int command__source(core_t *core, buf_t *buf, char flags, int c_argc, char **c_a // :run int command__run(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc == 0) { - bvim_error(buf->state.mode, "Error: empty plugin name!"); + bvim_error(core, buf, "Error: empty plugin name!"); } else { - bvim_run_lua_script(c_argv[0]); + bvim_run_lua_script(core, buf, c_argv[0]); } return 0; } // :cd int command__cd(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | ONE_FILE)) + if (chk_comm(core, buf, NO_ADDR | ONE_FILE)) return -1; if (!(flags & FLAG_FORCE)) { if (edits) { if (P(P_AW)) { - save(name, buf->mem, buf->maxpos, WRITE); + save(core, buf, name, WRITE); edits = 0; } else { - bvim_error(buf->state.mode, BVI_ERROR_NOWRITE, "cd"); + bvim_error(core, buf, BVI_ERROR_NOWRITE, "cd"); return -1; } } @@ -473,7 +473,7 @@ int command__cd(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) c_argv[0] = string; } if (chdir(c_argv[0])) - ui__SystemErrorMsg(c_argv[0]); + bvim_error(core, buf, c_argv[0]); return 0; } @@ -485,45 +485,45 @@ int command__edit(core_t *core, buf_t *buf, char flags, int c_argc, char **c_arg */ if (*tmp_cmd == 'e' && c_argc == 0) { if (tmp_cmd[1] == '!') - (void)doecmd(buf, &tmp_cmd[2], TRUE); + (void)doecmd(core, buf, &tmp_cmd[2], TRUE); else - (void)doecmd(buf, &tmp_cmd[1], FALSE); + (void)doecmd(core, buf, &tmp_cmd[1], FALSE); return 0; } - if (chk_comm(buf, NO_ADDR | MAX_ONE_FILE)) + if (chk_comm(core, buf, NO_ADDR | MAX_ONE_FILE)) return -1; - doecmd(buf, c_argv[0], flags & FLAG_FORCE); + doecmd(core, buf, c_argv[0], flags & FLAG_FORCE); return 0; } // :file int command__file(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | MAX_ONE_FILE)) + if (chk_comm(core, buf, NO_ADDR | MAX_ONE_FILE)) return -1; if (c_argc != 0) name = strdup(c_argv[0]); - fileinfo(name); + fileinfo(core, buf, name); return 0; } // :read int command__read(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | MAX_ONE_FILE)) + if (chk_comm(core, buf, NO_ADDR | MAX_ONE_FILE)) return -1; /* read reads a file at EOF */ if (c_argc == 0) c_argv[0] = name; - if (!addfile(c_argv[0])) { + if (!addfile(core, buf, c_argv[0])) { edits = U_TRUNC; undosize = filesize; - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } return 0; } // :xit int command__xit(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | NO_ARG)) + if (chk_comm(core, buf, NO_ADDR | NO_ARG)) return -1; do_exit(core); return 0; @@ -531,15 +531,15 @@ int command__xit(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv // :next int command__next(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | NO_ARG)) + if (chk_comm(core, buf, NO_ADDR | NO_ARG)) return -1; if (!(flags & FLAG_FORCE)) { if (edits) { if (P(P_AW)) { - save(name, buf->mem, buf->maxpos, WRITE); + save(core, buf, name, WRITE); edits = 0; } else { - bvim_error(buf->state.mode, BVI_ERROR_NOWRITE, "next"); + bvim_error(core, buf, BVI_ERROR_NOWRITE, "next"); return -1; } } @@ -552,23 +552,23 @@ int command__next(core_t *core, buf_t *buf, char flags, int c_argc, char **c_arg stuffin(files[++curfile]); stuffin("\n"); } else - bvim_error(buf->state.mode, "No more files@to edit!"); + bvim_error(core, buf, "No more files@to edit!"); return 0; } // :rewind int command__rewind(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | NO_ARG)) + if (chk_comm(core, buf, NO_ADDR | NO_ARG)) return -1; if (numfiles <= 1) /* nothing to rewind */ return -1; if (!(flags & FLAG_FORCE)) { if (edits) { if (P(P_AW)) { - save(name, buf->mem, buf->maxpos, WRITE); + save(core, buf, name, WRITE); edits = 0; } else { - bvim_error(buf->state.mode, BVI_ERROR_NOWRITE, "rewind"); + bvim_error(core, buf, BVI_ERROR_NOWRITE, "rewind"); return -1; } } @@ -585,35 +585,35 @@ int command__rewind(core_t *core, buf_t *buf, char flags, int c_argc, char **c_a // :append int command__append(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | MAX_ONE_ARG)) + if (chk_comm(core, buf, NO_ADDR | MAX_ONE_ARG)) return -1; - do_ins_chg(start_addr, c_argc == 1 ? c_argv[0] : "a", U_APPEND); + do_ins_chg(core, buf, start_addr, c_argc == 1 ? c_argv[0] : "a", U_APPEND); return 0; } // :change int command__change(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, MAX_ONE_ARG)) + if (chk_comm(core, buf, MAX_ONE_ARG)) return -1; if (!addr_flag) start_addr = buf->state.current; - do_ins_chg(start_addr, c_argc == 1 ? c_argv[0] : "a", U_EDIT); + do_ins_chg(core, buf, start_addr, c_argc == 1 ? c_argv[0] : "a", U_EDIT); return 0; } // :mark int command__mark(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc == 0) { - bvim_error(buf->state.mode, "Mark what?"); + bvim_error(core, buf, "Mark what?"); return -1; } if (c_argc > 1 || (strlen(c_argv[0]) > 1)) { - bvim_error(buf->state.mode, BVI_ERROR_EXTRACHARS); + bvim_error(core, buf, BVI_ERROR_EXTRACHARS); return -1; } if (!addr_flag) start_addr = buf->state.current; - do_mark(c_argv[0][0], start_addr); + do_mark(buf, c_argv[0][0], start_addr); return 0; } @@ -621,16 +621,16 @@ int command__mark(core_t *core, buf_t *buf, char flags, int c_argc, char **c_arg int command__yank(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if ((yanked = yd_addr(core, buf)) == 0L) return -1; - if ((yanked = alloc_buf(yanked, &yank_buf)) == 0L) + if ((yanked = alloc_buf(core, buf, yanked, &yank_buf)) == 0L) return -1; memcpy(yank_buf, start_addr, yanked); - bvim_info(buf->state.mode, "%lu bytes", (long)yanked); + bvim_info(core, buf, "%lu bytes", (long)yanked); return 0; } // :overwrite int command__overwrite(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ARG)) + if (chk_comm(core, buf, NO_ARG)) return -1; if (!addr_flag) start_addr = buf->state.current; @@ -640,7 +640,7 @@ int command__overwrite(core_t *core, buf_t *buf, char flags, int c_argc, char ** // :undo int command__undo(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | NO_ARG)) + if (chk_comm(core, buf, NO_ADDR | NO_ARG)) return -1; do_undo(core, buf); return 0; @@ -648,34 +648,34 @@ int command__undo(core_t *core, buf_t *buf, char flags, int c_argc, char **c_arg // :version int command__version(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | NO_ARG)) + if (chk_comm(core, buf, NO_ADDR | NO_ARG)) return -1; - bvim_info(buf->state.mode, "bvim version %s %s", VERSION, copyright); + bvim_info(core, buf, "bvim version %s %s", VERSION, copyright); return 0; } // :shell int command__shell(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | NO_ARG)) + if (chk_comm(core, buf, NO_ADDR | NO_ARG)) return -1; - do_shell(); + do_shell(core, buf); clear(); - ui__Screen_Repaint(); - clearstr(); + ui__Screen_Repaint(core, buf); + ui__clearstr(core, buf); return 0; } // :quit int command__quit(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { - if (chk_comm(buf, NO_ADDR | NO_ARG)) + if (chk_comm(core, buf, NO_ADDR | NO_ARG)) return -1; if (!(flags & FLAG_FORCE)) { if (edits) { - bvim_error(buf->state.mode, BVI_ERROR_NOWRITE, "quit"); + bvim_error(core, buf, BVI_ERROR_NOWRITE, "quit"); return -1; } else { if ((curfile + 1) < numfiles) { - bvim_error(buf->state.mode, "%d %s", numfiles - curfile - 1, BVI_ERROR_MOREFILES); + bvim_error(core,buf, "%d %s", numfiles - curfile - 1, BVI_ERROR_MOREFILES); return -1; } else quit(core, buf); @@ -691,13 +691,13 @@ int command__quit(core_t *core, buf_t *buf, char flags, int c_argc, char **c_arg // :sleft int command__sleft(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc == 1) { - math__logic(LSHIFT, c_argv[0]); + math__logic(core, buf, LSHIFT, c_argv[0]); } else if (c_argc == 0) { - math__logic(LSHIFT, "1"); + math__logic(core, buf, LSHIFT, "1"); } else if (c_argc == 2) { - math__logic_block(LSHIFT, c_argv[0], atoi(c_argv[1])); + math__logic_block(core, buf, LSHIFT, c_argv[0], atoi(c_argv[1])); } else { - bvim_error(buf->state.mode, BVI_ERROR_AMBVALUE); + bvim_error(core, buf, BVI_ERROR_AMBVALUE); return -1; } return 0; @@ -706,13 +706,13 @@ int command__sleft(core_t *core, buf_t *buf, char flags, int c_argc, char **c_ar // :sright int command__sright(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc == 1) { - math__logic(RSHIFT, c_argv[0]); + math__logic(core, buf, RSHIFT, c_argv[0]); } else if (c_argc == 0) { - math__logic(RSHIFT, "1"); + math__logic(core, buf, RSHIFT, "1"); } else if (c_argc == 2) { - math__logic_block(RSHIFT, c_argv[0], atoi(c_argv[1])); + math__logic_block(core, buf, RSHIFT, c_argv[0], atoi(c_argv[1])); } else { - bvim_error(buf->state.mode, BVI_ERROR_AMBVALUE); + bvim_error(core, buf, BVI_ERROR_AMBVALUE); return -1; } return 0; @@ -721,13 +721,13 @@ int command__sright(core_t *core, buf_t *buf, char flags, int c_argc, char **c_a // :rleft int command__rleft(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc == 1) { - math__logic(LROTATE, c_argv[0]); + math__logic(core, buf, LROTATE, c_argv[0]); } else if (c_argc == 0) { - math__logic(LROTATE, "1"); + math__logic(core, buf, LROTATE, "1"); } else if (c_argc == 2) { - math__logic_block(LROTATE, c_argv[0], atoi(c_argv[1])); + math__logic_block(core, buf, LROTATE, c_argv[0], atoi(c_argv[1])); } else { - bvim_error(buf->state.mode, BVI_ERROR_AMBVALUE); + bvim_error(core, buf, BVI_ERROR_AMBVALUE); return -1; } return 0; @@ -736,13 +736,13 @@ int command__rleft(core_t *core, buf_t *buf, char flags, int c_argc, char **c_ar // :rright int command__rright(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc == 1) { - math__logic(RROTATE, c_argv[0]); + math__logic(core, buf, RROTATE, c_argv[0]); } else if (c_argc == 0) { - math__logic(RROTATE, "1"); + math__logic(core, buf, RROTATE, "1"); } else if (c_argc == 2) { - math__logic_block(RROTATE, c_argv[0], atoi(c_argv[1])); + math__logic_block(core, buf, RROTATE, c_argv[0], atoi(c_argv[1])); } else { - bvim_error(buf->state.mode, BVI_ERROR_AMBVALUE); + bvim_error(core, buf, BVI_ERROR_AMBVALUE); return -1; } return 0; @@ -751,14 +751,14 @@ int command__rright(core_t *core, buf_t *buf, char flags, int c_argc, char **c_a // :and int command__and(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc == 1) { - math__logic(AND, c_argv[0]); + math__logic(core, buf, AND, c_argv[0]); } else if (c_argc == 0) { - bvim_error(buf->state.mode, BVI_ERROR_NOVAL); + bvim_error(core, buf, BVI_ERROR_NOVAL); return -1; } else if (c_argc == 2) { - math__logic_block(AND, c_argv[0], atoi(c_argv[1])); + math__logic_block(core, buf, AND, c_argv[0], atoi(c_argv[1])); } else { - bvim_error(buf->state.mode, BVI_ERROR_AMBVALUE); + bvim_error(core, buf, BVI_ERROR_AMBVALUE); return -1; } return 0; @@ -767,14 +767,14 @@ int command__and(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv // :or int command__or(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc == 1) { - math__logic(OR, c_argv[0]); + math__logic(core, buf, OR, c_argv[0]); } else if (c_argc == 0) { - bvim_error(buf->state.mode, BVI_ERROR_NOVAL); + bvim_error(core, buf, BVI_ERROR_NOVAL); return -1; } else if (c_argc == 2) { - math__logic_block(OR, c_argv[0], atoi(c_argv[1])); + math__logic_block(core, buf, OR, c_argv[0], atoi(c_argv[1])); } else { - bvim_error(buf->state.mode, BVI_ERROR_AMBVALUE); + bvim_error(core, buf, BVI_ERROR_AMBVALUE); return -1; } return 0; @@ -783,14 +783,14 @@ int command__or(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) // :xor int command__xor(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc == 1) { - math__logic(XOR, c_argv[0]); + math__logic(core, buf, XOR, c_argv[0]); } else if (c_argc == 0) { - bvim_error(buf->state.mode, BVI_ERROR_NOVAL); + bvim_error(core, buf, BVI_ERROR_NOVAL); return -1; } else if (c_argc == 2) { - math__logic_block(XOR, c_argv[0], atoi(c_argv[1])); + math__logic_block(core, buf, XOR, c_argv[0], atoi(c_argv[1])); } else { - bvim_error(buf->state.mode, BVI_ERROR_AMBVALUE); + bvim_error(core, buf, BVI_ERROR_AMBVALUE); return -1; } return 0; @@ -799,20 +799,20 @@ int command__xor(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv // :neg int command__neg(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc != 0) { - bvim_error(buf->state.mode, BVI_ERROR_EXTRACHARS); + bvim_error(core, buf, BVI_ERROR_EXTRACHARS); return -1; } - math__logic(NEG, "255"); + math__logic(core, buf, NEG, "255"); return 0; } // :not int command__not(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv) { if (c_argc != 0) { - bvim_error(buf->state.mode, BVI_ERROR_EXTRACHARS); + bvim_error(core, buf, BVI_ERROR_EXTRACHARS); return -1; } - math__logic(NOT, "255"); + math__logic(core, buf, NOT, "255"); return 0; } @@ -823,14 +823,14 @@ int command__fuz(core_t *core, buf_t *buf, char flags, int c_argc, char **c_argv int distance; if (c_argc == 1) { distance = 1; - fnd = fuzzy_search(buf->mem, filesize, c_argv[0], strlen(c_argv[0]), FUZZY_BITAP_HAMMINGTON_DISTANCE, distance); - bvim_info(buf->state.mode, "Found %d matches", fnd.cnt); + fnd = fuzzy_search(core, buf, filesize, c_argv[0], strlen(c_argv[0]), FUZZY_BITAP_HAMMINGTON_DISTANCE, distance); + bvim_info(core, buf, "Found %d matches", fnd.cnt); } else if (c_argc == 2) { distance = atoi(c_argv[0]); - fnd = fuzzy_search(buf->mem, filesize, c_argv[1], strlen(c_argv[1]), FUZZY_BITAP_HAMMINGTON_DISTANCE, distance); - bvim_info(buf->state.mode, "Found %d matches", fnd.cnt); + fnd = fuzzy_search(core, buf, filesize, c_argv[1], strlen(c_argv[1]), FUZZY_BITAP_HAMMINGTON_DISTANCE, distance); + bvim_info(core, buf, "Found %d matches", fnd.cnt); } else { - bvim_error(buf->state.mode, BVI_ERROR_EXTRACHARS); + bvim_error(core, buf, BVI_ERROR_EXTRACHARS); return -1; } return 0; @@ -863,7 +863,7 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) if (*cmdline == '"') return; /** comment **/ if (strlen(cmdline) > CMDSZ - 2) { - ui__ErrorMsg("Command too long"); + bvim_error(core, buf, "Command too long"); return; } strcpy(buff, cmdline); @@ -898,15 +898,15 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) } } SKIP_WHITE if (start_addr > (end_addr + 1)) { - bvim_error(buf->state.mode, "Addr1 > addr2|First address exceeds second"); + bvim_error(core, buf, "Addr1 > addr2|First address exceeds second"); return; } if ((end_addr + 1) > buf->maxpos) { - bvim_error(buf->state.mode, "Not that many bytes@in buffer"); + bvim_error(core, buf, "Not that many bytes@in buffer"); return; } if (start_addr < buf->mem) { - bvim_error(buf->state.mode, "Negative address@- first byte is %ld", P(P_OF)); + bvim_error(core, buf, "Negative address@- first byte is %ld", P(P_OF)); return; } @@ -914,7 +914,7 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) /**** End of address range calculation ****/ if (*cmd == '\0') { - setpage(end_addr); + setpage(core, buf, end_addr); return; } strcpy(cmdbuf, cmd); /* save the unmodified command */ @@ -922,26 +922,26 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) if (*cmd == '!') { if (*(cmdbuf + 1) == '\0') { - bvim_error(buf->state.mode, "Incomplete shell escape command@- use 'shell' to get a shell"); + bvim_error(core, buf, "Incomplete shell escape command@- use 'shell' to get a shell"); return; } if (*(cmdbuf + 1) == '!') { if (oldbuf[0] != '\0') { strcpy(cmdbuf + 1, oldbuf); - bvim_info(buf->state.mode, oldbuf); + bvim_info(core, buf, oldbuf); } else { - bvim_error(buf->state.mode, "No previous command@to substitute for !"); + bvim_error(core, buf, "No previous command@to substitute for !"); return; } } else sprintf(oldbuf, "\n%s\n", cmdbuf + 1); if (P(P_AW)) { - save(name, buf->mem, buf->maxpos, WRITE); + save(core, buf, name, WRITE); edits = 0; } if (edits) - bvim_info(buf->state.mode, "[No write]|[No write since last change]"); + bvim_info(core, buf, "[No write]|[No write since last change]"); savetty(); endwin(); shresult = system(cmdbuf + 1); @@ -950,8 +950,8 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) fprintf(stderr, "[Hit return to continue]"); getchar(); doupdate(); - ui__Screen_Repaint(); - clearstr(); + ui__Screen_Repaint(core, buf); + ui__clearstr(core, buf); return; } @@ -959,7 +959,7 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) while (*cmd >= 'a' && *cmd <= 'z') { cmdname[n++] = *cmd++; if (n == MAXNAME) { - bvim_error(buf->state.mode, "What?|%s: Not an editor command 1", cmdbuf); + bvim_error(core, buf, "What?|%s: Not an editor command 1", cmdbuf); return; } } @@ -972,14 +972,14 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) SKIP_WHITE if (!strncmp("substitute", cmdname, len) && CMDLNG(10, 1)) { repl_count = do_substitution(core, buf, *cmd, cmd + 1, start_addr, end_addr); if (repl_count == -1) { - bvim_error(buf->state.mode, "No previous substitute re|No previous substitute regular expression"); + bvim_error(core, buf, "No previous substitute re|No previous substitute regular expression"); return; /* No prev subst */ } - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); if (!repl_count) { - bvim_error(buf->state.mode, "Fail|Substitute pattern matching failed"); + bvim_error(core, buf, "Fail|Substitute pattern matching failed"); } else if (repl_count > 1) { - bvim_info(buf->state.mode, "%d subs|%d substitutions", repl_count, repl_count); + bvim_info(core, buf, "%d subs|%d substitutions", repl_count, repl_count); } return; } else if (!strncmp("global", cmdname, len) && CMDLNG(6, 1)) { @@ -988,26 +988,26 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) addch('\n'); while ((buf->state.current = searching(core, buf, *cmd, cmd + 1, buf->state.current, end_addr, FALSE | S_GLOBAL)) != 0L) { addch('\n'); - ui__Line_Print(buf->state.current, core->screen.maxy - 1); + ui__Line_Print(core, buf, buf->state.current, core->screen.maxy - 1); repl_count++; if (repl_count == LINES - 2) { - if (wait_return(core, FALSE)) + if (wait_return(core, buf, FALSE)) return; repl_count = 0; } } if (repl_count) { - wait_return(core, TRUE); + wait_return(core, buf, TRUE); } else { - ui__Screen_Repaint(); - bvim_error(buf->state.mode, BVI_ERROR_PATNOTFOUND); + ui__Screen_Repaint(core, buf); + bvim_error(core, buf, BVI_ERROR_PATNOTFOUND); } return; } else if (cmdname[0] == 't') { if (!addr_flag) start_addr = buf->state.current; cmd = cmdname + 1L; - SKIP_WHITE do_mark(*cmd, start_addr); + SKIP_WHITE do_mark(buf, *cmd, start_addr); return; } @@ -1018,11 +1018,11 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) cmd++; saveflag = APPEND; } else { - bvim_error(buf->state.mode, "Write forms are 'w' and 'w>>'"); + bvim_error(core, buf, "Write forms are 'w' and 'w>>'"); return; } } else if (*cmd == '!') { - bvim_error(buf->state.mode, "Not yet implemented"); + bvim_error(core, buf, "Not yet implemented"); return; } else { saveflag = WRITE; @@ -1034,7 +1034,7 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) c_argc++; if (c_argc > 1) { - bvim_error(buf->state.mode, BVI_ERROR_AMBIGOUS); + bvim_error(core, buf, BVI_ERROR_AMBIGOUS); return; } if (c_argc == 1) { @@ -1042,7 +1042,7 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) while ((p = strchr(c_argv[0], '%')) != NULL && *(p - 1) != '\\') { if (name == NULL) { - bvim_error(buf->state.mode, "No filename@to substitute for %"); + bvim_error(core, buf, "No filename@to substitute for %"); return; } *p = '\0'; @@ -1056,24 +1056,20 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) name = strdup(c_argv[0]); if (flags & FLAG_FORCE) { if (c_argc == 0) - ok = save(name, start_addr, end_addr, saveflag); + ok = save(core, buf, name, saveflag); else - ok = save(c_argv[0], start_addr, end_addr, - saveflag); + ok = save(core, buf, c_argv[0], saveflag); } else { if (c_argc == 0) { if (P(P_RO)) { - bvim_error(buf->state.mode, "\"%s\" File is read only", name); + bvim_error(core, buf, "\"%s\" File is read only", name); return; } else - ok = save(name, start_addr, end_addr, - saveflag); + ok = save(core, buf, name, saveflag); } else { if (!stat(c_argv[0], &bbuf)) { if (saveflag == WRITE) { - bvim_error(buf->state.mode , - "File exists@- use \"%s! %s\" to overwrite", - cmdname, c_argv[0]); + bvim_error(core, buf, "File exists@- use \"%s! %s\" to overwrite", cmdname, c_argv[0]); return; } else { /* APPEND */ /* We can only append to a regular file! */ @@ -1082,15 +1078,14 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) filemode = REGULAR; } else if (S_ISBLK(bbuf.st_mode)) { - bvim_error(buf->state.mode, "Cannot append to a block special file"); + bvim_error(core, buf, "Cannot append to a block special file"); return; } - ok = save(c_argv[0], start_addr, - end_addr, saveflag); + ok = save(core, buf, c_argv[0], saveflag); } } else { if (saveflag == APPEND) { - bvim_error(buf->state.mode, "No such file"); + bvim_error(core, buf, "No such file"); return; } else { /* WRITE */ /* If we write the block of a partial file to a new file, it will @@ -1098,8 +1093,7 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) */ if (filemode == PARTIAL) filemode = REGULAR; - ok = save(c_argv[0], start_addr, - end_addr, saveflag); + ok = save(core, buf, c_argv[0], saveflag); } } } @@ -1124,20 +1118,16 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) while (j < core->cmdmap.items) { if (!strncmp(core->cmdmap.arr[j].name, cmdname, len) && CMDLNG(core->cmdmap.arr[j].size1, core->cmdmap.arr[j].size2) && (core->cmdmap.arr[j].enabled == 1)) { - if ((core->cmdmap.arr[j].handler_type == - BVI_HANDLER_INTERNAL) & (core->cmdmap.arr[j].handler. - func != NULL)) { + if ((core->cmdmap.arr[j].handler_type == BVI_HANDLER_INTERNAL) & (core->cmdmap.arr[j].handler.func != NULL)) { (*(core->cmdmap.arr[j].handler.func)) (core, buf, flags, c_argc, c_argv); cmd_recognized = 1; break; - } else - if ((core->cmdmap.arr[j].handler_type == - BVI_HANDLER_LUA) & (core->cmdmap.arr[j].handler. - lua_cmd != NULL)) { - bvim_run_lua_string(core->cmdmap.arr[j].handler. - lua_cmd); - cmd_recognized = 1; - break; + } else { + if ((core->cmdmap.arr[j].handler_type == BVI_HANDLER_LUA) & (core->cmdmap.arr[j].handler.lua_cmd != NULL)) { + bvim_run_lua_string(core, buf, core->cmdmap.arr[j].handler.lua_cmd); + cmd_recognized = 1; + break; + } } } j++; @@ -1149,38 +1139,37 @@ void docmdline(core_t *core, buf_t *buf, char* cmdline) if (!strncmp("delete", cmdname, len) && CMDLNG(6, 1)) { if ((undo_count = yd_addr(core, buf)) == 0L) return; - do_delete(undo_count, start_addr); - bvim_info(buf->state.mode, "%lu bytes", (long)undo_count); + do_delete(core, buf, undo_count, start_addr); + bvim_info(core, buf, "%lu bytes", (long)undo_count); } else if (!strncmp("insert", cmdname, len) && CMDLNG(6, 1)) { - if (chk_comm(buf, MAX_ONE_ARG)) + if (chk_comm(core, buf, MAX_ONE_ARG)) return; if (!addr_flag) start_addr = buf->state.current; - do_ins_chg(start_addr - 1, - c_argc == 1 ? c_argv[0] : "a", - U_INSERT); + do_ins_chg(core, buf, start_addr - 1, + c_argc == 1 ? c_argv[0] : "a", U_INSERT); } else if (!strncmp("put", cmdname, len) && CMDLNG(3, 2)) { - if (chk_comm(buf, NO_ARG)) + if (chk_comm(core, buf, NO_ARG)) return; if (!addr_flag) start_addr = buf->state.current; do_put(core, buf, start_addr, yanked, yank_buf); } else { - bvim_error(buf->state.mode, string); + bvim_error(core, buf, string); } } else { if (!strncmp("delete", cmdname, len) && CMDLNG(6, 1)) { - movebyte(); + movebyte(core, buf); } else if (!strncmp("insert", cmdname, len) && CMDLNG(6, 1)) { - movebyte(); + movebyte(core, buf); } else if (!strncmp("put", cmdname, len) && CMDLNG(3, 2)) { - movebyte(); + movebyte(core, buf); } else { - bvim_error(buf->state.mode, string); + bvim_error(core, buf, string); } } } @@ -1216,7 +1205,7 @@ off_t yd_addr(core_t *core, buf_t *buf) break; } } else { - bvim_error(buf->state.mode, BVI_ERROR_AMBVALUE); + bvim_error(core, buf, BVI_ERROR_AMBVALUE); return 0; } return count; @@ -1226,27 +1215,27 @@ void do_exit(core_t *core) { // FIXME: implement function, which check all buffers if (edits) { - if (!save(name, core->curbuf->mem, core->curbuf->maxpos - 1L, WRITE)) + if (!save(core, core->curbuf, name, WRITE)) return; } if ((curfile + 1) < numfiles) { - bvim_error(core->curbuf->state.mode, "%d %s", numfiles - curfile - 1, BVI_ERROR_MOREFILES); + bvim_error(core, core->curbuf, "%d %s", numfiles - curfile - 1, BVI_ERROR_MOREFILES); } else { #ifdef HAVE_LUA_H - bvim_lua_finish(); + bvim_lua_finish(core); #endif quit(core, core->curbuf); } } -int doecmd(buf_t *buf, char* arg, int flags) +int doecmd(core_t *core, buf_t *buf, char* arg, int flags) { char *tmp; if (*arg == '\0') arg = NULL; if (!(flags & FLAG_FORCE) && edits) { - bvim_error(buf->state.mode, BVI_ERROR_NOWRITE, "edit"); + bvim_error(core, buf, BVI_ERROR_NOWRITE, "edit"); /* if (altfile) free(altfile); @@ -1261,7 +1250,7 @@ int doecmd(buf_t *buf, char* arg, int flags) } if (name != NULL && !strcmp(arg, "#")) { if (altfile == NULL) { - bvim_error(buf->state.mode, "No alternate filename@to substitute for #"); + bvim_error(core, buf, "No alternate filename@to substitute for #"); return FALSE; } tmp = name; @@ -1276,25 +1265,25 @@ int doecmd(buf_t *buf, char* arg, int flags) } } if (name == NULL) { - bvim_error(buf->state.mode, "No file|No current filename"); + bvim_error(core, buf, "No file|No current filename"); return FALSE; } edits = 0; - filesize = load(name); + filesize = load(core, buf, name); if (arg == NULL) { - setpage(buf->state.current < buf->maxpos ? buf->state.current : buf->maxpos - 1L); + setpage(core, buf, buf->state.current < buf->maxpos ? buf->state.current : buf->maxpos - 1L); } return TRUE; } /* If flag == TRUE we do a ui__Screen_Repaint */ -int wait_return(core_t *core, int flag) +int wait_return(core_t *core, buf_t *buf, int flag) { int c; signal(SIGINT, jmpproc); - clearstr(); + ui__clearstr(core, buf); attrset(A_REVERSE); mvaddstr(core->screen.maxy, 0, "[Hit return to continue]"); attrset(A_NORMAL); @@ -1302,9 +1291,9 @@ int wait_return(core_t *core, int flag) c = getch(); if (flag) { clear(); - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } - clearstr(); + ui__clearstr(core, buf); signal(SIGINT, SIG_IGN); if (c != CR && c != NL && c != ' ' && c != ':' && c != KEY_ENTER) return 1; @@ -1312,27 +1301,28 @@ int wait_return(core_t *core, int flag) } /* check command arguments for integrity */ -int chk_comm(buf_t* buf, int flag) +int chk_comm(core_t *core, buf_t* buf, int flag) { if ((flag & NO_ADDR) && (addr_flag > 0)) { - bvim_error(buf->state.mode, BVI_ERROR_NOADDR); + bvim_error(core, buf, BVI_ERROR_NOADDR); return 1; } if ((flag & NO_ARG) && (c_argc > 0)) { - bvim_error(buf->state.mode, BVI_ERROR_EXTRACHARS); + bvim_error(core, buf, BVI_ERROR_EXTRACHARS); return 1; } if ((flag & MAX_ONE_ARG) && (c_argc > 1)) { - bvim_error(buf->state.mode, BVI_ERROR_AMBVALUE); + bvim_error(core, buf, BVI_ERROR_AMBVALUE); return 1; } if ((flag & ONE_FILE) && (c_argc == 0)) { - bvim_error(buf->state.mode, "Missing filename"); + bvim_error(core, buf, "Missing filename"); return 1; } if ((flag & MAX_ONE_FILE) && (c_argc > 1)) { - bvim_error(buf->state.mode, BVI_ERROR_AMBIGOUS); + bvim_error(core, buf, BVI_ERROR_AMBIGOUS); return 1; } return 0; } + diff --git a/edit.c b/edit.c index 7494ea4..d9d0662 100644 --- a/edit.c +++ b/edit.c @@ -25,6 +25,7 @@ #include "commands.h" #include "set.h" #include "ui.h" +#include "messages.h" extern int precount; extern core_t core; @@ -65,7 +66,7 @@ static char *getcnext = NULL; * for insert and append we misuse the undo buffer for the inserted * characters (for "." command) */ -off_t edit(int mode) +off_t edit(core_t *core, buf_t *buf, int mode) { unsigned int ch, ch1; size_t len; @@ -77,7 +78,7 @@ off_t edit(int mode) mode = 'A'; } if (mode != 'A' && mode != 'a') { - if (state.current - core.editor.mem >= filesize) { + if (buf->state.current - buf->mem >= filesize) { beep(); return 0L; } @@ -85,12 +86,12 @@ off_t edit(int mode) if (precount < 1) precount = 1; len = strlen(rep_buf); - if (mode == 'r' && state.current + precount > core.editor.maxpos) { + if (mode == 'r' && buf->state.current + precount > buf->maxpos) { beep(); rep_buf[len] = '\0'; return 0L; } - if (alloc_buf(buffer, &undo_buf) == 0L) { + if (alloc_buf(core, buf, buffer, &undo_buf) == 0L) { rep_buf[len] = '\0'; return 0L; } @@ -100,26 +101,26 @@ off_t edit(int mode) break; case 'R': edits = U_EDIT; - smsg("REPLACE MODE"); + ui__smsg(core, buf, "REPLACE MODE"); break; case 'r': edits = U_EDIT; - smsg("REPLACE 1 CHAR"); + ui__smsg(core, buf, "REPLACE 1 CHAR"); break; case 'a': case 'i': edits = U_INSERT; - smsg("INSERT MODE"); + ui__smsg(core, buf, "INSERT MODE"); break; } - undo_start = state.current; + undo_start = buf->state.current; while ((ch = vgetc()) != ESC) { ch &= 0xff; rep_buf[len++] = ch; if (ch == '\t') { - toggle(); + toggle(core, buf); setcur(); continue; } @@ -129,16 +130,16 @@ off_t edit(int mode) count--; if (mode == 'A' || mode == 'a' || mode == 'i') { filesize--; - core.editor.maxpos--; + buf->maxpos--; } - state.current--; - cur_back(); + buf->state.current--; + cur_back(core, buf); setcur(); } else beep(); continue; } - if (state.loc == HEX) { + if (buf->state.loc == HEX) { if (isxdigit(ch)) { mvaddch(y, x + 1, ' '); mvaddch(y, x, ch); @@ -146,8 +147,8 @@ off_t edit(int mode) ch1 = vgetc() & 0xff; if (ch1 == ESC) { mvaddch(y, x, ' '); - state.current--; - cur_back(); + buf->state.current--; + cur_back(core, buf); goto escape; } if (!isxdigit(ch1)) { @@ -172,44 +173,44 @@ off_t edit(int mode) goto wrong; } } - core.editor.curpos = state.current++; + buf->state.curpos = buf->state.current++; if (mode == 'i' || mode == 'a') { - memmove(state.current, core.editor.curpos, core.editor.maxpos - core.editor.curpos); + memmove(buf->state.current, buf->state.curpos, buf->maxpos - buf->state.curpos); } if (mode == 'A' || mode == 'i' || mode == 'a') { - core.editor.maxpos++; + buf->maxpos++; filesize++; /* NEU undo_buf[count++] = ch; */ count++; } else { - undo_buf[count++] = *core.editor.curpos; + undo_buf[count++] = *(buf->state.curpos); } if (count == buffer) { buffer += BUFFER; - if (alloc_buf(buffer, &undo_buf) == 0L) { + if (alloc_buf(core, buf, buffer, &undo_buf) == 0L) { rep_buf[len] = '\0'; return count; } } - *core.editor.curpos = (char)ch; - cur_forw(0); - statpos(); + *(buf->state.curpos) = (char)ch; + cur_forw(core, buf, 0); + statpos(core, buf); if (mode == 'i' || mode == 'a') { - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } else { - ui__lineout(); + ui__lineout(core, buf); } setcur(); if (filesize > memsize - 2L) { - if (enlarge(100L)) + if (enlarge(core, buf, 100L)) break; } - if ((mode != 'A' && mode != 'a') && core.editor.curpos == core.editor.maxpos - 1) + if ((mode != 'A' && mode != 'a') && buf->state.curpos == buf->maxpos - 1) break; if (mode == 'r') { break; @@ -229,17 +230,16 @@ off_t edit(int mode) case 'A': psize = count * (precount - 1); if (filesize + psize > memsize - 2L) { - if (enlarge(psize + 100L)) + if (enlarge(core, buf, psize + 100L)) return count; } if (psize + count > buffer) { - if (alloc_buf(psize + count, &undo_buf) == 0L) + if (alloc_buf(core, buf, psize + count, &undo_buf) == 0L) return count; } if (mode == 'i' || mode == 'a') { - memmove(state.current + psize, state.current, - core.editor.maxpos - core.editor.curpos); + memmove(buf->state.current + psize, buf->state.current, buf->maxpos - buf->state.curpos); } /* NEU @@ -250,46 +250,46 @@ off_t edit(int mode) memcpy(undo_pos + 1L, undo_pos - count + 1L, count); undo_pos += count; */ - memcpy(core.editor.curpos + 1L, core.editor.curpos - count + 1L, count); - core.editor.curpos += count; + memcpy(buf->state.curpos + 1L, buf->state.curpos - count + 1L, count); + buf->state.curpos += count; } filesize += psize; count += psize; - core.editor.maxpos += psize; + buf->maxpos += psize; undo_count += psize; - state.current = core.editor.curpos + 1L; - setpage(state.current); - ui__Screen_Repaint(); + buf->state.current = buf->state.curpos + 1L; + setpage(core, buf, buf->state.current); + ui__Screen_Repaint(core, buf); break; case 'R': - if (state.current + count * (precount - 1) > core.editor.maxpos) + if (buf->state.current + count * (precount - 1) > buf->maxpos) break; psize = count; while (--precount) { - memcpy(undo_buf + psize, core.editor.curpos + 1L, count); + memcpy(undo_buf + psize, buf->state.curpos + 1L, count); psize += count; - memcpy(core.editor.curpos + 1L, core.editor.curpos - count + 1L, count); - core.editor.curpos += count; + memcpy(buf->state.curpos + 1L, buf->state.curpos - count + 1L, count); + buf->state.curpos += count; } count = psize; - setpage(++core.editor.curpos); - ui__Screen_Repaint(); + setpage(core, buf, ++(buf->state.curpos)); + ui__Screen_Repaint(core, buf); break; case 'r': while (--precount) { - undo_buf[count++] = *(++core.editor.curpos); - *core.editor.curpos = (char)ch; - cur_forw(0); - statpos(); - ui__lineout(); + undo_buf[count++] = *(++(buf->state.curpos)); + *(buf->state.curpos) = (char)ch; + cur_forw(core, buf, 0); + statpos(core, buf); + ui__lineout(core, buf); } break; } } - cur_back(); + cur_back(core, buf); escape: setcur(); - smsg(""); + ui__smsg(core, buf, ""); return (count); } @@ -356,13 +356,13 @@ PTR do_ft(core_t *core, buf_t *buf, int ch, int flag) } while (--precount > 0); if (*ptr == chi) { if (buf->state.loc == HEX) - toggle(); + toggle(core, buf); if (chp == 't') ptr--; if (chp == 'T') ptr++; if (!flag) { - setpage(ptr); + setpage(core, buf, ptr); } return (ptr); } @@ -406,7 +406,7 @@ void do_z(core_t *core, buf_t *buf, int mode) beep(); break; } - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } /* Scroll down on lines */ @@ -419,7 +419,7 @@ void scrolldown(core_t *core, buf_t *buf, int lines) beep(); lines = 0; } - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); refresh(); } } @@ -434,7 +434,7 @@ void scrollup(core_t *core, buf_t *buf, int lines) beep(); lines = 0; } - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); refresh(); } } @@ -451,7 +451,7 @@ int xpos(core_t *core, buf_t *buf) int get_cursor_position(core_t *core, buf_t *buf) { - return (buf->state.current - core->editor.mem); + return (buf->state.current - buf->mem); } /* toggle between ASCII and HEX windows positions */ @@ -510,8 +510,7 @@ void statpos(core_t *core, buf_t *buf) } else strcpy(str, " "); - sprintf(string, "%08lX \\%03o 0x%02X %3d %3s", - (long)(bytepos + P(P_OF)), Char1, Char1, Char1, str); + sprintf(string, "%08lX \\%03o 0x%02X %3d %3s", (long)(bytepos + P(P_OF)), Char1, Char1, Char1, str); // ncurses ! Split this out ! attrset(A_BOLD); @@ -552,7 +551,7 @@ void setpage(core_t *core, buf_t *buf, PTR addr) x = core->params.COLUMNS_ADDRESS + core->params.COLUMNS_HEX + ((addr - buf->state.pagepos) - y * core->params.COLUMNS_DATA); - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } } @@ -580,15 +579,15 @@ int cur_forw(core_t *core, buf_t *buf, int check) } else x = core->params.COLUMNS_ADDRESS; } - statpos(); - ui__lineout(); + statpos(core, buf); + ui__lineout(core, buf); if (y < core->screen.maxy - 1) { y++; return 0; } else { if (buf->state.pagepos < (PTR) (buf->mem + filesize)) { buf->state.pagepos += core->params.COLUMNS_DATA; - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); return 0; } else { beep(); @@ -618,15 +617,15 @@ int cur_back(core_t *core, buf_t *buf) core->params.COLUMNS_HEX - 3; } } - statpos(); - ui__lineout(); + statpos(core, buf); + ui__lineout(core, buf); if (y > 0) { y--; return 0; } else { if (buf->state.pagepos > buf->mem) { buf->state.pagepos -= core->params.COLUMNS_DATA; - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); return 0; } else { beep(); @@ -635,7 +634,7 @@ int cur_back(core_t *core, buf_t *buf) } } -void fileinfo(char* fname) +void fileinfo(core_t* core, buf_t* buf, char* fname) { off_t bytepos; char fstatus[64]; @@ -652,16 +651,15 @@ void fileinfo(char* fname) if (edits) strcat(string, "[Modified] "); if (filesize) { - bytepos = - (state.pagepos + y * core.params.COLUMNS_DATA + xpos()) - - core.editor.mem + 1L; + bytepos = (buf->state.pagepos + y * core->params.COLUMNS_DATA + xpos(core, buf)) - + buf->mem + 1L; sprintf(fstatus, "byte %lu of %lu --%lu%%--", (long)bytepos, (long)filesize, (long)(bytepos * 100L / filesize)); strcat(string, fstatus); } else { strcat(string, " 0 bytes"); } - ui__StatusMsg(string); + bvim_info(core, buf, string); } int vgetc() @@ -692,55 +690,55 @@ void stuffin(char* s) strcat(getcbuff, s); } -void do_back(off_t n, PTR start) +void do_back(core_t* core, buf_t* buf, off_t n, PTR start) { - if (start - n < core.editor.mem) { + if (start - n < buf->mem) { beep(); return; } - if ((undo_count = alloc_buf(n, &undo_buf)) == 0L) + if ((undo_count = alloc_buf(core, buf, n, &undo_buf)) == 0L) return; - yanked = alloc_buf(n, &yank_buf); + yanked = alloc_buf(core, buf, n, &yank_buf); edits = U_BACK; undo_start = start - n; memcpy(undo_buf, start - undo_count, undo_count); memcpy(yank_buf, start - undo_count, undo_count); - memmove(start - undo_count, start, core.editor.maxpos - start); + memmove(start - undo_count, start, buf->maxpos - start); filesize -= undo_count; - core.editor.maxpos -= undo_count; - setpage(start - undo_count); - ui__Screen_Repaint(); + buf->maxpos -= undo_count; + setpage(core, buf, start - undo_count); + ui__Screen_Repaint(core, buf); } -int do_delete(off_t n, PTR start) +int do_delete(core_t *core, buf_t* buf, off_t n, PTR start) { - if (n + start > core.editor.maxpos) { + if (n + start > buf->maxpos) { beep(); return 1; } - if ((undo_count = alloc_buf(n, &undo_buf)) == 0L) + if ((undo_count = alloc_buf(core, buf, n, &undo_buf)) == 0L) return 1; - yanked = alloc_buf(n, &yank_buf); + yanked = alloc_buf(core, buf, n, &yank_buf); edits = U_DELETE; undo_start = start; memcpy(undo_buf, start, undo_count); memcpy(yank_buf, start, undo_count); - memmove(start, start + undo_count, core.editor.maxpos - (start + undo_count)); + memmove(start, start + undo_count, buf->maxpos - (start + undo_count)); filesize -= undo_count; - core.editor.maxpos -= undo_count; - if (start == core.editor.maxpos && start > core.editor.mem) { + buf->maxpos -= undo_count; + if (start == buf->maxpos && start > buf->mem) { start--; - cur_back(); + cur_back(core, buf); } - setpage(start); - ui__Screen_Repaint(); + setpage(core, buf, start); + ui__Screen_Repaint(core, buf); return 0; } /* * The :insert, :append and :change command */ -void do_ins_chg(PTR start, char* arg, int mode) +void do_ins_chg(core_t* core, buf_t* buf, PTR start, char* arg, int mode) { int base; off_t buffer = BUFFER; @@ -750,7 +748,7 @@ void do_ins_chg(PTR start, char* arg, int mode) char *tempbuf = NULL; char *poi, *epoi; - if ((mode == U_EDIT) && (state.current - core.editor.mem >= filesize)) { + if ((mode == U_EDIT) && (buf->state.current - buf->mem >= filesize)) { beep(); return; } @@ -766,15 +764,15 @@ void do_ins_chg(PTR start, char* arg, int mode) } else if (!strncmp("hexadecimal", arg, len) && CMDLNG(11, 1)) { base = 16; } else { - ui__ErrorMsg("No such option"); + bvim_error(core, buf, "No such option"); return; } addch('\n'); - if (getcmdstr(cmdstr, 0) == 1) { - ui__Screen_Repaint(); + if (getcmdstr(core, cmdstr, 0) == 1) { + ui__Screen_Repaint(core, buf); return; } - if (alloc_buf(buffer, &tempbuf) == 0L) + if (alloc_buf(core, buf, buffer, &tempbuf) == 0L) return; while (strcmp(cmdstr, ".")) { poi = cmdstr; @@ -813,8 +811,8 @@ void do_ins_chg(PTR start, char* arg, int mode) while (*poi != '\0') { val = strtol(poi, &epoi, base); if (val > 255 || val < 0 || poi == epoi) { - ui__Screen_Repaint(); - ui__ErrorMsg("Invalid value"); + ui__Screen_Repaint(core, buf); + bvim_error(core, buf, "Invalid value"); free(tempbuf); return; } @@ -823,58 +821,59 @@ void do_ins_chg(PTR start, char* arg, int mode) } } addch('\n'); - if (getcmdstr(cmdstr, 0) == 1) { - ui__Screen_Repaint(); + if (getcmdstr(core, cmdstr, 0) == 1) { + ui__Screen_Repaint(core, buf); free(tempbuf); return; } } if (count == 0) { - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); free(tempbuf); return; } switch (mode) { case U_INSERT: - do_put(start, count, tempbuf); + do_put(core, buf, start, count, tempbuf); break; case U_EDIT: - do_over(start, count, tempbuf); + do_over(core, buf, start, count, tempbuf); break; case U_APPEND: - if ((undo_count = alloc_buf(count, &undo_buf)) == 0L) { - ui__Screen_Repaint(); + if ((undo_count = alloc_buf(core, buf, count, &undo_buf)) == 0L) { + ui__Screen_Repaint(core, buf); free(tempbuf); return; } - do_append(count, tempbuf); + do_append(core, buf, count, tempbuf); memcpy(undo_buf, tempbuf, count); - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); break; } free(tempbuf); return; } -void clear_marks() +/* TODO: move marks buffer into buf_t struct */ +void clear_marks(buf_t *buf) { int n; for (n = 0; n < 26; markbuf[n++] = NULL) ; undo_count = 0; - last_motion = core.editor.mem; + last_motion = buf->mem; } -void do_mark(int mark, PTR addr) +void do_mark(buf_t* buf, int mark, PTR addr) { - if (mark < 'a' || mark > 'z' || state.current >= core.editor.maxpos) + if (mark < 'a' || mark > 'z' || buf->state.current >= buf->maxpos) return; markbuf[mark - 'a'] = addr; } -void movebyte() +void movebyte(core_t* core, buf_t* buf) { - bvim_error(state.mode, "Command disabled@- use ':set memmove' to enable "); + bvim_error(core, buf, "Command disabled@- use ':set memmove' to enable "); } void trunc_cur(core_t *core, buf_t *buf) @@ -882,25 +881,25 @@ void trunc_cur(core_t *core, buf_t *buf) undosize = filesize; undo_count = (off_t) (buf->maxpos - buf->state.current); undo_start = buf->state.current; - filesize = buf->state.pagepos - buf->mem + y * core->params.COLUMNS_DATA + xpos(); + filesize = buf->state.pagepos - buf->mem + y * core->params.COLUMNS_DATA + xpos(core, buf); buf->maxpos = (PTR) (buf->mem + filesize); if (filesize == 0L) { - bvim_error(buf->state.mode, BVI_ERROR_NOBYTES); + bvim_error(core, buf, BVI_ERROR_NOBYTES); } else - cur_back(); + cur_back(core, buf); edits = U_TRUNC; - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } int do_append(core_t *core, buf_t *buf, int count, char* buffer) { if (filesize + count > memsize) { - if (enlarge(count + 100L)) + if (enlarge(core, buf, count + 100L)) return 1; } memcpy(buf->mem + filesize, buffer, count); undo_start = buf->mem + filesize - 1L; - setpage(undo_start + count); + setpage(core, buf, undo_start + count); edits = U_APPEND; undosize = filesize; filesize += count; @@ -917,7 +916,7 @@ void do_tilde(core_t *core, buf_t *buf, off_t count) beep(); return; } - if ((undo_count = alloc_buf(count, &undo_buf)) == 0L) + if ((undo_count = alloc_buf(core, buf, count, &undo_buf)) == 0L) return; memcpy(undo_buf, buf->state.current, undo_count); while (count--) { @@ -926,7 +925,7 @@ void do_tilde(core_t *core, buf_t *buf, off_t count) else if (islower((int)(*(buf->state.current)))) *(buf->state.current) = toupper((int)(*(buf->state.current))); buf->state.current++; - cur_forw(0); + cur_forw(core, buf, 0); } edits = U_TILDE; setcur(); @@ -941,7 +940,7 @@ void do_undo(core_t *core, buf_t *buf) PTR d; if (undo_count == 0L) { - ui__ErrorMsg("Nothing to undo"); + bvim_error(core, buf, "Nothing to undo"); return; } set_cursor = undo_start; @@ -988,36 +987,36 @@ void do_undo(core_t *core, buf_t *buf) edits = U_INSERT; break; } - setpage(set_cursor); + setpage(core, buf, set_cursor); if (edits == U_TRUNC && undosize > filesize) - cur_back(); - ui__Screen_Repaint(); + cur_back(core, buf); + ui__Screen_Repaint(core, buf); } void do_over(core_t *core, buf_t *buf, PTR loc, off_t n, PTR bbuf) { if (n < 1L) { - bvim_error(buf->state.mode, BVI_ERROR_NOBYTES); + bvim_error(core, buf, BVI_ERROR_NOBYTES); return; } if (loc + n > buf->maxpos) { beep(); return; } - if ((undo_count = alloc_buf(n, &undo_buf)) == 0L) + if ((undo_count = alloc_buf(core, buf, n, &undo_buf)) == 0L) return; undo_start = loc; memcpy(undo_buf, loc, n); memcpy(loc, bbuf, n); edits = U_EDIT; - setpage(loc + n - 1); - ui__Screen_Repaint(); + setpage(core, buf, loc + n - 1); + ui__Screen_Repaint(core, buf); } void do_put(core_t *core, buf_t *buf, PTR loc, off_t n, PTR bbuf) { if (n < 1L) { - bvim_error(buf->state.mode, BVI_ERROR_NOBYTES); + bvim_error(core, buf, BVI_ERROR_NOBYTES); return; } if (loc > buf->maxpos) { @@ -1025,10 +1024,10 @@ void do_put(core_t *core, buf_t *buf, PTR loc, off_t n, PTR bbuf) return; } if (filesize + n > memsize) { - if (enlarge(n + 1024)) + if (enlarge(core, buf, n + 1024)) return; } - if ((undo_count = alloc_buf(n, &undo_buf)) == 0L) + if ((undo_count = alloc_buf(core, buf, n, &undo_buf)) == 0L) return; undo_start = loc + 1; edits = U_INSERT; @@ -1036,7 +1035,7 @@ void do_put(core_t *core, buf_t *buf, PTR loc, off_t n, PTR bbuf) buf->maxpos += n; memmove(undo_start + n, undo_start, buf->maxpos - loc); memcpy(undo_start, bbuf, n); - setpage(loc + n); - ui__Screen_Repaint(); + setpage(core, buf, loc + n); + ui__Screen_Repaint(core, buf); } diff --git a/include/bmath.h b/include/bmath.h index 80b381d..2350888 100644 --- a/include/bmath.h +++ b/include/bmath.h @@ -33,9 +33,9 @@ typedef unsigned char bool_t; typedef uint64_t crc_reg_t; #define REGFMT PRIx64 -int math__logic(int mode, char* str); -int math__logic_block(int mode, char* str, int block_id); -double math__entropy(int block_id); +int math__logic(core_t* core, buf_t* buf, int mode, char* str); +int math__logic_block(core_t *core, buf_t* buf, int mode, char* str, int block_id); +double math__entropy(core_t *core, buf_t* buf, int block_id); /* Arithmetics */ long math__eval(int mode, char *expression); diff --git a/include/bscript.h b/include/bscript.h index 44e2258..4f951da 100644 --- a/include/bscript.h +++ b/include/bscript.h @@ -36,15 +36,15 @@ struct lua_io { lua_io_link next; }; -void bvim_lua_init(void); -void bvim_lua_finish(void); -int bvim_run_lua_string(char *string); -int bvim_run_lua_script(char *name); +void bvim_lua_init(core_t*); +void bvim_lua_finish(core_t*); +int bvim_run_lua_string(core_t*, buf_t*, char*); +int bvim_run_lua_script(core_t*, buf_t*, char*); int bvim_repl_read(); int bvim_repl_eval(char* line); int luaF_Add(core_t*, struct luaF_item b); -int luaF_Iterator(int (*(func))(), int result); +int luaF_Iterator(core_t *, int (*(func))(), int result); int luaF_DelByName(core_t*, char *); int luaF_DelByID(core_t*, int); struct luaF_item* luaF_GetByName(core_t*, char*); diff --git a/include/bvim.h b/include/bvim.h index ea913ff..eae23f3 100644 --- a/include/bvim.h +++ b/include/bvim.h @@ -102,28 +102,28 @@ extern off_t block_begin, block_end, block_size; /* ================= Debug utilities ================ */ -void bvim_error(int mode, char* fmt, ...); -void bvim_info(int mode, char* fmt, ...); -void bvim_debug(int mode, char* fmt, ...); +void bvim_error(core_t *core, buf_t *buf, char* fmt, ...); +void bvim_info(core_t *core, buf_t *buf, char* fmt, ...); +void bvim_debug(core_t *core, buf_t *buf, char* fmt, ...); /* ================= Exports ================ */ char *bvim_substr(const char *, size_t, size_t); -off_t alloc_buf(off_t, char **); +off_t alloc_buf(core_t*, buf_t*, off_t, char **); off_t yd_addr(core_t*, buf_t*); off_t range(core_t *, buf_t*, int); void do_dot(void); void do_exit(core_t*); -void do_shell(void); +void do_shell(core_t*, buf_t*); void do_undo(core_t *core, buf_t *buf); void do_tilde(core_t *core, buf_t *buf, off_t); void trunc_cur(core_t *core, buf_t *buf); -void do_back(off_t, PTR); -void do_ins_chg(PTR, char *, int); -void do_mark(int, PTR); +void do_back(core_t*, buf_t*, off_t, PTR); +void do_ins_chg(core_t*, buf_t*, PTR, char*, int); +void do_mark(buf_t*, int, PTR); void badcmd(char *); -void movebyte(void); +void movebyte(core_t*, buf_t*); void do_over(core_t *core, buf_t *buf, PTR, off_t, PTR); void do_put(core_t *, buf_t *, PTR, off_t, PTR); void jmpproc(int); @@ -131,10 +131,9 @@ void printline(PTR, int); int addfile(core_t*, buf_t*, char *); int bregexec(core_t*, buf_t*, PTR, char *); -int chk_comm(buf_t*, int); -int doecmd(buf_t *buf, char *, int); -int do_append(core_t *core, buf_t *buf, int, char *); -int do_delete(off_t, PTR); +int doecmd(core_t*, buf_t*, char *, int); +int do_append(core_t*, buf_t*, int, char *); +int do_delete(core_t*, buf_t*, off_t, PTR); int doset(core_t*, char *); int do_substitution(core_t*, buf_t*, int, char *, PTR, PTR); int hexchar(void); @@ -146,44 +145,43 @@ PTR fsearch(core_t*, buf_t*, PTR, PTR, char *); PTR rsearch(core_t*, buf_t*, PTR, PTR, char *); PTR end_word(core_t*, buf_t*, PTR); PTR calc_addr(core_t*, buf_t*, char **, PTR); -PTR do_ft(int, int); +PTR do_ft(core_t*, buf_t*, int, int); char *patcpy(char *, char *, char); -void setpage(PTR), smsg(char *); +void setpage(core_t*, buf_t*, PTR); void usage(void); void bvim_init(core_t*, char *); -void statpos(void); +void statpos(core_t*, buf_t*); void setcur(void); void showparms(core_t*, int); -void toggle(void); -void scrolldown(int); -void scrollup(int); -void fileinfo(char *); -void clearstr(void); -void clear_marks(void); +void toggle(core_t*, buf_t*); +void scrolldown(core_t*, buf_t*, int); +void scrollup(core_t*, buf_t*, int); +void fileinfo(core_t*, buf_t*, char *); +void clear_marks(buf_t*); -void quit(core_t *, buf_t *); +void quit(core_t*, buf_t *); -void do_z(int); +void do_z(core_t*, buf_t*, int); void stuffin(char *); -off_t edit(int); +off_t edit(core_t*, buf_t*, int); off_t load(core_t*, buf_t*, char *); off_t calc_size(char *); int ascii_comp(char *, char *); int hex_comp(char *, char *); -int cur_forw(int); -int cur_back(void); +int cur_forw(core_t*, buf_t*, int); +int cur_back(core_t*, buf_t*); int lineout(void); -int save(core_t*, buf_t*, char *, PTR, PTR, int); +int save(core_t*, buf_t*, char *, int); int at_least(char *, char *, int); int vgetc(void); -int xpos(void); +int xpos(core_t*, buf_t*); int enlarge(core_t*, buf_t*, off_t); int getcmdstr(core_t*, char *, int); int read_rc(core_t*, char*); -int wait_return(core_t*, int); +int wait_return(core_t*, buf_t *buf, int); int get_cursor_position(); int read_history(core_t*, char*); diff --git a/include/commands.h b/include/commands.h index ae28c43..233db36 100644 --- a/include/commands.h +++ b/include/commands.h @@ -62,4 +62,5 @@ int commands__Cmd_Del(core_t*, char*); void docmdline(core_t*, buf_t *, char *); +int chk_comm(core_t *core, buf_t* buf, int flag); diff --git a/include/data.h b/include/data.h index eafdb25..737be07 100644 --- a/include/data.h +++ b/include/data.h @@ -320,9 +320,9 @@ struct luaF { // TODO: Add buffers handling api struct API_T { - void (*error)(int, char*, ...); - void (*info)(int, char*, ...); - void (*debug)(int, char*, ...); + void (*error)(core_t*, buf_t*, char*, ...); + void (*info)(core_t*, buf_t*, char*, ...); + void (*debug)(core_t*, buf_t*, char*, ...); /* editor functions */ /* block functions */ diff --git a/include/search.h b/include/search.h index b1c71a6..f12b9cc 100644 --- a/include/search.h +++ b/include/search.h @@ -24,5 +24,5 @@ struct found { /* Exported functions */ -struct found fuzzy_search(void* input, long input_size, void *pattern, long pattern_size, int algo, int distance); +struct found fuzzy_search(core_t *core, buf_t *buf, long input_size, void *pattern, long pattern_size, int algo, int distance); diff --git a/include/ui.h b/include/ui.h index 3c17b59..f517d1a 100644 --- a/include/ui.h +++ b/include/ui.h @@ -44,36 +44,40 @@ struct repl_t { int current_x; }; -void ui__Init(void); -void ui__ErrorMsg(char *); -void ui__SystemErrorMsg(char *); -void ui__StatusMsg(char *); -void ui__MsgWin_Show(char *, int width, int height); +void ui__Init(core_t*, buf_t*); +void ui__ErrorMsg(core_t*, buf_t*, char *); +void ui__SystemErrorMsg(core_t*, buf_t *, char *); +void ui__StatusMsg(core_t*, buf_t*, char *); +void ui__MsgWin_Show(core_t*, buf_t*, char *, int width, int height); -int ui__ToolWin_Show(int), ui__ToolWin_Hide(); -int ui__ToolWin_Print(char *, int); -short ui__ToolWin_Exist(); +int ui__ToolWin_Show(core_t*, buf_t*, int); +int ui__ToolWin_Hide(core_t*, buf_t*); +int ui__ToolWin_Print(core_t*, buf_t*, char *, int); +short ui__ToolWin_Exist(core_t*, buf_t*); -int ui__REPL_Main(); -int ui__REPLWin_Show(); -int ui__REPLWin_Hide(); -int ui__REPLWin_print(const char*); -int ui__REPLWin_clear(); +int ui__REPL_Main(core_t*, buf_t*); +int ui__REPLWin_Show(core_t*, buf_t*); +int ui__REPLWin_Hide(core_t*, buf_t*); +int ui__REPLWin_print(core_t*, buf_t*, char*); +int ui__REPLWin_clear(core_t*, buf_t*); short ui__REPLWin_Exist(); -void ui__MainWin_Resize(int); +void ui__MainWin_Resize(core_t*, int); void printcolorline(int, int, int, char *); -void ui__Line_Print(); -int ui__lineout(); +void ui__Line_Print(core_t*, buf_t*, PTR, int); +int ui__lineout(core_t*, buf_t*); +void ui__clearstr(core_t*, buf_t*); +void ui__smsg(core_t*, buf_t*, char *s); void ui__setcur(void); -int ui__Color_Set(char *); +int ui__Color_Set(core_t *core, buf_t *buf, char *); void ui__Colors_Set(); void ui__Colors_Load(); void ui__Colors_Save(); -void ui__Screen_Repaint(void), ui__Screen_New(void); +void ui__Screen_Repaint(core_t*, buf_t*); +void ui__Screen_New(core_t*, buf_t*); int ui__BlockHighlightAdd(struct block_item *blk); diff --git a/io.c b/io.c index 73a8e14..7217e7f 100644 --- a/io.c +++ b/io.c @@ -47,7 +47,7 @@ static off_t block_read; char *terminal; /* Save the patched file */ -int save(core_t* core, buf_t* buf, char* fname, char* start, char* end, int flags) +int save(core_t* core, buf_t* buf, char* fname, int flags) { int fd; char string[255]; @@ -55,7 +55,7 @@ int save(core_t* core, buf_t* buf, char* fname, char* start, char* end, int flag off_t filesize; if (!fname) { - ui__ErrorMsg("No file|No current filename"); + bvim_error(core, buf, "No file|No current filename"); return 0; } if (stat(fname, &sbuf) == -1) { @@ -63,11 +63,11 @@ int save(core_t* core, buf_t* buf, char* fname, char* start, char* end, int flag } else { if (S_ISDIR(sbuf.st_mode)) { sprintf(string, "\"%s\" Is a directory", fname); - ui__StatusMsg(string); + bvim_info(core, buf, string); return 0; } else if (S_ISCHR(sbuf.st_mode)) { sprintf(string, "\"%s\" Character special file", fname); - ui__StatusMsg(string); + bvim_info(core, buf, string); return 0; } else if (S_ISBLK(sbuf.st_mode)) { /* @@ -82,7 +82,7 @@ int save(core_t* core, buf_t* buf, char* fname, char* start, char* end, int flag if (filemode == PARTIAL) flags = O_RDWR; if ((fd = open(fname, flags, 0666)) < 0) { - ui__SystemErrorMsg(fname); + bvim_error(core, buf, fname); return 0; } if (filemode == PARTIAL) { @@ -92,26 +92,26 @@ int save(core_t* core, buf_t* buf, char* fname, char* start, char* end, int flag (unsigned long)block_begin, (unsigned long)(block_begin - 1 + filesize)); if (lseek(fd, block_begin, SEEK_SET) < 0) { - ui__SystemErrorMsg(fname); + bvim_error(core, buf, fname); return 0; } } else { - ui__StatusMsg("Null range"); + bvim_info(core, buf, "Null range"); return 0; } } else { - filesize = end - start + 1L; + filesize = buf->maxpos - buf->mem + 1L; sprintf(string, "\"%s\" %s%lu bytes", fname, newstr, (unsigned long)filesize); } - if (write(fd, start, filesize) != filesize) { - ui__SystemErrorMsg(fname); + if (write(fd, buf->mem, filesize) != filesize) { + bvim_error(core, buf, fname); close(fd); return 0; } close(fd); edits = 0; - ui__StatusMsg(string); + bvim_info(core, buf, string); return 1; } @@ -124,7 +124,7 @@ off_t load(core_t* core, buf_t* buf, char* fname) sbuf.st_size = 0L; if (fname != NULL) { sprintf(string, "\"%s\"", fname); - ui__StatusMsg(string); + bvim_info(core, buf, string); refresh(); if (stat(fname, &sbuf) == -1) { filemode = NEW; @@ -144,7 +144,7 @@ off_t load(core_t* core, buf_t* buf, char* fname) P(P_RO) = TRUE; params[P_RO].flags |= P_CHANGED; } else { - ui__SystemErrorMsg(fname); + bvim_error(core, buf, fname); filemode = ERROR; } } else if (S_ISREG(sbuf.st_mode)) { @@ -162,7 +162,7 @@ off_t load(core_t* core, buf_t* buf, char* fname) params[P_RO].flags |= P_CHANGED; } } else { - ui__SystemErrorMsg(fname); + bvim_error(core, buf, fname); filemode = ERROR; } } @@ -183,12 +183,12 @@ off_t load(core_t* core, buf_t* buf, char* fname) printf("Out of memory\n"); exit(0); } - clear_marks(); + clear_marks(buf); if (block_flag && ((filemode == REGULAR) || (filemode == BLOCK_SPECIAL))) { if (lseek(fd, block_begin, SEEK_SET) < 0) { - ui__SystemErrorMsg(fname); + bvim_error(core, buf, fname); filemode = ERROR; } else { if ((filesize = read(fd, buf->mem, block_size)) == 0) { @@ -204,13 +204,13 @@ off_t load(core_t* core, buf_t* buf, char* fname) P(P_OF) = block_begin; params[P_OF].flags |= P_CHANGED; } - ui__StatusMsg(string); + bvim_info(core, buf, string); refresh(); } } else if (filemode == REGULAR) { filesize = sbuf.st_size; if (read(fd, buf->mem, filesize) != filesize) { - ui__SystemErrorMsg(fname); + bvim_error(core, buf, fname); filemode = ERROR; } } else { @@ -239,14 +239,14 @@ off_t load(core_t* core, buf_t* buf, char* fname) break; } if (filemode != ERROR) - ui__StatusMsg(string); + bvim_info(core, buf, string); } buf->state.pagepos = buf->mem; buf->maxpos = buf->mem + filesize; buf->state.loc = HEX; x = core->params.COLUMNS_ADDRESS; y = 0; - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); return (filesize); } @@ -263,7 +263,7 @@ void bvim_init(core_t* core, char* dir) if (shell == NULL || *shell == '\0') shell = "/bin/sh"; - if ((initstr = getenv("BVIINIT")) != NULL) { + if ((initstr = getenv("BVIMINIT")) != NULL) { docmdline(core, core->curbuf, initstr); return; } @@ -305,7 +305,7 @@ int enlarge(core_t* core, buf_t* buf, off_t add) newmem = realloc(buf->mem, memsize + add); } if (newmem == NULL) { - ui__ErrorMsg("Out of memory"); + bvim_error(core, buf, "Out of memory"); return 1; } @@ -319,16 +319,16 @@ int enlarge(core_t* core, buf_t* buf, off_t add) return 0; } -void do_shell() +void do_shell(core_t *core, buf_t *buf) { addch('\n'); savetty(); - shresult = system(shell); - ui__StatusMsg("shell executed successfully!"); + system(shell); + bvim_info(core, buf, "shell executed successfully!"); resetty(); } -off_t alloc_buf(off_t n, char** buffer) +off_t alloc_buf(core_t *core, buf_t *buf, off_t n, char** buffer) { if (*buffer == NULL) { *buffer = (char *)malloc(n); @@ -336,7 +336,7 @@ off_t alloc_buf(off_t n, char** buffer) *buffer = (char *)realloc(*buffer, n); } if (*buffer == NULL) { - ui__ErrorMsg("No buffer space available"); + bvim_error(core, buf, "No buffer space available"); return 0L; } return n; @@ -348,24 +348,24 @@ int addfile(core_t* core, buf_t* buf, char* fname) off_t oldsize; if (stat(fname, &sbuf)) { - ui__SystemErrorMsg(fname); + bvim_error(core, buf, fname); return 1; } if ((fd = open(fname, O_RDONLY)) == -1) { - ui__SystemErrorMsg(fname); + bvim_error(core, buf, fname); return 1; } oldsize = filesize; - if (enlarge(sbuf.st_size)) + if (enlarge(core, buf, sbuf.st_size)) return 1; if (read(fd, buf->mem + filesize, sbuf.st_size) == -1) { - ui__SystemErrorMsg(fname); + bvim_error(core, buf, fname); return 1; } filesize += sbuf.st_size; buf->maxpos = buf->mem + filesize; close(fd); - setpage(buf->mem + oldsize); + setpage(core, buf, buf->mem + oldsize); return 0; } diff --git a/keys.c b/keys.c index 54eb0e0..88b35a5 100644 --- a/keys.c +++ b/keys.c @@ -340,8 +340,8 @@ void keys__KeyMaps_Show(core_t *core) strcat(dispbuf, luacmdbuf); i++; } - ui__StatusMsg(dispbuf); - wait_return(core, TRUE); + bvim_info(core, core->curbuf, dispbuf); + wait_return(core, core->curbuf, TRUE); } int keys__Key_Pressed(core_t *core, int key_code) @@ -358,7 +358,7 @@ int keys__Key_Pressed(core_t *core, int key_code) if ((core->keymap.arr[j].handler_type == BVI_HANDLER_LUA) & (core->keymap.arr[j].handler. lua_cmd != NULL)) { - bvim_run_lua_string(core->keymap.arr[j].handler. + bvim_run_lua_string(core, core->curbuf, core->keymap.arr[j].handler. lua_cmd); } } diff --git a/math.c b/math.c index 988abc1..fda7f7a 100644 --- a/math.c +++ b/math.c @@ -26,9 +26,9 @@ #include "ui.h" #include "bmath.h" -extern struct MARKERS_ markers[MARK_COUNT]; -extern core_t core; -extern state_t state; +//extern struct MARKERS_ markers[MARK_COUNT]; +//extern core_t core; +//extern state_t state; /* Math expressions */ @@ -71,18 +71,21 @@ long math__eval(int mode, char* expression) { /* Logic functions */ -int math__logic(int mode, char* str) +int math__logic(core_t *core, buf_t* buf, int mode, char* str) { int a, b; int value; size_t n; + char* start_addr = NULL; + char* end_addr = NULL; + char *err_str = "Invalid value@for bit manipulation"; if (mode == LSHIFT || mode == RSHIFT || mode == LROTATE || mode == RROTATE) { value = atoi(str); if (value < 1 || value > 8) { - ui__ErrorMsg(err_str); + bvim_error(core, buf, err_str); return 1; } } else { @@ -108,12 +111,13 @@ int math__logic(int mode, char* str) value = atoi(str); } if (value < 0 || value > 255) { - ui__ErrorMsg(err_str); + bvim_error(core, buf, err_str); return 1; } } - if ((undo_count = - alloc_buf((off_t) (end_addr - start_addr + 1), &undo_buf))) { + start_addr = buf->mem; + end_addr = buf->maxpos; + if ((undo_count = alloc_buf(core, buf, (off_t) (end_addr - start_addr + 1), &undo_buf))) { memcpy(undo_buf, start_addr, undo_count); } undo_start = start_addr; @@ -160,21 +164,24 @@ int math__logic(int mode, char* str) } *start_addr++ = (char)(a & 0xff); } - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); return (0); } -int math__logic_block(int mode, char* str, int block_id) +int math__logic_block(core_t *core, buf_t* buf, int mode, char* str, int block_id) { int a, b; int value; size_t n; + char* start_addr = NULL; + char* end_addr = NULL; + char *err_str = "Invalid value@for bit manipulation"; struct block_item *tmp_blk; - tmp_blk = blocks__GetByID(block_id); + tmp_blk = blocks__GetByID(buf, block_id); if ((tmp_blk == NULL) | ((tmp_blk != NULL) & (tmp_blk->pos_start < tmp_blk->pos_end))) { - bvim_error(state.mode, "Invalid block %d for bit manupulation!", block_id); + bvim_error(core, buf, "Invalid block %d for bit manupulation!", block_id); return 1; } @@ -182,7 +189,7 @@ int math__logic_block(int mode, char* str, int block_id) || mode == RROTATE) { value = atoi(str); if (value < 1 || value > 8) { - ui__ErrorMsg(err_str); + bvim_error(core, buf, err_str); return 1; } } else { @@ -208,23 +215,20 @@ int math__logic_block(int mode, char* str, int block_id) value = atoi(str); } if (value < 0 || value > 255) { - ui__ErrorMsg(err_str); + bvim_error(core, buf, err_str); return 1; } } - if ((undo_count = - alloc_buf((off_t) (tmp_blk->pos_end - - tmp_blk->pos_start + 1), - &undo_buf))) { - memcpy(undo_buf, - start_addr + tmp_blk->pos_start, - undo_count); + start_addr = buf->mem; + end_addr = buf->maxpos; + + if ((undo_count = alloc_buf(core, buf, (off_t) (tmp_blk->pos_end - tmp_blk->pos_start + 1), &undo_buf))) { + memcpy(undo_buf, start_addr + tmp_blk->pos_start, undo_count); } undo_start = start_addr + tmp_blk->pos_start; edits = U_EDIT; start_addr = start_addr + tmp_blk->pos_start; - end_addr = - start_addr + tmp_blk->pos_end - tmp_blk->pos_start; + end_addr = start_addr + tmp_blk->pos_end - tmp_blk->pos_start; while (start_addr <= end_addr) { a = *start_addr; a &= 0xff; @@ -267,11 +271,11 @@ int math__logic_block(int mode, char* str, int block_id) } *start_addr++ = (char)(a & 0xff); } - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); return (0); } -double math__entropy(int block_id) +double math__entropy(core_t *core, buf_t* buf, int block_id) { long i = 0; int j = 0; @@ -285,14 +289,14 @@ double math__entropy(int block_id) for (i = 0; i <= 256; i++) freq[i] = 0; - tmp_blk = blocks__GetByID(block_id); + tmp_blk = blocks__GetByID(buf, block_id); if (tmp_blk == NULL) { - bvim_error(state.mode, "Invalid block %d for entropy calculation!", block_id); + bvim_error(core, buf, "Invalid block %d for entropy calculation!", block_id); return -1; } size = tmp_blk->pos_end - tmp_blk->pos_start; while (i < size) { - j = (unsigned int)core.editor.mem[tmp_blk->pos_start + i]; + j = (unsigned int)buf->mem[tmp_blk->pos_start + i]; freq[j]++; i++; } @@ -405,9 +409,7 @@ struct crc_param { * Note: len is given in bits */ -static crc_reg_t crc_calc(const unsigned char *data, - size_t len, - const struct crc_param *param) +static crc_reg_t crc_calc(const unsigned char *data, size_t len, const struct crc_param *param) { struct bit_reg reg; struct bit_reg poly; diff --git a/plugins.c b/plugins.c index e3efe4e..609a6c8 100644 --- a/plugins.c +++ b/plugins.c @@ -63,7 +63,7 @@ int PluginAdd(core_t *core, plugin_t i) return 0; } -int PluginDel(core_t *core, plugin_t i) +int PluginDel(core_t *core, plugin_t *i) { return 0; } @@ -131,7 +131,7 @@ int plugin__Load(core_t *core, char* path) /* Check if file exist and valid */ if (access(path, R_OK) != 0) { - bvim_error(core->curbuf->state.mode, "Can't read/find plugin *.so file"); + bvim_error(core, core->curbuf, "Can't read/find plugin *.so file"); return -1; } module = dlopen(path, RTLD_NOW); @@ -139,7 +139,7 @@ int plugin__Load(core_t *core, char* path) msg = dlerror(); if (msg != NULL) { dlclose(module); - bvim_error(core->curbuf->state.mode, "plugin load error: %s", msg); + bvim_error(core, core->curbuf, "plugin load error: %s", msg); return -1; } } @@ -148,7 +148,7 @@ int plugin__Load(core_t *core, char* path) msg = dlerror(); if (msg != NULL) { dlclose(module); - bvim_error(core->curbuf->state.mode, "plugin load error: can't find plugin_register() function; %s", msg); + bvim_error(core, core->curbuf, "plugin load error: can't find plugin_register() function; %s", msg); return -1; } if (plugin_register != NULL) { @@ -160,11 +160,11 @@ int plugin__Load(core_t *core, char* path) msg = dlerror(); if (msg != NULL) { dlclose(plg.module); - bvim_error(core->curbuf->state.mode, "plugin init error: can't find plugin_init() function: %s", msg); + bvim_error(core, core->curbuf, "plugin init error: can't find plugin_init() function: %s", msg); return -1; } if (plugin_init != NULL) { - plugin_init(core, &state); + plugin_init(core); if (plg.exports.keys != NULL) { i = 0; while (plg.exports.keys[i].id != 0) @@ -198,12 +198,12 @@ int plugin__Load(core_t *core, char* path) } else { dlclose(module); - bvim_error(core->curbuf->state.mode, "plugin init error: wrong plugin_init() function"); + bvim_error(core, core->curbuf, "plugin init error: wrong plugin_init() function"); return -1; } } else { dlclose(module); - bvim_error(core->curbuf->state.mode, "plugin load error: wrong plugin_register() function"); + bvim_error(core, core->curbuf, "plugin load error: wrong plugin_register() function"); return -1; } return 0; @@ -220,7 +220,7 @@ int plugin__Unload(core_t *core, plugin_t *plg) msg = dlerror(); if (msg != NULL) { dlclose(plg->module); - bvim_error(core->curbuf->state.mode, "plugin unload error: can't find plugin_unregister() function"); + bvim_error(core, core->curbuf, "plugin unload error: can't find plugin_unregister() function"); return -1; } /* Unregistering commands, hotkeys, lua functions */ @@ -252,8 +252,8 @@ int plugin__Unload(core_t *core, plugin_t *plg) } } - dlclose(plg.module); - PluginDel(plg); + dlclose(plg->module); + PluginDel(core, plg); return 0; } diff --git a/re.c b/re.c index 7c77bb2..c20d534 100644 --- a/re.c +++ b/re.c @@ -273,10 +273,8 @@ int do_substitution(core_t *core, buf_t *buf, int delim, char* line, PTR startpo repl_pat[pat_len++] = '\\'; break; default: - sprintf(string, - "No such escape sequence \\%c", - *poi); - ui__ErrorMsg(string); + sprintf(string, "No such escape sequence \\%c", *poi); + bvim_error(core, buf, string); return 0; } } else { @@ -300,8 +298,7 @@ int do_substitution(core_t *core, buf_t *buf, int delim, char* line, PTR startpo poi++; } else { if ((n = hexchar()) < 0) { - ui__ErrorMsg - ("Badly formed replacement pattern"); + bvim_error(core, buf, "Badly formed replacement pattern"); return 0; } repl_pat[pat_len] = n; @@ -314,12 +311,11 @@ int do_substitution(core_t *core, buf_t *buf, int delim, char* line, PTR startpo case 'c': break; default: - ui__ErrorMsg("Extra chars|Extra characters at end of command"); + bvim_error(core, buf, "Extra chars|Extra characters at end of command"); return -1; } if (pat_len == -1) { - ui__ErrorMsg - ("No previous substitute re|No previous substitute to repeat"); + bvim_error(core, buf, "No previous substitute re|No previous substitute to repeat"); return -1; } if (delim != '\0') { @@ -330,14 +326,14 @@ int do_substitution(core_t *core, buf_t *buf, int delim, char* line, PTR startpo } if ((strchr("\\#", ch) && buf->state.loc == ASCII) || (strchr("/?", ch) && buf->state.loc == HEX)) { - toggle(); + toggle(core, buf); } startpos--; move(core->screen.maxy, 0); refresh(); if (global) { - if ((undo_count = alloc_buf(endpos - startpos, &undo_buf))) { + if ((undo_count = alloc_buf(core, buf, endpos - startpos, &undo_buf))) { memcpy(undo_buf, startpos + 1, undo_count); } undo_start = startpos + 1; @@ -353,34 +349,29 @@ int do_substitution(core_t *core, buf_t *buf, int delim, char* line, PTR startpo if (!found) { if (!repl_count) { if (P(P_WS)) { - ui__ErrorMsg(BVI_ERROR_PATNOTFOUND); + bvim_error(core, buf, BVI_ERROR_PATNOTFOUND); } else { if (P(P_TE)) - sprintf(string, "No match to %s", - direct == - FORWARD ? "BOTTOM" : "TOP"); + sprintf(string, "No match to %s", direct == FORWARD ? "BOTTOM" : "TOP"); else - sprintf(string, - "Address search hit %s without matching pattern", - direct == - FORWARD ? "BOTTOM" : "TOP"); - ui__ErrorMsg(string); + sprintf(string, "Address search hit %s without matching pattern", direct == FORWARD ? "BOTTOM" : "TOP"); + bvim_error(core, buf, string); } } return repl_count; } else { - setpage(found); + setpage(core, buf, found); if (conf) { - ui__Screen_Repaint(); - ui__StatusMsg("Replace?"); + ui__Screen_Repaint(core, buf); + bvim_info(core, buf, "Replace?"); move(y, x); if (vgetc() != 'y') goto SKIP; } repl_count++; - current_start = buf->state.pagepos + y * core->params.COLUMNS_DATA + xpos(); + current_start = buf->state.pagepos + y * core->params.COLUMNS_DATA + xpos(core, buf); if (!global) { - if ((undo_count = alloc_buf(pat_len, &undo_buf))) { + if ((undo_count = alloc_buf(core, buf, pat_len, &undo_buf))) { memcpy(undo_buf, current_start, undo_count); } undo_start = current_start; @@ -412,7 +403,7 @@ PTR searching(core_t *core, buf_t *buf, int ch, char* line, PTR startpos, PTR en static int direct; if (line[0] == '\0' && again == 0) { - ui__ErrorMsg(BVI_ERROR_NOPREVEXPR); + bvim_error(core, buf, BVI_ERROR_NOPREVEXPR); return 0L; } @@ -421,7 +412,7 @@ PTR searching(core_t *core, buf_t *buf, int ch, char* line, PTR startpos, PTR en start_addr--; if ((strchr("\\#", ch) && buf->state.loc == ASCII) || (strchr("/?", ch) && buf->state.loc == HEX)) { - toggle(); + toggle(core, buf); } if (!strchr("Nn", ch)) { m[0] = ch; @@ -452,7 +443,7 @@ PTR searching(core_t *core, buf_t *buf, int ch, char* line, PTR startpos, PTR en return 0L; } else { cmd = ""; - ui__StatusMsg(m); + bvim_info(core, buf, m); } move(core->screen.maxy, 0); refresh(); @@ -464,7 +455,7 @@ PTR searching(core_t *core, buf_t *buf, int ch, char* line, PTR startpos, PTR en return (found); if (!found) if (flag & 1) { - ui__StatusMsg("Search wrapped BOTTOM|Search wrapped around BOTTOM of buffer"); + bvim_info(core, buf, "Search wrapped BOTTOM|Search wrapped around BOTTOM of buffer"); found = fsearch(core, buf, buf->mem, startpos, search_pat); } } else { @@ -473,34 +464,31 @@ PTR searching(core_t *core, buf_t *buf, int ch, char* line, PTR startpos, PTR en return (found); if (!found) if (flag & 1) { - ui__StatusMsg("Search wrapped TOP|Search wrapped around TOP of buffer"); + bvim_info(core, buf, "Search wrapped TOP|Search wrapped around TOP of buffer"); found = rsearch(core, buf, endpos, startpos, search_pat); } } if (!found) { if (flag & 1) { - ui__ErrorMsg(BVI_ERROR_PATNOTFOUND); + bvim_error(core, buf, BVI_ERROR_PATNOTFOUND); } else { if (P(P_TE)) { - sprintf(string, "No match to %s", - sdir == FORWARD ? "BOTTOM" : "TOP"); + sprintf(string, "No match to %s", sdir == FORWARD ? "BOTTOM" : "TOP"); } else { - sprintf(string, - "Address search hit %s without matching pattern", - sdir == FORWARD ? "BOTTOM" : "TOP"); + sprintf(string, "Address search hit %s without matching pattern", sdir == FORWARD ? "BOTTOM" : "TOP"); } - ui__ErrorMsg(string); + bvim_error(core, buf, string); } } else { - setpage(found); + setpage(core, buf, found); if (cmd) { switch (*cmd) { case 'z': - do_z(*++cmd); + do_z(core, buf, *(++cmd)); break; case 's': do_substitution(core, buf, ch, cmd + 2, found, endpos); - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); break; case ';': searching(core, buf, *(cmd + 1), cmd + 2, found, buf->maxpos - 1, flag); @@ -595,11 +583,11 @@ PTR calc_addr(core_t* core, buf_t* buf, char** pointer, PTR def_addr) cmd++; break; } else if (mark < 'a' || mark > 'z') { - ui__ErrorMsg("Marks are ' and a-z"); + bvim_error(core, buf, "Marks are ' and a-z"); return NULL; } if (markbuf[mark - 'a'] == NULL) { - ui__ErrorMsg("Mark not defined"); + bvim_error(core, buf, "Mark not defined"); return NULL; } addr = markbuf[mark - 'a']; @@ -609,7 +597,7 @@ PTR calc_addr(core_t* core, buf_t* buf, char** pointer, PTR def_addr) case '/': cmd = patcpy(pattern, cmd + 1, ch); if (pattern[0] == '\0' && again == 0) { - ui__ErrorMsg(BVI_ERROR_NOPREVEXPR); + bvim_error(core, buf, BVI_ERROR_NOPREVEXPR); return NULL; } if (pattern[0] != '\0') { @@ -628,7 +616,7 @@ PTR calc_addr(core_t* core, buf_t* buf, char** pointer, PTR def_addr) case '?': cmd = patcpy(pattern, cmd + 1, ch); if (pattern[0] == '\0' && again == 0) { - ui__ErrorMsg(BVI_ERROR_NOPREVEXPR); + bvim_error(core, buf, BVI_ERROR_NOPREVEXPR); return NULL; } if (pattern[0] != '\0') { @@ -671,3 +659,4 @@ PTR calc_addr(core_t* core, buf_t* buf, char** pointer, PTR def_addr) } return addr; } + diff --git a/recomp.c b/recomp.c index 32455c6..0b78709 100644 --- a/recomp.c +++ b/recomp.c @@ -42,6 +42,7 @@ extern int ignore_case; /* * Compiling an ASCII sequence to a regex string */ +// TODO: Add return codes/error codes int ascii_comp(smem, pattern) char *smem; char *pattern; @@ -68,12 +69,12 @@ char *pattern; } if (bracket) { if (!(end = strchr(poi, ']'))) { - ui__ErrorMsg("Missing ]"); + //bvim_error(core, buf, "Missing ]"); return 1; } poi++; if (*poi == ']' || (*poi == '^' && *(poi + 1) == ']')) { - ui__ErrorMsg(BVI_ERROR_EMPTYCLASS); + //bvim_error(core, buf, BVI_ERROR_EMPTYCLASS); return 1; } if (magic) { @@ -189,6 +190,7 @@ char *pattern; /* * Compiling a hex expression to a regex string */ +// TODO: Add return codes/error codes int hex_comp(smem, pattern) char *smem; char *pattern; @@ -206,7 +208,7 @@ char *pattern; poi++; if (*poi == '[') { if (!(end = strchr(poi, ']'))) { - ui__ErrorMsg("Missing ]"); + //bvim_error(core, buf, "Missing ]"); return 1; } poi++; @@ -214,7 +216,7 @@ char *pattern; poi++; if (*poi == ']' || (*poi == '^' && *(poi + 1) == ']')) { - ui__ErrorMsg(BVI_ERROR_EMPTYCLASS); + //bvim_error(core, buf, BVI_ERROR_EMPTYCLASS); return 1; } if (*(end + 1) == '*') @@ -257,7 +259,7 @@ char *pattern; poi++; if (!(end = strchr(poi, '"'))) { /* - ui__ErrorMsg("Missing '\"'"); + bvim_error(core, buf, "Missing '\"'"); return 1; */ end = poi + strlen(poi); @@ -314,7 +316,7 @@ int hexchar() poi++; return nr; } else { - ui__ErrorMsg("Bad hex character@in expression"); + //bvim_error(core, buf, "Bad hex character@in expression"); return -1; } } diff --git a/search.c b/search.c index 970a7ca..1f58076 100644 --- a/search.c +++ b/search.c @@ -1,8 +1,8 @@ #include #include #include -#include "search.h" #include "bvim.h" +#include "search.h" extern core_t core; extern state_t state; @@ -98,18 +98,18 @@ long bitap_fuzzy_bitwise_search_binary(mlist ml, const char *buf, long buf_lengt } // TODO: Add distance propose -struct found fuzzy_search(void* input, long input_size, void *pattern, long pattern_size, int algo, int distance) +struct found fuzzy_search(core_t *core, buf_t *buf, long input_size, void *pattern, long pattern_size, int algo, int distance) { struct found fnd; fnd.cnt = 0; fnd.ml = NULL; if (algo == FUZZY_BITAP_HAMMINGTON_DISTANCE) { - fnd.cnt = bitap_fuzzy_bitwise_search_binary(fnd.ml, input, input_size, pattern, pattern_size, distance); + fnd.cnt = bitap_fuzzy_bitwise_search_binary(fnd.ml, buf->mem, input_size, pattern, pattern_size, distance); } else if (algo == FUZZY_BITAP_LEVENSHTEIN_DISTANCE) { - bvim_error(state.mode, "Levenshtein distance: not yet supported!"); + bvim_error(core, buf, "Levenshtein distance: not yet supported!"); } else { - bvim_error(state.mode, "Wrong fuzzy search algorithm!"); + bvim_error(core, buf, "Wrong fuzzy search algorithm!"); } return fnd; } diff --git a/set.c b/set.c index 6a9107a..72c3763 100644 --- a/set.c +++ b/set.c @@ -75,10 +75,10 @@ int doset(core_t *core, char *arg) /* extract colors section */ if (!strncmp(arg, "color", 5)) { arg = bvim_substr(arg, 6, -1); - ui__Color_Set(arg); + ui__Color_Set(core, core->curbuf, arg); return 0; } else { - ui__ErrorMsg(arg); + bvim_error(core, core->curbuf, arg); return 1; } @@ -103,21 +103,19 @@ int doset(core_t *core, char *arg) else sprintf(buf, " %s=%ld", params[i].fullname, params[i].nvalue); - ui__StatusMsg(buf); + bvim_info(core, core->curbuf, buf); return 0; } if (!strcmp(params[i].fullname, "term")) { - ui__ErrorMsg - ("Can't change type of terminal from within bvim"); + bvim_error(core, core->curbuf, "Can't change type of terminal from within bvim"); return 1; } if (params[i].flags & P_NUM) { if ((i == P_LI) || (i == P_OF)) did_window++; if (arg[strlen(s)] != '=' || state == FALSE) { - sprintf(string, "Option %s is not a toggle", - params[i].fullname); - ui__ErrorMsg(string); + sprintf(string, "Option %s is not a toggle", params[i].fullname); + bvim_error(core, core->curbuf, string); return 1; } else { s = arg + strlen(s) + 1; @@ -129,37 +127,22 @@ int doset(core_t *core, char *arg) params[i].flags |= P_CHANGED; if (i == P_CM) { - if (((COLS - - core->params.COLUMNS_ADDRESS - - 1) / 4) >= P(P_CM)) { - core->params.COLUMNS_DATA = - P(P_CM); + if (((COLS - core->params.COLUMNS_ADDRESS - 1) / 4) >= P(P_CM)) { + core->params.COLUMNS_DATA = P(P_CM); } else { - core->params.COLUMNS_DATA = - P(P_CM) = - ((COLS - - core->params. - COLUMNS_ADDRESS - 1) / 4); + core->params.COLUMNS_DATA = P(P_CM) = ((COLS - core->params.COLUMNS_ADDRESS - 1) / 4); } - core->screen.maxx = - core->params.COLUMNS_DATA * 4 + - core->params.COLUMNS_ADDRESS + 1; - core->params.COLUMNS_HEX = - core->params.COLUMNS_DATA * 3; - status = - core->params.COLUMNS_HEX + - core->params.COLUMNS_DATA - 17; - screen = - core->params.COLUMNS_DATA * - (core->screen.maxy - 1); + core->screen.maxx = core->params.COLUMNS_DATA * 4 + core->params.COLUMNS_ADDRESS + 1; + core->params.COLUMNS_HEX = core->params.COLUMNS_DATA * 3; + status = core->params.COLUMNS_HEX + core->params.COLUMNS_DATA - 17; + screen = core->params.COLUMNS_DATA * (core->screen.maxy - 1); did_window++; stuffin("H"); /* set cursor at HOME */ } } } else { /* boolean */ if (arg[strlen(s)] == '=') { - ui__ErrorMsg - ("Invalid set of boolean parameter"); + bvim_error(core, core->curbuf, "Invalid set of boolean parameter"); return 1; } else { params[i].nvalue = state; @@ -167,14 +150,13 @@ int doset(core_t *core, char *arg) } } } else { - ui__ErrorMsg - ("No such option@- `set all' gives all option values"); + bvim_error(core, core->curbuf, "No such option@- `set all' gives all option values"); return 1; } if (did_window) { core->screen.maxy = P(P_LI) - 1; - ui__Screen_New(); + ui__Screen_New(core, core->curbuf); } return 0; @@ -187,27 +169,26 @@ void showparms(core_t *core, int all) int n; n = 2; - ui__StatusMsg("Parameters:\n"); + bvim_info(core, core->curbuf, "Parameters:\n"); for (p = ¶ms[0]; p->fullname[0] != '\0'; p++) { if (!all && ((p->flags & P_CHANGED) == 0)) continue; if (p->flags & P_BOOL) - sprintf(buf, " %s%s\n", - (p->nvalue ? " " : "no"), p->fullname); + sprintf(buf, " %s%s\n", (p->nvalue ? " " : "no"), p->fullname); else if (p->flags & P_TEXT) sprintf(buf, " %s=%s\n", p->fullname, p->svalue); else sprintf(buf, " %s=%ld\n", p->fullname, p->nvalue); - ui__StatusMsg(buf); + bvim_info(core, core->curbuf, buf); n++; if (n == params[P_LI].nvalue) { - if (wait_return(core, FALSE)) + if (wait_return(core, core->curbuf, FALSE)) return; n = 1; } } - wait_return(core, TRUE); + wait_return(core, core->curbuf, TRUE); } int getcmdstr(core_t *core, char* p, int x) @@ -247,7 +228,7 @@ int getcmdstr(core_t *core, char* p, int x) move(core->screen.maxy, n); } else { *buff = '\0'; - ui__StatusMsg(""); + bvim_info(core, core->curbuf, ""); attroff(COLOR_PAIR(C_CM + 1)); signal(SIGINT, SIG_IGN); return 1; @@ -255,7 +236,7 @@ int getcmdstr(core_t *core, char* p, int x) break; case ESC: /* abandon command */ *buff = '\0'; - ui__StatusMsg(""); + bvim_info(core, core->curbuf, ""); attroff(COLOR_PAIR(C_CM + 1)); signal(SIGINT, SIG_IGN); return 1; diff --git a/ui.c b/ui.c index 712c799..12524f2 100644 --- a/ui.c +++ b/ui.c @@ -27,18 +27,6 @@ #include "keys.h" #include "bscript.h" -// TODO: Implement UI object/structure and its slots, move all current implementations -// TODO: in console.c -/* struct ui_t { - * int *(init)(); - * int *(destroy)(); - * } - * - */ - -extern core_t core; -extern state_t state; - extern struct MARKERS_ markers[MARK_COUNT]; char tmpbuf[10]; @@ -49,12 +37,12 @@ char linbuf[256]; * ---------------------------------------------------------------------- */ -int outmsg(char *s) +int outmsg(core_t *core, char *s) { char *poi; int cnt = 0; - move(core.screen.maxy, 0); + move(core->screen.maxy, 0); poi = strchr(s, '|'); if (P(P_TE)) { @@ -166,7 +154,7 @@ void ui__Colors_Set() } } -int ui__Color_Set(char *arg) +int ui__Color_Set(core_t *core, buf_t *buf, char *arg) { int i; char *s; @@ -179,7 +167,7 @@ int ui__Color_Set(char *arg) break; } if (i == 0) { - ui__ErrorMsg("Wrong color name!"); + ui__ErrorMsg(core, buf, "Wrong color name!"); return -1; } else { colors[i].r = atoi(bvim_substr(arg, strlen(s) + 1, 3)); @@ -187,34 +175,34 @@ int ui__Color_Set(char *arg) colors[i].b = atoi(bvim_substr(arg, strlen(s) + 9, 3)); /*set_palette(); */ ui__Colors_Set(); - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } return 0; } -void ui__Init() +void ui__Init(core_t *core, buf_t *buf) { // Initialization of curses initscr(); if (has_colors() != FALSE) { start_color(); ColorsSave(); - ui__Colors_Set(); + ui__Colors_Set(core, buf); } attrset(A_NORMAL); - ui__MainWin_Resize(LINES); + ui__MainWin_Resize(core, LINES); } -void ui__MainWin_Resize(int lines_count) +void ui__MainWin_Resize(core_t *core, int lines_count) { - core.screen.maxy = lines_count; + core->screen.maxy = lines_count; if (params[P_LI].flags & P_CHANGED) - core.screen.maxy = P(P_LI); - state.scrolly = core.screen.maxy / 2; - P(P_SS) = state.scrolly; - P(P_LI) = core.screen.maxy; - core.screen.maxy--; + core->screen.maxy = P(P_LI); + core->curbuf->state.scrolly = core->screen.maxy / 2; + P(P_SS) = core->curbuf->state.scrolly; + P(P_LI) = core->screen.maxy; + core->screen.maxy--; keypad(stdscr, TRUE); scrollok(stdscr, TRUE); @@ -222,19 +210,19 @@ void ui__MainWin_Resize(int lines_count) cbreak(); noecho(); - core.params.COLUMNS_ADDRESS = 10; + core->params.COLUMNS_ADDRESS = 10; strcpy(addr_form, "%08lX%c:"); - core.params.COLUMNS_DATA = - ((COLS - core.params.COLUMNS_ADDRESS - 1) / 16) * 4; - P(P_CM) = core.params.COLUMNS_DATA; - core.screen.maxx = - core.params.COLUMNS_DATA * 4 + core.params.COLUMNS_ADDRESS + 1; - core.params.COLUMNS_HEX = core.params.COLUMNS_DATA * 3; - status = core.params.COLUMNS_HEX + core.params.COLUMNS_DATA - 17; - state.screen = core.params.COLUMNS_DATA * (core.screen.maxy - 1); + core->params.COLUMNS_DATA = + ((COLS - core->params.COLUMNS_ADDRESS - 1) / 16) * 4; + P(P_CM) = core->params.COLUMNS_DATA; + core->screen.maxx = + core->params.COLUMNS_DATA * 4 + core->params.COLUMNS_ADDRESS + 1; + core->params.COLUMNS_HEX = core->params.COLUMNS_DATA * 3; + status = core->params.COLUMNS_HEX + core->params.COLUMNS_DATA - 17; + core->curbuf->state.screen = core->params.COLUMNS_DATA * (core->screen.maxy - 1); - ui__Screen_New(); + ui__Screen_New(core, core->curbuf); } /* ==================== REPL window ======================= */ @@ -256,12 +244,12 @@ short ui__REPLWin_Exist() } /* Show tools window with height */ -int ui__REPLWin_Show() +int ui__REPLWin_Show(core_t *core, buf_t *buf) { if (repl_win == NULL) { refresh(); attron(COLOR_PAIR(C_WN + 1)); - repl_win = newwin(LINES - 1, core.screen.maxx + 1, 0, 0); + repl_win = newwin(LINES - 1, core->screen.maxx + 1, 0, 0); box(repl_win, 0, 0); wrefresh(repl_win); //attroff(COLOR_PAIR(C_WN + 1)); @@ -274,7 +262,7 @@ int ui__REPLWin_Show() } /* Print string in REPL */ -int ui__REPLWin_print(const char *str) +int ui__REPLWin_print(core_t *core, buf_t* buf, char *str) { char *saved = NULL; char *sptr = NULL; @@ -290,13 +278,13 @@ int ui__REPLWin_print(const char *str) } return 0; } else { - ui__ErrorMsg("print_repl_window: repl window not exist!"); + ui__ErrorMsg(core, buf, "print_repl_window: repl window not exist!"); return -1; } } // FIXME: clear repl window -int ui__REPLWin_clear() +int ui__REPLWin_clear(core_t *core, buf_t* buf) { repl.current_y = 1; repl.current_x = 1; @@ -305,7 +293,7 @@ int ui__REPLWin_clear() return 0; } -int ui__REPL_Main() +int ui__REPL_Main(core_t *core, buf_t *buf) { int c; int i = 0; @@ -314,8 +302,8 @@ int ui__REPL_Main() p[0] = '\0'; signal(SIGINT, jmpproc); - state.mode = BVI_MODE_REPL; - ui__REPLWin_Show(); + buf->state.mode = BVI_MODE_REPL; + ui__REPLWin_Show(core, buf); repl.current_y = 1; repl.current_x = 1; mvwaddch(repl_win, repl.current_y, repl.current_x, '>'); @@ -358,8 +346,8 @@ int ui__REPL_Main() wrefresh(repl_win); } while (c != BVI_CTRL('D')); p[0] = '\0'; - ui__REPLWin_Hide(); - state.mode = BVI_MODE_EDIT; + ui__REPLWin_Hide(core, buf); + buf->state.mode = BVI_MODE_EDIT; signal(SIGINT, SIG_IGN); return 0; } @@ -375,7 +363,7 @@ int ui__REPLWin_ScrollDown(int lines) } /* Hides tools window */ -int ui__REPLWin_Hide() +int ui__REPLWin_Hide(core_t *core, buf_t *buf) { if (repl_win != NULL) { //attron(COLOR_PAIR(C_WN + 1)); @@ -384,10 +372,10 @@ int ui__REPLWin_Hide() delwin(repl_win); repl_win = NULL; attroff(COLOR_PAIR(C_WN + 1)); - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); return 0; } else { - ui__ErrorMsg("hide_tools_window: tools window not exist!\n"); + ui__ErrorMsg(core, buf, "hide_tools_window: tools window not exist!\n"); return -1; } } @@ -401,7 +389,7 @@ WINDOW *tools_win = NULL; // TODO: buffer and make it scrollable /* Check if tools window already exist */ -short ui__ToolWin_Exist() +short ui__ToolWin_Exist(core_t *core, buf_t *buf) { if (tools_win == NULL) return 0; @@ -410,29 +398,27 @@ short ui__ToolWin_Exist() } /* Show tools window with height */ -int ui__ToolWin_Show(int lines_count) +int ui__ToolWin_Show(core_t *core, buf_t *buf, int lines_count) { if (tools_win == NULL) { lines_count = LINES - lines_count - 2; - ui__MainWin_Resize(lines_count); + ui__MainWin_Resize(core, lines_count); refresh(); attron(COLOR_PAIR(C_WN + 1)); - tools_win = - newwin(LINES - lines_count + 2, core.screen.maxx + 1, - lines_count, 0); + tools_win = newwin(LINES - lines_count + 2, core->screen.maxx + 1, lines_count, 0); box(tools_win, 0, 0); wrefresh(tools_win); attroff(COLOR_PAIR(C_WN + 1)); return 0; } else { - ui__ToolWin_Hide(); - ui__ToolWin_Show(lines_count); + ui__ToolWin_Hide(core, buf); + ui__ToolWin_Show(core, buf, lines_count); return 0; } } /* Print string in tools window in line */ -int ui__ToolWin_Print(char *str, int line) +int ui__ToolWin_Print(core_t *core, buf_t *buf, char *str, int line) { if (tools_win != NULL) { attron(COLOR_PAIR(C_WN + 1)); @@ -441,7 +427,7 @@ int ui__ToolWin_Print(char *str, int line) attroff(COLOR_PAIR(C_WN + 1)); return 0; } else { - ui__ErrorMsg("print_tools_window: tools window not exist!\n"); + ui__ErrorMsg(core, buf, "print_tools_window: tools window not exist!\n"); return -1; } } @@ -457,20 +443,20 @@ int ui__ToolWin_ScrollDown(int lines) } /* Hides tools window */ -int ui__ToolWin_Hide() +int ui__ToolWin_Hide(core_t *core, buf_t *buf) { if (tools_win != NULL) { - ui__MainWin_Resize(LINES); + ui__MainWin_Resize(core, LINES); attron(COLOR_PAIR(C_WN + 1)); wborder(tools_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '); wrefresh(tools_win); delwin(tools_win); attroff(COLOR_PAIR(C_WN + 1)); - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); tools_win = NULL; return 0; } else { - ui__ErrorMsg("hide_tools_window: tools window not exist!\n"); + ui__ErrorMsg(core, buf, "hide_tools_window: tools window not exist!\n"); return -1; } } @@ -545,7 +531,7 @@ struct hl_item* HighlightGetByID(unsigned int id) { /* =========== Implementation ========= */ -int highlight_block(struct block_item *tmp_blk) { +int highlight_block(core_t *core, buf_t* buf, struct block_item *tmp_blk) { int i = 0; struct hl_item *thl; // TODO: Implement recursive handling for highlights @@ -557,33 +543,33 @@ int highlight_block(struct block_item *tmp_blk) { thl->dat_start = 0; thl->palette = tmp_blk->palette; if (thl->flg == 1) { - thl->hex_end = core.params.COLUMNS_DATA * 3; - thl->dat_end = core.params.COLUMNS_DATA; + thl->hex_end = core->params.COLUMNS_DATA * 3; + thl->dat_end = core->params.COLUMNS_DATA; } else { thl->hex_end = 0; thl->dat_end = 0; } - for (i = 0; i < core.params.COLUMNS_DATA * 3; i += 3) { - if (((long)(tmp_mem - core.editor.mem + (i / 3)) == tmp_blk->pos_start) & (thl->flg != 1)) { + for (i = 0; i < core->params.COLUMNS_DATA * 3; i += 3) { + if (((long)(tmp_mem - buf->mem + (i / 3)) == tmp_blk->pos_start) & (thl->flg != 1)) { thl->hex_start = i; thl->dat_start = i / 3; - thl->hex_end = core.params.COLUMNS_DATA * 3; - thl->dat_end = core.params.COLUMNS_DATA; + thl->hex_end = core->params.COLUMNS_DATA * 3; + thl->dat_end = core->params.COLUMNS_DATA; thl->flg = 1; } else - if (((long)(tmp_mem - core.editor.mem + (i / 3)) < tmp_blk->pos_end) & ((long)(tmp_mem - core.editor.mem + (i / 3)) > tmp_blk->pos_start) & (thl->flg != 1)) { + if (((long)(tmp_mem - buf->mem + (i / 3)) < tmp_blk->pos_end) & ((long)(tmp_mem - buf->mem + (i / 3)) > tmp_blk->pos_start) & (thl->flg != 1)) { thl->hex_start = i; thl->dat_start = i / 3; - thl->hex_end = core.params.COLUMNS_DATA * 3; - thl->dat_end = core.params.COLUMNS_DATA; + thl->hex_end = core->params.COLUMNS_DATA * 3; + thl->dat_end = core->params.COLUMNS_DATA; thl->flg = 1; } else - if (((long)(tmp_mem - core.editor.mem + (i / 3)) == tmp_blk->pos_end) & (thl->flg == 1)) { + if (((long)(tmp_mem - buf->mem + (i / 3)) == tmp_blk->pos_end) & (thl->flg == 1)) { thl->hex_end = i + 2; thl->dat_end = i / 3 + 1; thl->flg = 0; } else - if (((long)(tmp_mem - core.editor.mem + (i / 3)) > tmp_blk->pos_end)) { + if (((long)(tmp_mem - buf->mem + (i / 3)) > tmp_blk->pos_end)) { thl->flg = 0; } } @@ -633,7 +619,7 @@ void printcolorline_dathl(int y, int x, int base_palette, char *string) } } -void ui__Line_Print(PTR mempos, int scpos) +void ui__Line_Print(core_t *core, buf_t *buf, PTR mempos, int scpos) { char hl_msg[256]; unsigned int k = 0; @@ -646,10 +632,10 @@ void ui__Line_Print(PTR mempos, int scpos) hl_msg[0] = '\0'; *linbuf = '\0'; - if (mempos > core.editor.maxpos) { + if (mempos > buf->maxpos) { strcpy(linbuf, "~ "); } else { - address = (long)(mempos - core.editor.mem + P(P_OF)); + address = (long)(mempos - buf->mem + P(P_OF)); while (markers[k].address != 0) { if (markers[k].address == address) { marker = markers[k].marker; @@ -667,11 +653,11 @@ void ui__Line_Print(PTR mempos, int scpos) *linbuf = '\0'; tmp_mem = mempos; - blocks__Iterator(highlight_block, 1); + blocks__Iterator(buf, highlight_block, 1); mempos = tmp_mem; - for (print_pos = 0; print_pos < core.params.COLUMNS_DATA; print_pos++) { - if (mempos + print_pos >= core.editor.maxpos) { + for (print_pos = 0; print_pos < core->params.COLUMNS_DATA; print_pos++) { + if (mempos + print_pos >= buf->maxpos) { sprintf(tmpbuf, " "); Zeichen = ' '; } else { @@ -684,7 +670,7 @@ void ui__Line_Print(PTR mempos, int scpos) else *(string + print_pos) = '.'; } - *(string + core.params.COLUMNS_DATA) = '\0'; + *(string + core->params.COLUMNS_DATA) = '\0'; /* load color from C(C_HX) */ strcat(linbuf, "|"); @@ -701,12 +687,12 @@ void ui__Line_Print(PTR mempos, int scpos) /* displays a line on screen * at state.pagepos + line y */ -int ui__lineout() +int ui__lineout(core_t *core, buf_t *buf) { off_t Address; - Address = state.pagepos - core.editor.mem + y * core.params.COLUMNS_DATA; - ui__Line_Print(core.editor.mem + Address, y); + Address = buf->state.pagepos - buf->mem + y * core->params.COLUMNS_DATA; + ui__Line_Print(core, buf, buf->mem + Address, y); move(y, x); /*if (k != 0) y = k; @@ -714,16 +700,16 @@ int ui__lineout() return (0); } -void ui__Screen_New() +void ui__Screen_New(core_t *core, buf_t *buf) { - state.screen = core.params.COLUMNS_DATA * (core.screen.maxy - 1); + buf->state.screen = core->params.COLUMNS_DATA * (core->screen.maxy - 1); clear(); - ui__Screen_Repaint(); + ui__Screen_Repaint(core, buf); } -int ui__line(int line, int address) +int ui__line(core_t *core, buf_t *buf, int line, int address) { - ui__Line_Print(core.editor.mem + address, line); + ui__Line_Print(core, buf, buf->mem + address, line); move(line, x); return (0); } @@ -745,7 +731,7 @@ int fold_block(struct block_item tmp_blk) { } // Redraw screen -void ui__Screen_Repaint() +void ui__Screen_Repaint(core_t *core, buf_t *buf) { int save_y; int fold_start = 0; @@ -753,9 +739,9 @@ void ui__Screen_Repaint() int i = 0; save_y = y; - for (y = 0; y < core.screen.maxy; y++) { - buf_address = state.pagepos - core.editor.mem + y * core.params.COLUMNS_DATA; - ui__line(y, buf_address); + for (y = 0; y < core->screen.maxy; y++) { + buf_address = buf->state.pagepos - buf->mem + y * core->params.COLUMNS_DATA; + ui__line(core, buf, y, buf_address); } y = save_y; if (repl_win != NULL) @@ -764,37 +750,37 @@ void ui__Screen_Repaint() wrefresh(tools_win); } -void clearstr() +void ui__clearstr(core_t *core, buf_t *buf) { int n; - move(core.screen.maxy, 0); - for (n = 0; n < core.screen.maxx; n++) + move(core->screen.maxy, 0); + for (n = 0; n < core->screen.maxx; n++) addch(' '); - move(core.screen.maxy, 0); + move(core->screen.maxy, 0); } // Displays an error message -void ui__ErrorMsg(char* s) +void ui__ErrorMsg(core_t *core, buf_t* buf, char *s) { int cnt; if (P(P_EB)) beep(); - clearstr(); + ui__clearstr(core, buf); attron(COLOR_PAIR(C_ER + 1)); /* attrset(A_REVERSE); */ - cnt = outmsg(s); + cnt = outmsg(core, s); /* attrset(A_NORMAL); */ attroff(COLOR_PAIR(C_ER + 1)); - if (cnt >= (core.screen.maxx - 25)) { /* 25 = status */ + if (cnt >= (core->screen.maxx - 25)) { /* 25 = status */ addch('\n'); - wait_return(TRUE); + wait_return(core, buf, TRUE); } } // System error message -void ui__SystemErrorMsg(char* s) +void ui__SystemErrorMsg(core_t *core, buf_t* buf, char* s) { char string[256]; @@ -804,20 +790,20 @@ void ui__SystemErrorMsg(char* s) sprintf(string, "%s: %s", s, sys_errlist[errno]); #endif - ui__ErrorMsg(string); + ui__ErrorMsg(core, buf, string); } /*** displays mode if showmode set *****/ -void smsg(char* s) +void ui__smsg(core_t *core, buf_t *buf, char* s) { if (P(P_MO)) { - ui__StatusMsg(s); + ui__StatusMsg(core, buf, s); setcur(); } } // Display simple message window -void ui__MsgWin_Show(char* s, int height, int width) +void ui__MsgWin_Show(core_t *core, buf_t* buf, char* s, int height, int width) { WINDOW *msg_win; int starty = (LINES - height) / 2; /* Calculating for a center placement */ @@ -836,8 +822,8 @@ void ui__MsgWin_Show(char* s, int height, int width) wrefresh(msg_win); delwin(msg_win); attroff(COLOR_PAIR(C_WN + 1)); - if (state.mode != BVI_MODE_REPL) - ui__Screen_Repaint(); + if (buf->state.mode != BVI_MODE_REPL) + ui__Screen_Repaint(core, buf); /* else wrefresh(repl_win); @@ -845,12 +831,12 @@ void ui__MsgWin_Show(char* s, int height, int width) } // Display message in the status line -void ui__StatusMsg(char* msg) +void ui__StatusMsg(core_t *core, buf_t *buf, char* msg) { - clearstr(); - if (outmsg(msg) >= (core.screen.maxx - 25)) { /* 25 = status */ + ui__clearstr(core, buf); + if (outmsg(core, msg) >= (core->screen.maxx - 25)) { /* 25 = status */ addch('\n'); - wait_return(TRUE); + wait_return(core, buf, TRUE); } }