From 95f33accb3c758e7dc769b2d478d34afc5dd2721 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Thu, 19 May 2016 09:29:38 -0400 Subject: [PATCH] command maps can now be reopened and extended --- 4coder_custom.h | 2 +- 4coder_helper.h | 13 +++++- 4ed.cpp | 83 ++++++++++++++++++++++++++++++++---- 4ed_command.cpp | 19 ++++++--- 4ed_gui.cpp | 5 --- power/4coder_experiments.cpp | 13 +++++- 6 files changed, 113 insertions(+), 22 deletions(-) diff --git a/4coder_custom.h b/4coder_custom.h index 67292040..6a3293cc 100644 --- a/4coder_custom.h +++ b/4coder_custom.h @@ -604,7 +604,7 @@ struct Binding_Unit{ union{ struct{ int total_size; int user_map_count; int error; } header; - struct{ int mapid; int bind_count; } map_begin; + struct{ int mapid; int replace; int bind_count; } map_begin; struct{ int mapid; } map_inherit; struct{ short code; diff --git a/4coder_helper.h b/4coder_helper.h index 5940ad4b..ce090629 100644 --- a/4coder_helper.h +++ b/4coder_helper.h @@ -75,17 +75,28 @@ begin_bind_helper(void *data, int size){ } inline void -begin_map(Bind_Helper *helper, int mapid){ +begin_map_(Bind_Helper *helper, int mapid, int replace){ if (helper->group != 0 && helper->error == 0) helper->error = BH_ERR_MISSING_END; if (!helper->error && mapid < mapid_global) ++helper->header->header.user_map_count; Binding_Unit unit; unit.type = unit_map_begin; unit.map_begin.mapid = mapid; + unit.map_begin.replace = replace; helper->group = write_unit(helper, unit); helper->group->map_begin.bind_count = 0; } +inline void +begin_map(Bind_Helper *helper, int mapid){ + begin_map_(helper, mapid, 0); +} + +inline void +restart_map(Bind_Helper *helper, int mapid){ + begin_map_(helper, mapid, 1); +} + inline void end_map(Bind_Helper *helper){ if (helper->group == 0 && helper->error == 0) helper->error = BH_ERR_MISSING_BEGIN; diff --git a/4ed.cpp b/4ed.cpp index 78aed2d5..c7e67b99 100644 --- a/4ed.cpp +++ b/4ed.cpp @@ -118,17 +118,59 @@ app_get_map_index(Models *models, i32 mapid){ } internal Command_Map* -app_get_map(Models *models, i32 mapid){ +app_get_map_base(Models *models, i32 mapid, b32 add){ Command_Map *map = 0; if (mapid < mapid_global){ - mapid = app_get_map_index(models, mapid); + if (add){ + mapid = app_get_or_add_map_index(models, mapid); + } + else{ + mapid = app_get_map_index(models, mapid); + } if (mapid < models->user_map_count){ map = models->user_maps + mapid; } } else if (mapid == mapid_global) map = &models->map_top; else if (mapid == mapid_file) map = &models->map_file; - return map; + return(map); +} + +internal Command_Map* +app_get_or_add_map(Models *models, i32 mapid){ + Command_Map *map = app_get_map_base(models, mapid, 1); + return(map); +} + +internal Command_Map* +app_get_map(Models *models, i32 mapid){ + Command_Map *map = app_get_map_base(models, mapid, 0); + return(map); +} + +internal void +app_map_set_count(Models *models, i32 mapid, i32 count){ + Command_Map *map = app_get_or_add_map(models, mapid); + Assert(map->commands == 0); + map->count = count; + if (map->max < count){ + map->max = count; + } +} + +internal i32 +app_map_get_count(Models *models, i32 mapid){ + Command_Map *map = app_get_or_add_map(models, mapid); + i32 count = map->count; + Assert(map->commands == 0); + return(count); +} + +internal i32 +app_map_get_max_count(Models *models, i32 mapid){ + Command_Map *map = app_get_or_add_map(models, mapid); + i32 count = map->max; + return(count); } inline void @@ -2614,8 +2656,9 @@ setup_ui_commands(Command_Map *commands, Partition *part, Command_Map *parent){ commands->vanilla_keyboard_default.function = command_null; - // TODO(allen): This is hacky, when the new UI stuff happens, let's fix it, and by that - // I mean actually fix it, don't just say you fixed it with something stupid again. + // TODO(allen): This is hacky, when the new UI stuff happens, let's fix it, + // and by that I mean actually fix it, don't just say you fixed it with + // something stupid again. u8 mdfr; u8 mdfr_array[] = {MDFR_NONE, MDFR_SHIFT, MDFR_CTRL, MDFR_SHIFT | MDFR_CTRL}; for (i32 i = 0; i < 4; ++i){ @@ -3220,7 +3263,7 @@ App_Init_Sig(app_init){ Command_Map *map_ptr = 0; Binding_Unit *unit, *end; i32 user_map_count; - + unit = (Binding_Unit*)models->app_links.memory; if (unit->type == unit_header && unit->header.error == 0){ end = unit + unit->header.total_size; @@ -3235,13 +3278,31 @@ App_Init_Sig(app_init){ &models->mem.part, Command_Map, user_map_count); models->user_map_count = user_map_count; - + for (++unit; unit < end; ++unit){ switch (unit->type){ case unit_map_begin: { - int table_max = unit->map_begin.bind_count * 3 / 2; int mapid = unit->map_begin.mapid; + int count = app_map_get_count(models, mapid); + if (unit->map_begin.replace){ + app_map_set_count(models, mapid, unit->map_begin.bind_count); + } + else{ + app_map_set_count(models, mapid, unit->map_begin.bind_count + count); + } + }; + } + } + + unit = (Binding_Unit*)models->app_links.memory; + for (++unit; unit < end; ++unit){ + switch (unit->type){ + case unit_map_begin: + { + int mapid = unit->map_begin.mapid; + int count = app_map_get_max_count(models, mapid); + int table_max = count * 3 / 2; if (mapid == mapid_global){ map_ptr = &models->map_top; map_init(map_ptr, &models->mem.part, table_max, global); @@ -3259,8 +3320,12 @@ App_Init_Sig(app_init){ map_init(map_ptr, &models->mem.part, table_max, global); } else map_ptr = 0; + + if (map_ptr && unit->map_begin.replace){ + map_clear(map_ptr); + } }break; - + case unit_inherit: if (map_ptr){ Command_Map *parent = 0; diff --git a/4ed_command.cpp b/4ed_command.cpp index a58ee7fe..b7f3a857 100644 --- a/4ed_command.cpp +++ b/4ed_command.cpp @@ -117,16 +117,25 @@ command_binding_zero(){ } internal void -map_init(Command_Map *commands, Partition *part, i32 max, Command_Map *parent){ - max = ((max < 6)?(6):(max)); - commands->parent = parent; - commands->commands = push_array(part, Command_Binding, max); +map_clear(Command_Map *commands){ + i32 max = commands->max; memset(commands->commands, 0, max*sizeof(*commands->commands)); commands->vanilla_keyboard_default = command_binding_zero(); - commands->max = max; commands->count = 0; } +internal void +map_init(Command_Map *commands, Partition *part, i32 max, Command_Map *parent){ + if (commands->commands == 0){ + max = ((max < 6)?(6):(max)); + commands->parent = parent; + commands->commands = push_array(part, Command_Binding, max); + commands->max = max; + + map_clear(commands); + } +} + internal void map_get_vanilla_keyboard_default(Command_Map *map, u8 command, Command_Binding *bind_out){ diff --git a/4ed_gui.cpp b/4ed_gui.cpp index 53e154c6..70b78c7b 100644 --- a/4ed_gui.cpp +++ b/4ed_gui.cpp @@ -1274,11 +1274,6 @@ gui_standard_list(GUI_Target *target, GUI_id id, GUI_Scroll_Vars scroll, } if (update->has_index_position){ - // TODO(allen): THOUGHT: - // Could we better abstract this idea of having something that - // wants to stay in view so that users don't have to manage this - // nasty view back and forth directly if they don't want? - GUI_View_Jump jump = gui_compute_view_jump(scroll, update->index_position); jump.view_min += 45.f; diff --git a/power/4coder_experiments.cpp b/power/4coder_experiments.cpp index 521bf70a..ca05e8dc 100644 --- a/power/4coder_experiments.cpp +++ b/power/4coder_experiments.cpp @@ -305,11 +305,13 @@ CUSTOM_COMMAND_SIG(save_theme_settings){ } #endif +#if 0 void experiment_extension(Bind_Helper *context){ bind(context, 'k', MDFR_ALT, kill_rect); bind(context, '/', MDFR_ALT, mark_matching_brace); bind(context, '\'', MDFR_ALT, cursor_to_surrounding_scope); } +#endif #include @@ -354,7 +356,16 @@ int get_bindings(void *data, int size){ set_scroll_rule(context, smooth_scroll_rule); - default_keys(context, experiment_extension); + default_keys(context, 0); + + begin_map(context, mapid_file); + bind(context, 'k', MDFR_ALT, kill_rect); + end_map(context); + + begin_map(context, my_code_map); + bind(context, '/', MDFR_ALT, mark_matching_brace); + bind(context, '\'', MDFR_ALT, cursor_to_surrounding_scope); + end_map(context); int result = end_bind_helper(context); return(result);