dynamic remapping up and running, first pass at the new maps is done

This commit is contained in:
Allen Webster 2017-11-08 16:28:44 -05:00
parent dca823fe4f
commit bb7a098663
12 changed files with 570 additions and 167 deletions

View File

@ -47,11 +47,11 @@ ENUM(int32_t, Key_Modifier_Index){
Flags can be combined with bit or to specify a state with multiple modifiers.) */
ENUM(uint32_t, Key_Modifier_Flag){
/* DOC(MDFR_NONE specifies that no modifiers are pressed.) */
MDFR_NONE = 0x0,
MDFR_CTRL = 0x1,
MDFR_ALT = 0x2,
MDFR_COMMAND = 0x4,
MDFR_SHIFT = 0x8,
MDFR_NONE = 0x0,
MDFR_CTRL = 0x1,
MDFR_ALT = 0x2,
MDFR_CMND = 0x4,
MDFR_SHIFT = 0x8,
};
/* DOC(A Command_ID is used as a name for commands implemented internally in 4coder.) */

View File

@ -700,6 +700,17 @@ change_mapping(Application_Links *app, String mapping){
print_message(app, literal("Leaving bindings unaltered.\n"));
}
CUSTOM_COMMAND_SIG(remap_interactive){
Query_Bar bar = {0};
char space[1024];
bar.prompt = make_lit_string("Map Name: ");
bar.string = make_fixed_width_string(space);
if (!query_user_string(app, &bar)) return;
change_mapping(app, bar.string);
}
//
// Configuration

View File

@ -26,8 +26,8 @@ CUSTOM_COMMAND_SIG(set_bindings_mac_default);
static Named_Mapping named_maps_values[] = {
{make_lit_string("choose") , set_bindings_choose },
{make_lit_string("default") , set_bindings_default },
{make_lit_string("mac_4coder_like"), set_bindings_mac_4coder_like},
{make_lit_string("mac_default") , set_bindings_mac_default },
{make_lit_string("mac-4coder-like"), set_bindings_mac_4coder_like},
{make_lit_string("mac-default") , set_bindings_mac_default },
};
START_HOOK_SIG(default_start){

View File

@ -480,7 +480,7 @@ CUSTOM_COMMAND_SIG(execute_arbitrary_command){
// upon it so that it has all the cmdid_* commands by default. However, with this
// as an example you have everything you need to make it work already. You could
// even use app->memory to create a hash table in the start hook.
Query_Bar bar;
Query_Bar bar = {0};
char space[1024];
bar.prompt = make_lit_string("Command: ");
bar.string = make_fixed_width_string(space);
@ -513,6 +513,9 @@ CUSTOM_COMMAND_SIG(execute_arbitrary_command){
match_ss(bar.string, make_lit_string("nixify"))){
exec_command(app, eol_nixify);
}
else if (match_ss(bar.string, make_lit_string("remap"))){
exec_command(app, remap_interactive);
}
else{
print_message(app, literal("unrecognized command\n"));
}

View File

@ -47,7 +47,7 @@ begin_bind_helper(void *data, int32_t size){
result.start = result.cursor;
result.end = result.start + size / sizeof(*result.cursor);
Binding_Unit unit;
Binding_Unit unit = {0};
unit.type = unit_header;
unit.header.total_size = sizeof(*result.header);
result.header = write_unit(&result, unit);

View File

@ -65,7 +65,7 @@ struct Tail_Temp_Partition{
inline Partition
make_part(void *memory, i32_4tech size){
Partition partition;
Partition partition = {0};
partition.base = (char*)memory;
partition.pos = 0;
partition.max = size;

View File

@ -191,17 +191,369 @@ default_keys(Bind_Helper *context){
end_map(context);
}
void
bind_ctrl_and_cmnd(Bind_Helper *context, Key_Code code, int32_t command){
bind(context, code, MDFR_CTRL, command);
bind(context, code, MDFR_CMND, command);
}
void
bind_ctrl_and_cmnd(Bind_Helper *context, Key_Code code, Custom_Command_Function *command){
bind(context, code, MDFR_CTRL, command);
bind(context, code, MDFR_CMND, command);
}
void
mac_4coder_like_keys(Bind_Helper *context){
// NOTE(allen|a4.0.22): GLOBAL
begin_map(context, mapid_global);
bind(context, 'j', MDFR_ALT, execute_arbitrary_command);
bind_ctrl_and_cmnd(context, 'p', open_panel_vsplit);
bind_ctrl_and_cmnd(context, '_', open_panel_hsplit);
bind_ctrl_and_cmnd(context, 'P', close_panel);
bind_ctrl_and_cmnd(context, ',', change_active_panel);
bind_ctrl_and_cmnd(context, '<', change_active_panel_backwards);
bind_ctrl_and_cmnd(context, 'n', interactive_new);
bind_ctrl_and_cmnd(context, 'o', interactive_open_or_new);
bind(context, 'o', MDFR_ALT, open_in_other);
bind_ctrl_and_cmnd(context, 'k', interactive_kill_buffer);
bind_ctrl_and_cmnd(context, 'i', interactive_switch_buffer);
bind_ctrl_and_cmnd(context, 'w', save_as);
bind_ctrl_and_cmnd(context, 'h', project_go_to_root_directory);
bind_ctrl_and_cmnd(context, 'S', save_all_dirty_buffers);
bind(context, 'c', MDFR_ALT, open_color_tweaker);
bind(context, 'd', MDFR_ALT, open_debug);
bind(context, '.', MDFR_ALT, change_to_build_panel);
bind(context, ',', MDFR_ALT, close_build_panel);
bind(context, 'n', MDFR_ALT, goto_next_error);
bind(context, 'N', MDFR_ALT, goto_prev_error);
bind(context, 'M', MDFR_ALT, goto_first_error);
bind(context, 'm', MDFR_ALT, build_in_build_panel);
bind(context, 'z', MDFR_ALT, execute_any_cli);
bind(context, 'Z', MDFR_ALT, execute_previous_cli);
bind(context, 'x', MDFR_ALT, execute_arbitrary_command);
bind(context, 's', MDFR_ALT, show_scrollbar);
bind(context, 'w', MDFR_ALT, hide_scrollbar);
bind(context, 'b', MDFR_ALT, toggle_filebar);
bind(context, '@', MDFR_ALT, toggle_mouse);
bind(context, key_page_up, MDFR_CTRL, toggle_fullscreen);
bind(context, 'E', MDFR_ALT, exit_4coder);
bind(context, key_f1, MDFR_NONE, project_fkey_command);
bind(context, key_f2, MDFR_NONE, project_fkey_command);
bind(context, key_f3, MDFR_NONE, project_fkey_command);
bind(context, key_f4, MDFR_NONE, project_fkey_command);
bind(context, key_f5, MDFR_NONE, project_fkey_command);
bind(context, key_f6, MDFR_NONE, project_fkey_command);
bind(context, key_f7, MDFR_NONE, project_fkey_command);
bind(context, key_f8, MDFR_NONE, project_fkey_command);
bind(context, key_f9, MDFR_NONE, project_fkey_command);
bind(context, key_f10, MDFR_NONE, project_fkey_command);
bind(context, key_f11, MDFR_NONE, project_fkey_command);
bind(context, key_f12, MDFR_NONE, project_fkey_command);
bind(context, key_f13, MDFR_NONE, project_fkey_command);
bind(context, key_f14, MDFR_NONE, project_fkey_command);
bind(context, key_f15, MDFR_NONE, project_fkey_command);
bind(context, key_f16, MDFR_NONE, project_fkey_command);
end_map(context);
// NOTE(allen|a4.0.22): FILE
begin_map(context, mapid_file);
bind_vanilla_keys(context, write_character);
bind(context, key_mouse_left, MDFR_NONE, click_set_cursor);
bind(context, key_mouse_right, MDFR_NONE, click_set_mark);
bind(context, key_mouse_left_release, MDFR_NONE, click_set_mark);
bind(context, key_left, MDFR_NONE, move_left);
bind(context, key_right, MDFR_NONE, move_right);
bind(context, key_del, MDFR_NONE, delete_char);
bind(context, key_del, MDFR_SHIFT, delete_char);
bind(context, key_back, MDFR_NONE, backspace_char);
bind(context, key_back, MDFR_SHIFT, backspace_char);
bind(context, key_up, MDFR_NONE, move_up);
bind(context, key_down, MDFR_NONE, move_down);
bind(context, key_end, MDFR_NONE, seek_end_of_line);
bind(context, key_home, MDFR_NONE, seek_beginning_of_line);
bind(context, key_page_up, MDFR_NONE, page_up);
bind(context, key_page_down, MDFR_NONE, page_down);
bind(context, key_right, MDFR_CMND, seek_whitespace_right);
bind(context, key_left, MDFR_CMND, seek_whitespace_left);
bind(context, key_up, MDFR_CMND, seek_whitespace_up_end_line);
bind(context, key_down, MDFR_CMND, seek_whitespace_down_end_line);
bind(context, key_up, MDFR_ALT, move_up_10);
bind(context, key_down, MDFR_ALT, move_down_10);
bind_ctrl_and_cmnd(context, key_back, backspace_word);
bind_ctrl_and_cmnd(context, key_del, delete_word);
bind(context, key_back, MDFR_ALT, snipe_token_or_word);
bind(context, key_del, MDFR_ALT, snipe_token_or_word_right);
bind_ctrl_and_cmnd(context, ' ', set_mark);
bind_ctrl_and_cmnd(context, 'a', replace_in_range);
bind_ctrl_and_cmnd(context, 'c', copy);
bind_ctrl_and_cmnd(context, 'd', delete_range);
bind_ctrl_and_cmnd(context, 'e', center_view);
bind_ctrl_and_cmnd(context, 'E', left_adjust_view);
bind_ctrl_and_cmnd(context, 'f', search);
bind_ctrl_and_cmnd(context, 'F', list_all_locations);
bind(context, 'F', MDFR_ALT, list_all_substring_locations_case_insensitive);
bind_ctrl_and_cmnd(context, 'g', goto_line);
bind_ctrl_and_cmnd(context, 'j', to_lowercase);
bind_ctrl_and_cmnd(context, 'K', kill_buffer);
bind_ctrl_and_cmnd(context, 'l', toggle_line_wrap);
bind_ctrl_and_cmnd(context, 'm', cursor_mark_swap);
bind_ctrl_and_cmnd(context, 'O', reopen);
bind_ctrl_and_cmnd(context, 'q', query_replace);
bind_ctrl_and_cmnd(context, 'Q', query_replace_identifier);
bind_ctrl_and_cmnd(context, 'r', reverse_search);
bind_ctrl_and_cmnd(context, 's', save);
bind_ctrl_and_cmnd(context, 't', search_identifier);
bind_ctrl_and_cmnd(context, 'T', list_all_locations_of_identifier);
bind_ctrl_and_cmnd(context, 'u', to_uppercase);
bind_ctrl_and_cmnd(context, 'v', paste_and_indent);
bind(context, 'v', MDFR_ALT, toggle_virtual_whitespace);
bind_ctrl_and_cmnd(context, 'V', paste_next_and_indent);
bind_ctrl_and_cmnd(context, 'x', cut);
bind_ctrl_and_cmnd(context, 'y', redo);
bind_ctrl_and_cmnd(context, 'z', undo);
bind_ctrl_and_cmnd(context, '2', decrease_line_wrap);
bind_ctrl_and_cmnd(context, '3', increase_line_wrap);
bind_ctrl_and_cmnd(context, '?', toggle_show_whitespace);
bind_ctrl_and_cmnd(context, '~', clean_all_lines);
bind(context, '\n', MDFR_NONE, newline_or_goto_position);
bind(context, '\n', MDFR_SHIFT, newline_or_goto_position_same_panel);
bind(context, ' ', MDFR_SHIFT, write_character);
end_map(context);
// NOTE(allen|a4.0.22): CODE
begin_map(context, default_code_map);
inherit_map(context, mapid_file);
bind_ctrl_and_cmnd(context, key_right, seek_alphanumeric_or_camel_right);
bind_ctrl_and_cmnd(context, key_left, seek_alphanumeric_or_camel_left);
bind(context, '\n', MDFR_NONE, write_and_auto_tab);
bind(context, '\n', MDFR_SHIFT, write_and_auto_tab);
bind(context, '}', MDFR_NONE, write_and_auto_tab);
bind(context, ')', MDFR_NONE, write_and_auto_tab);
bind(context, ']', MDFR_NONE, write_and_auto_tab);
bind(context, ';', MDFR_NONE, write_and_auto_tab);
bind(context, '#', MDFR_NONE, write_and_auto_tab);
bind(context, '\t', MDFR_NONE, word_complete);
bind_ctrl_and_cmnd(context, '\t', auto_tab_range);
bind(context, '\t', MDFR_SHIFT, auto_tab_line_at_cursor);
bind(context, 'h', MDFR_ALT, write_hack);
bind(context, 'r', MDFR_ALT, write_block);
bind(context, 't', MDFR_ALT, write_todo);
bind(context, 'y', MDFR_ALT, write_note);
bind_ctrl_and_cmnd(context, '[', open_long_braces);
bind_ctrl_and_cmnd(context, '{', open_long_braces_semicolon);
bind_ctrl_and_cmnd(context, '}', open_long_braces_break);
bind(context, 'i', MDFR_ALT, if0_off);
bind(context, '1', MDFR_ALT, open_file_in_quotes);
bind(context, '2', MDFR_ALT, open_matching_file_cpp);
bind_ctrl_and_cmnd(context, '0', write_zero_struct);
bind_ctrl_and_cmnd(context, 'I', list_all_functions_current_buffer);
end_map(context);
}
void
mac_default_keys(Bind_Helper *context){
// NOTE(allen|a4.0.22): GLOBAL
begin_map(context, mapid_global);
bind(context, 'x', MDFR_ALT, execute_arbitrary_command);
bind(context, 'p', MDFR_CMND, open_panel_vsplit);
bind(context, '_', MDFR_CMND, open_panel_hsplit);
bind(context, 'P', MDFR_CMND, close_panel);
bind(context, ',', MDFR_CMND, change_active_panel);
bind(context, '<', MDFR_CMND, change_active_panel_backwards);
bind(context, 'n', MDFR_CMND, interactive_new);
bind(context, 'o', MDFR_CMND, interactive_open_or_new);
bind(context, 'o', MDFR_CTRL, open_in_other);
bind(context, 'k', MDFR_CMND, interactive_kill_buffer);
bind(context, 'i', MDFR_CMND, interactive_switch_buffer);
bind(context, 'w', MDFR_CMND, save_as);
bind(context, 'h', MDFR_CMND, project_go_to_root_directory);
bind(context, 'S', MDFR_CMND, save_all_dirty_buffers);
bind(context, 'c', MDFR_CTRL, open_color_tweaker);
bind(context, 'd', MDFR_CTRL, open_debug);
bind(context, '.', MDFR_CTRL, change_to_build_panel);
bind(context, ',', MDFR_CTRL, close_build_panel);
bind(context, 'n', MDFR_CTRL, goto_next_error);
bind(context, 'N', MDFR_CTRL, goto_prev_error);
bind(context, 'M', MDFR_CTRL, goto_first_error);
bind(context, 'm', MDFR_CTRL, build_in_build_panel);
bind(context, 'z', MDFR_CTRL, execute_any_cli);
bind(context, 'Z', MDFR_CTRL, execute_previous_cli);
bind(context, 'x', MDFR_CTRL, execute_arbitrary_command);
bind(context, 's', MDFR_CTRL, show_scrollbar);
bind(context, 'w', MDFR_CTRL, hide_scrollbar);
bind(context, 'b', MDFR_CTRL, toggle_filebar);
bind(context, '@', MDFR_CTRL, toggle_mouse);
bind(context, key_page_up, MDFR_CMND, toggle_fullscreen);
bind(context, 'E', MDFR_CTRL, exit_4coder);
bind(context, key_f1, MDFR_NONE, project_fkey_command);
bind(context, key_f2, MDFR_NONE, project_fkey_command);
bind(context, key_f3, MDFR_NONE, project_fkey_command);
bind(context, key_f4, MDFR_NONE, project_fkey_command);
bind(context, key_f5, MDFR_NONE, project_fkey_command);
bind(context, key_f6, MDFR_NONE, project_fkey_command);
bind(context, key_f7, MDFR_NONE, project_fkey_command);
bind(context, key_f8, MDFR_NONE, project_fkey_command);
bind(context, key_f9, MDFR_NONE, project_fkey_command);
bind(context, key_f10, MDFR_NONE, project_fkey_command);
bind(context, key_f11, MDFR_NONE, project_fkey_command);
bind(context, key_f12, MDFR_NONE, project_fkey_command);
bind(context, key_f13, MDFR_NONE, project_fkey_command);
bind(context, key_f14, MDFR_NONE, project_fkey_command);
bind(context, key_f15, MDFR_NONE, project_fkey_command);
bind(context, key_f16, MDFR_NONE, project_fkey_command);
end_map(context);
// NOTE(allen|a4.0.22): FILE
begin_map(context, mapid_file);
bind_vanilla_keys(context, write_character);
bind(context, key_mouse_left, MDFR_NONE, click_set_cursor);
bind(context, key_mouse_right, MDFR_NONE, click_set_mark);
bind(context, key_mouse_left_release, MDFR_NONE, click_set_mark);
bind(context, key_left, MDFR_NONE, move_left);
bind(context, key_right, MDFR_NONE, move_right);
bind(context, key_del, MDFR_NONE, delete_char);
bind(context, key_del, MDFR_SHIFT, delete_char);
bind(context, key_back, MDFR_NONE, backspace_char);
bind(context, key_back, MDFR_SHIFT, backspace_char);
bind(context, key_up, MDFR_NONE, move_up);
bind(context, key_down, MDFR_NONE, move_down);
bind(context, key_end, MDFR_NONE, seek_end_of_line);
bind(context, key_home, MDFR_NONE, seek_beginning_of_line);
bind(context, key_page_up, MDFR_NONE, page_up);
bind(context, key_page_down, MDFR_NONE, page_down);
bind(context, key_right, MDFR_CMND, seek_whitespace_right);
bind(context, key_left, MDFR_CMND, seek_whitespace_left);
bind(context, key_up, MDFR_CMND, seek_whitespace_up_end_line);
bind(context, key_down, MDFR_CMND, seek_whitespace_down_end_line);
bind(context, key_up, MDFR_CTRL, move_up_10);
bind(context, key_down, MDFR_CTRL, move_down_10);
bind(context, key_back, MDFR_CMND, backspace_word);
bind(context, key_del, MDFR_CMND, delete_word);
bind(context, key_back, MDFR_CTRL, snipe_token_or_word);
bind(context, key_del, MDFR_CTRL, snipe_token_or_word_right);
bind(context, ' ', MDFR_CMND, set_mark);
bind(context, 'a', MDFR_CMND, replace_in_range);
bind(context, 'c', MDFR_CMND, copy);
bind(context, 'd', MDFR_CMND, delete_range);
bind(context, 'e', MDFR_CMND, center_view);
bind(context, 'E', MDFR_CMND, left_adjust_view);
bind(context, 'f', MDFR_CMND, search);
bind(context, 'F', MDFR_CMND, list_all_locations);
bind(context, 'F', MDFR_CTRL, list_all_substring_locations_case_insensitive);
bind(context, 'g', MDFR_CMND, goto_line);
bind(context, 'j', MDFR_CMND, to_lowercase);
bind(context, 'K', MDFR_CMND, kill_buffer);
bind(context, 'l', MDFR_CMND, toggle_line_wrap);
bind(context, 'm', MDFR_CMND, cursor_mark_swap);
bind(context, 'O', MDFR_CMND, reopen);
bind(context, 'q', MDFR_CMND, query_replace);
bind(context, 'Q', MDFR_CMND, query_replace_identifier);
bind(context, 'r', MDFR_CMND, reverse_search);
bind(context, 's', MDFR_CMND, save);
bind(context, 't', MDFR_CMND, search_identifier);
bind(context, 'T', MDFR_CMND, list_all_locations_of_identifier);
bind(context, 'u', MDFR_CMND, to_uppercase);
bind(context, 'v', MDFR_CMND, paste_and_indent);
bind(context, 'v', MDFR_CTRL, toggle_virtual_whitespace);
bind(context, 'V', MDFR_CMND, paste_next_and_indent);
bind(context, 'x', MDFR_CMND, cut);
bind(context, 'y', MDFR_CMND, redo);
bind(context, 'z', MDFR_CMND, undo);
bind(context, '2', MDFR_CMND, decrease_line_wrap);
bind(context, '3', MDFR_CMND, increase_line_wrap);
bind(context, '?', MDFR_CMND, toggle_show_whitespace);
bind(context, '~', MDFR_CMND, clean_all_lines);
bind(context, '\n', MDFR_NONE, newline_or_goto_position);
bind(context, '\n', MDFR_SHIFT, newline_or_goto_position_same_panel);
bind(context, ' ', MDFR_SHIFT, write_character);
end_map(context);
// NOTE(allen|a4.0.22): CODE
begin_map(context, default_code_map);
inherit_map(context, mapid_file);
bind(context, key_right, MDFR_CMND, seek_alphanumeric_or_camel_right);
bind(context, key_left, MDFR_CMND, seek_alphanumeric_or_camel_left);
bind(context, '\n', MDFR_NONE, write_and_auto_tab);
bind(context, '\n', MDFR_SHIFT, write_and_auto_tab);
bind(context, '}', MDFR_NONE, write_and_auto_tab);
bind(context, ')', MDFR_NONE, write_and_auto_tab);
bind(context, ']', MDFR_NONE, write_and_auto_tab);
bind(context, ';', MDFR_NONE, write_and_auto_tab);
bind(context, '#', MDFR_NONE, write_and_auto_tab);
bind(context, '\t', MDFR_NONE, word_complete);
bind(context, '\t', MDFR_CMND, auto_tab_range);
bind(context, '\t', MDFR_SHIFT, auto_tab_line_at_cursor);
bind(context, 'h', MDFR_CTRL, write_hack);
bind(context, 'r', MDFR_CTRL, write_block);
bind(context, 't', MDFR_CTRL, write_todo);
bind(context, 'y', MDFR_CTRL, write_note);
bind(context, '[', MDFR_CMND, open_long_braces);
bind(context, '{', MDFR_CMND, open_long_braces_semicolon);
bind(context, '}', MDFR_CMND, open_long_braces_break);
bind(context, 'i', MDFR_CTRL, if0_off);
bind(context, '1', MDFR_CTRL, open_file_in_quotes);
bind(context, '2', MDFR_CTRL, open_matching_file_cpp);
bind(context, '0', MDFR_CMND, write_zero_struct);
bind(context, 'I', MDFR_CMND, list_all_functions_current_buffer);
end_map(context);
}

260
4ed.cpp
View File

@ -619,10 +619,12 @@ SCROLL_RULE_SIG(fallback_scroll_rule){
return(result);
}
#define DEFAULT_MAP_SIZE 10
#define DEFAULT_UI_MAP_SIZE 32
internal void
setup_ui_commands(Command_Map *commands, Partition *part, i32 parent){
map_init(commands, part, 32, parent);
map_clear(commands);
map_init(commands, part, DEFAULT_UI_MAP_SIZE, parent);
// 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
@ -640,20 +642,23 @@ setup_ui_commands(Command_Map *commands, Partition *part, i32 parent){
internal void
setup_file_commands(Command_Map *commands, Partition *part, i32 parent){
map_init(commands, part, 10, parent);
map_init(commands, part, DEFAULT_MAP_SIZE, parent);
}
internal void
setup_top_commands(Command_Map *commands, Partition *part, i32 parent){
map_init(commands, part, 10, parent);
map_init(commands, part, DEFAULT_MAP_SIZE, parent);
}
internal b32
interpret_binding_buffer(Models *models, void *buffer, i32 size){
b32 result = true;
//General_Memory *gen = &models->mem.general;
General_Memory *gen = &models->mem.general;
Partition *part = &models->mem.part;
Temp_Memory temp = begin_temp_memory(part);
Partition local_part = {0};
Mapping new_mapping = {0};
@ -670,128 +675,188 @@ interpret_binding_buffer(Models *models, void *buffer, i32 size){
Command_Map *map_ptr = 0;
Binding_Unit *unit = (Binding_Unit*)models->app_links.memory;
Binding_Unit *unit = (Binding_Unit*)buffer;
if (unit->type == unit_header && unit->header.error == 0){
Binding_Unit *end = unit + unit->header.total_size;
i32 user_map_count = unit->header.user_map_count;
new_mapping.user_map_count = user_map_count;
// Initialize Table and User Maps in Temp Buffer
new_mapping.map_id_table = push_array(part, i32, user_map_count);
memset(new_mapping.map_id_table, -1, user_map_count*sizeof(i32));
new_mapping.user_maps = push_array(part, Command_Map, user_map_count);
new_mapping.user_map_count = user_map_count;
// Find the Size of Each Map
for (++unit; unit < end; ++unit){
switch (unit->type){
case unit_map_begin:
{
i32 mapid = unit->map_begin.mapid;
i32 count = map_get_count(&new_mapping, mapid);
if (mapid == mapid_ui || mapid == mapid_nomap){
break;
}
else if (mapid == mapid_global){
did_top = true;
}
else if (mapid == mapid_file){
did_file = true;
}
Command_Map *map = get_or_add_map(&new_mapping, mapid);
i32 count = map->count;
i32 new_count = 0;
if (unit->map_begin.replace){
map_set_count(&new_mapping, mapid, unit->map_begin.bind_count);
map->real_beginning = unit;
new_count = unit->map_begin.bind_count;
}
else{
map_set_count(&new_mapping, mapid, unit->map_begin.bind_count + count);
if (map->real_beginning == 0){
map->real_beginning = unit;
}
new_count = unit->map_begin.bind_count + count;
}
map->count = new_count;
if (map->max < new_count){
map->max = new_count;
}
}break;
}
}
unit = (Binding_Unit*)models->app_links.memory;
// Add up the Map Counts
i32 count_global = DEFAULT_MAP_SIZE;
if (did_top){
count_global = clamp_bottom(6, new_mapping.map_top.count*3/2);
}
i32 count_file = DEFAULT_MAP_SIZE;
if (did_file){
count_file = clamp_bottom(6, new_mapping.map_file.count*3/2);
}
i32 count_ui = DEFAULT_UI_MAP_SIZE;
i32 count_user = 0;
for (i32 i = 0; i < user_map_count; ++i){
Command_Map *map = &new_mapping.user_maps[i];
count_user += clamp_bottom(6, map->max*3/2);
}
i32 binding_memsize = (count_global + count_file + count_ui + count_user)*sizeof(Command_Binding);
// Allocate Needed Memory
i32 map_id_table_memsize = user_map_count*sizeof(i32);
i32 user_maps_memsize = user_map_count*sizeof(Command_Map);
i32 map_id_table_rounded_memsize = l_round_up_i32(map_id_table_memsize, 8);
i32 user_maps_rounded_memsize = l_round_up_i32(user_maps_memsize, 8);
i32 binding_rounded_memsize = l_round_up_i32(binding_memsize, 8);
i32 needed_memsize = map_id_table_rounded_memsize + user_maps_rounded_memsize + binding_rounded_memsize;
new_mapping.memory = general_memory_allocate(gen, needed_memsize);
local_part = make_part(new_mapping.memory, needed_memsize);
// Move ID Table Memory and Pointer
i32 *old_table = new_mapping.map_id_table;
new_mapping.map_id_table = push_array(&local_part, i32, user_map_count);
memmove(new_mapping.map_id_table, old_table, map_id_table_memsize);
// Move User Maps Memory and Pointer
Command_Map *old_maps = new_mapping.user_maps;
new_mapping.user_maps = push_array(&local_part, Command_Map, user_map_count);
memmove(new_mapping.user_maps, old_maps, user_maps_memsize);
// Fill in Command Maps
unit = (Binding_Unit*)buffer;
for (++unit; unit < end; ++unit){
switch (unit->type){
case unit_map_begin:
{
i32 mapid = unit->map_begin.mapid;
i32 count = map_get_max_count(&new_mapping, mapid);
i32 table_max = count * 3 / 2;
b32 auto_clear = false;
if (mapid == mapid_global){
map_ptr = &new_mapping.map_top;
auto_clear = map_init(map_ptr, part, table_max, mapid_global);
did_top = true;
}
else if (mapid == mapid_file){
map_ptr = &new_mapping.map_file;
auto_clear = map_init(map_ptr, part, table_max, mapid_global);
did_file = true;
}
else if (mapid < mapid_global){
i32 index = get_or_add_map_index(&new_mapping, mapid);
Assert(index < user_map_count);
map_ptr = new_mapping.user_maps + index;
auto_clear = map_init(map_ptr, part, table_max, mapid_global);
}
else{
if (mapid == mapid_ui || mapid == mapid_nomap){
map_ptr = 0;
break;
}
if (map_ptr && (unit->map_begin.replace || auto_clear)){
map_clear(map_ptr);
Command_Map *map = get_or_add_map(&new_mapping, mapid);
if (unit >= map->real_beginning){
if (mapid == mapid_global){
map_ptr = &new_mapping.map_top;
}
else if (mapid == mapid_file){
map_ptr = &new_mapping.map_file;
}
else if (mapid < mapid_global){
i32 index = get_or_add_map_index(&new_mapping, mapid);
Assert(index < user_map_count);
map_ptr = &new_mapping.user_maps[index];
}
else{
map_ptr = 0;
break;
}
Assert(map_ptr != 0);
// NOTE(allen): Map can begin multiple times, only alloc and clear when we first see it.
if (map_ptr->commands == 0){
i32 count = map->max;
i32 table_max = clamp_bottom(6, count*3/2);
map_init(map_ptr, &local_part, table_max, mapid_global);
}
}
}break;
case unit_inherit:
if (map_ptr){
#if 0
Command_Map *parent = 0;
i32 mapid = unit->map_inherit.mapid;
if (mapid == mapid_global){
parent = &new_mapping.map_top;
{
if (map_ptr != 0){
map_ptr->parent = unit->map_inherit.mapid;
}
else if (mapid == mapid_file){
parent = &new_mapping.map_file;
}
else if (mapid < mapid_global){
i32 index = get_or_add_map_index(&new_mapping, mapid);
if (index < user_map_count){
parent = new_mapping.user_maps + index;
}
else{
parent = 0;
}
}
map_ptr->parent = parent;
#endif
map_ptr->parent = unit->map_inherit.mapid;
}break;
case unit_binding:
if (map_ptr){
Command_Function *func = 0;
if (unit->binding.command_id >= 0 && unit->binding.command_id < cmdid_count)
func = command_table[unit->binding.command_id];
if (func){
if (unit->binding.code == 0){
u32 index = 0;
if (map_get_modifiers_hash(unit->binding.modifiers, &index)){
map_ptr->vanilla_keyboard_default[index].function = func;
map_ptr->vanilla_keyboard_default[index].custom_id = unit->binding.command_id;
{
if (map_ptr != 0){
Command_Function *func = 0;
if (unit->binding.command_id >= 0 && unit->binding.command_id < cmdid_count)
func = command_table[unit->binding.command_id];
if (func){
if (unit->binding.code == 0){
u32 index = 0;
if (map_get_modifiers_hash(unit->binding.modifiers, &index)){
map_ptr->vanilla_keyboard_default[index].function = func;
map_ptr->vanilla_keyboard_default[index].custom_id = unit->binding.command_id;
}
}
else{
map_add(map_ptr, unit->binding.code, unit->binding.modifiers, func, unit->binding.command_id);
}
}
else{
map_add(map_ptr, unit->binding.code, unit->binding.modifiers, func, unit->binding.command_id);
}
}
}break;
case unit_callback:
if (map_ptr){
Command_Function *func = command_user_callback;
Custom_Command_Function *custom = unit->callback.func;
if (func){
if (unit->callback.code == 0){
u32 index = 0;
if (map_get_modifiers_hash(unit->binding.modifiers, &index)){
map_ptr->vanilla_keyboard_default[index].function = func;
map_ptr->vanilla_keyboard_default[index].custom = custom;
{
if (map_ptr != 0){
Command_Function *func = command_user_callback;
Custom_Command_Function *custom = unit->callback.func;
if (func){
if (unit->callback.code == 0){
u32 index = 0;
if (map_get_modifiers_hash(unit->binding.modifiers, &index)){
map_ptr->vanilla_keyboard_default[index].function = func;
map_ptr->vanilla_keyboard_default[index].custom = custom;
}
}
else{
map_add(map_ptr, unit->callback.code, unit->callback.modifiers, func, custom);
}
}
else{
map_add(map_ptr, unit->callback.code, unit->callback.modifiers, func, custom);
}
}
}break;
@ -850,17 +915,28 @@ interpret_binding_buffer(Models *models, void *buffer, i32 size){
}break;
}
}
if (!did_top){
setup_top_commands(&new_mapping.map_top, &local_part, mapid_global);
}
if (!did_file){
setup_file_commands(&new_mapping.map_file, &local_part, mapid_global);
}
setup_ui_commands(&new_mapping.map_ui, &local_part, mapid_global);
}
else{
// TODO(allen): Probably should have some recovery plan here.
InvalidCodePath;
}
if (!did_top){
setup_top_commands(&new_mapping.map_top, part, mapid_global);
Mapping old_mapping = models->mapping;
if (old_mapping.memory != 0){
// TODO(allen): Do I need to explicity work on recovering the old ids of buffers?
general_memory_free(gen, old_mapping.memory);
}
if (!did_file){
setup_file_commands(&new_mapping.map_file, part, mapid_global);
}
setup_ui_commands(&new_mapping.map_ui, part, mapid_global);
models->mapping = new_mapping;
end_temp_memory(temp);
return(result);
}
@ -1967,7 +2043,9 @@ App_Step_Sig(app_step){
b32 hit_esc = 0;
for (i32 key_i = 0; key_i < key_data.count; ++key_i){
if (models->command_coroutine != 0) break;
if (models->command_coroutine != 0){
break;
}
switch (vars->state){
case APP_STATE_EDIT:
@ -1983,10 +2061,10 @@ App_Step_Sig(app_step){
if (cmd_bind.function){
if (key.keycode == key_esc){
hit_esc = 1;
hit_esc = true;
}
else{
hit_something = 1;
hit_something = true;
}
Assert(models->command_coroutine == 0);
@ -2001,7 +2079,7 @@ App_Step_Sig(app_step){
models->prev_command = cmd_bind;
app_result.animating = 1;
app_result.animating = true;
}
}break;

View File

@ -28,10 +28,13 @@ struct Command_Map{
i32 parent;
Command_Binding vanilla_keyboard_default[8];
Command_Binding *commands;
u32 count, max;
i32 count, max;
void *real_beginning;
};
struct Mapping{
void *memory;
Command_Map map_top;
Command_Map map_file;
Command_Map map_ui;
@ -43,10 +46,10 @@ struct Mapping{
internal i32
get_or_add_map_index(Mapping *mapping, i32 mapid){
i32 result = 0;
i32 user_map_count = mapping->user_map_count;
i32 *map_id_table = mapping->map_id_table;
for (result = 0; result < user_map_count; ++result){
i32 result = 0;
for (; result < user_map_count; ++result){
if (map_id_table[result] == mapid){
break;
}
@ -58,12 +61,13 @@ get_or_add_map_index(Mapping *mapping, i32 mapid){
return(result);
}
// HACK(allen): This seems busted, investigate.
internal i32
get_map_index(Mapping *mapping, i32 mapid){
i32 result = 0;
i32 user_map_count = mapping->user_map_count;
i32 *map_id_table = mapping->map_id_table;
for (result = 0; result < user_map_count; ++result){
i32 result = 0;
for (; result < user_map_count; ++result){
if (map_id_table[result] == mapid){
break;
}
@ -201,25 +205,16 @@ map_drop(Command_Map *map, Key_Code event_code, u8 modifiers){
}
internal void
map_clear(Command_Map *commands){
u32 max = commands->max;
memset(commands->commands, 0, max*sizeof(*commands->commands));
memset(commands->vanilla_keyboard_default, 0, sizeof(commands->vanilla_keyboard_default));
commands->count = 0;
}
internal b32
map_init(Command_Map *commands, Partition *part, u32 max, i32 parent){
b32 result = false;
if (commands->commands == 0){
max = clamp_bottom((u32)6, max);
commands->parent = parent;
commands->commands = push_array(part, Command_Binding, max);
commands->count = 0;
commands->max = max;
result = true;
}
return(result);
map_init(Command_Map *map, Partition *part, i32 max, i32 parent){
Assert(max >= 6);
Assert(map->commands == 0);
map->parent = parent;
map->commands = push_array(part, Command_Binding, max);
map->count = 0;
map->max = max;
memset(map->commands, 0, max*sizeof(*map->commands));
memset(map->vanilla_keyboard_default, 0, sizeof(map->vanilla_keyboard_default));
}
internal b32
@ -258,7 +253,7 @@ map_extract(Command_Map *map, Key_Event_Data key){
u8 mod_flags = MDFR_NONE;
if (ctrl) mod_flags |= MDFR_CTRL;
if (command) mod_flags |= MDFR_COMMAND;
if (command) mod_flags |= MDFR_CMND;
if (alt) mod_flags |= MDFR_ALT;
if (shift) mod_flags |= MDFR_SHIFT;
@ -287,11 +282,11 @@ map_extract_recursive(Mapping *mapping, i32 map_id, Key_Event_Data key){
map = &mapping->map_top;
}
Command_Binding cmd_bind = {0};
Command_Map *visited_maps[16] = {0};
i32 visited_top = 0;
while (map){
Command_Binding cmd_bind = {0};
for (; map != 0; ){
cmd_bind = map_extract(map, key);
if (cmd_bind.function == 0){
if (visited_top < ArrayCount(visited_maps)){

View File

@ -9,33 +9,6 @@
// TOP
internal void
map_set_count(Mapping *mapping, i32 mapid, u32 count){
Command_Map *map = get_or_add_map(mapping, mapid);
Assert(map->commands == 0);
map->count = count;
if (map->max < count){
map->max = count;
}
}
internal u32
map_get_count(Mapping *mapping, i32 mapid){
Command_Map *map = get_or_add_map(mapping, mapid);
u32 count = map->count;
Assert(map->commands == 0);
return(count);
}
internal u32
map_get_max_count(Mapping *mapping, i32 mapid){
Command_Map *map = get_or_add_map(mapping, mapid);
u32 count = map->max;
return(count);
}
/////////////////
inline void*
get_view_body(View *view){
char *result = (char*)view;

View File

@ -15,10 +15,6 @@ TYPE: 'build-target'
#define NO_BINDING
#include "4coder_default_bindings.cpp"
#ifndef BIND_4CODER_TESTS
# define BIND_4CODER_TESTS(context) ((void)context)
#endif
#include <string.h>
static float
@ -1390,9 +1386,6 @@ get_bindings(void *data, int32_t size){
// You can also use the helper "restart_map" instead of
// begin_map to clear everything that was in the map and
// bind new things instead.
begin_map(context, mapid_global);
end_map(context);
begin_map(context, mapid_file);
bind(context, 's', MDFR_CTRL, punishment);
bind(context, 's', MDFR_ALT, save);
@ -1421,8 +1414,6 @@ get_bindings(void *data, int32_t size){
bind(context, 'p', MDFR_ALT, rename_parameter);
end_map(context);
BIND_4CODER_TESTS(context);
int32_t result = end_bind_helper(context);
return(result);
}

View File

@ -28,7 +28,7 @@ default_font_name = "Liberation Mono";
user_name = "NAME";
// Extensions
treat_as_code = ".cpp.c.hpp.h.cc.cs.java.rs";
treat_as_code = ".cpp.c.hpp.h.cc.cs.java.rs.glsl.m";
// Load project on startup
automatically_load_project = false;