From 7774bd6da09296b249bc7fca743f2b1482b8379e Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Sat, 27 Feb 2016 12:34:13 -0500 Subject: [PATCH] metagenerating delayed action stuff --- 4coder_custom.h | 5 +- 4coder_keycodes.h | 56 ++++++------- 4ed.cpp | 54 +++++++----- 4ed_app_target.cpp | 1 + 4ed_delay.cpp | 74 +++++++++++++++++ 4ed_file.cpp | 25 ++++++ 4ed_file_view.cpp | 61 -------------- 4ed_interactive_view.cpp | 11 ++- 4ed_metagen.cpp | 174 +++++++++++++++++++++++++++++++++++++-- 9 files changed, 340 insertions(+), 121 deletions(-) create mode 100644 4ed_delay.cpp diff --git a/4coder_custom.h b/4coder_custom.h index 5e5f1bd4..81c1a628 100644 --- a/4coder_custom.h +++ b/4coder_custom.h @@ -140,13 +140,16 @@ struct File_View_Summary{ struct User_Input{ int type; + int abort; union{ Key_Event_Data key; Mouse_State mouse; }; - int abort; + unsigned long long command; }; +#define CommandEqual(c1,c2) ((unsigned long long)(c1) == (unsigned long long)(c2)) + struct Query_Bar{ String prompt; String string; diff --git a/4coder_keycodes.h b/4coder_keycodes.h index 7ecafcc6..92df28f2 100644 --- a/4coder_keycodes.h +++ b/4coder_keycodes.h @@ -1,30 +1,30 @@ enum Key_Code{ -key_back = 1, -key_up = 2, -key_down = 3, -key_left = 4, -key_right = 5, -key_del = 6, -key_insert = 7, -key_home = 8, -key_end = 11, -key_page_up = 12, -key_page_down = 13, -key_esc = 14, -key_f1 = 127, -key_f2 = 128, -key_f3 = 129, -key_f4 = 130, -key_f5 = 131, -key_f6 = 132, -key_f7 = 133, -key_f8 = 134, -key_f9 = 135, -key_f10 = 136, -key_f11 = 137, -key_f12 = 138, -key_f13 = 139, -key_f14 = 140, -key_f15 = 141, -key_f16 = 142, + key_back = 1, + key_up = 2, + key_down = 3, + key_left = 4, + key_right = 5, + key_del = 6, + key_insert = 7, + key_home = 8, + key_end = 11, + key_page_up = 12, + key_page_down = 13, + key_esc = 14, + key_f1 = 127, + key_f2 = 128, + key_f3 = 129, + key_f4 = 130, + key_f5 = 131, + key_f6 = 132, + key_f7 = 133, + key_f8 = 134, + key_f9 = 135, + key_f10 = 136, + key_f11 = 137, + key_f12 = 138, + key_f13 = 139, + key_f14 = 140, + key_f15 = 141, + key_f16 = 142, }; diff --git a/4ed.cpp b/4ed.cpp index e1521ff5..b5d54df8 100644 --- a/4ed.cpp +++ b/4ed.cpp @@ -255,47 +255,39 @@ COMMAND_DECL(write_character){ } COMMAND_DECL(seek_whitespace_right){ -#if BUFFER_EXPERIMENT_SCALPEL <= 3 ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); i32 pos = buffer_seek_whitespace_right(&file->state.buffer, view->cursor.pos); view_cursor_move(view, pos); -#endif } COMMAND_DECL(seek_whitespace_left){ -#if BUFFER_EXPERIMENT_SCALPEL <= 3 ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); i32 pos = buffer_seek_whitespace_left(&file->state.buffer, view->cursor.pos); view_cursor_move(view, pos); -#endif } COMMAND_DECL(seek_whitespace_up){ -#if BUFFER_EXPERIMENT_SCALPEL <= 3 ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); i32 pos = buffer_seek_whitespace_up(&file->state.buffer, view->cursor.pos); view_cursor_move(view, pos); -#endif } COMMAND_DECL(seek_whitespace_down){ -#if BUFFER_EXPERIMENT_SCALPEL <= 3 ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); i32 pos = buffer_seek_whitespace_down(&file->state.buffer, view->cursor.pos); view_cursor_move(view, pos); -#endif } internal i32 @@ -2183,6 +2175,22 @@ extern "C"{ return(result); } + BUFFER_SAVE_SIG(external_buffer_save){ + Command_Data *cmd = (Command_Data*)context->data; + Editing_File *file; + Working_Set *working_set; + Delay *delay; + + if (buffer->exists){ + delay = cmd->delay; + working_set = cmd->working_set; + file = working_set->files + buffer->buffer_id; + if (!file->state.is_dummy && file_is_ready(file) && buffer_needs_save(file)){ + + } + } + } + GET_VIEW_MAX_INDEX_SIG(external_get_view_max_index){ Command_Data *cmd = (Command_Data*)context->data; Live_Views *live_set = cmd->live_set; @@ -2390,7 +2398,7 @@ app_links_init(System_Functions *system){ app_links.buffer_seek_delimiter = external_buffer_seek_delimiter; app_links.buffer_read_range = external_buffer_read_range; app_links.buffer_replace_range = external_buffer_replace_range; - app_links.buffer_save = 0;//external_buffer_save; + app_links.buffer_save = external_buffer_save; app_links.get_view_max_index = external_get_view_max_index; app_links.get_file_view = external_get_file_view; @@ -3889,6 +3897,7 @@ App_Step_Sig(app_step){ Delayed_Action *act = vars->delay.acts + i; String *string = &act->string; Panel *panel = act->panel; + Editing_File *file = act->file; switch (act->type){ case DACT_OPEN: @@ -3901,23 +3910,28 @@ App_Step_Sig(app_step){ case DACT_SAVE_AS: { - View *view = panel->view; - File_View *fview = view_to_file_view(view); - - if (!fview && view->is_minor) fview = view_to_file_view(view->major); - if (fview){ - Editing_File *file = fview->file; - if (file && !file->state.is_dummy){ - i32 sys_id = file_save_and_set_names(system, exchange, mem, working_set, file, string->str); - app_push_file_binding(vars, sys_id, get_file_id(working_set, file)); + if (!file){ + View *view = panel->view; + File_View *fview = view_to_file_view(view); + + if (!fview && view->is_minor) fview = view_to_file_view(view->major); + + if (fview){ + file = working_set_lookup_file(working_set, *string); } } + if (file && !file->state.is_dummy){ + i32 sys_id = file_save_and_set_names(system, exchange, mem, working_set, file, string->str); + app_push_file_binding(vars, sys_id, get_file_id(working_set, file)); + } }break; case DACT_SAVE: { - Editing_File *file = working_set_lookup_file(working_set, *string); - if (!file->state.is_dummy){ + if (!file){ + file = working_set_lookup_file(working_set, *string); + } + if (!file->state.is_dummy && buffer_needs_save(file)){ i32 sys_id = file_save(system, exchange, mem, file, file->name.source_path.str); app_push_file_binding(vars, sys_id, get_file_id(working_set, file)); } diff --git a/4ed_app_target.cpp b/4ed_app_target.cpp index 4997fc94..41d07e30 100644 --- a/4ed_app_target.cpp +++ b/4ed_app_target.cpp @@ -52,6 +52,7 @@ #include "4ed_style.cpp" #include "4ed_file.cpp" #include "4ed_gui.cpp" +#include "4ed_delay.cpp" #include "4ed_file_view.cpp" #include "4ed_color_view.cpp" #include "4ed_interactive_view.cpp" diff --git a/4ed_delay.cpp b/4ed_delay.cpp new file mode 100644 index 00000000..de492132 --- /dev/null +++ b/4ed_delay.cpp @@ -0,0 +1,74 @@ +enum Action_Type{ + DACT_OPEN, + DACT_SAVE_AS, + DACT_SAVE, + DACT_NEW, + DACT_SWITCH, + DACT_TRY_KILL, + DACT_KILL, + DACT_CLOSE_MINOR, + DACT_CLOSE_MAJOR, + DACT_THEME_OPTIONS, + DACT_KEYBOARD_OPTIONS, +}; + +struct Delayed_Action{ + Action_Type type; + String string; + Panel* panel; + Editing_File* file; +}; + +struct Delay{ + Delayed_Action* acts; + i32 count; + i32 max; +}; + +inline Delayed_Action* +delayed_action(Delay *delay, Action_Type type){ + Delayed_Action *result; + Assert(delay->count < delay->max); + result = delay->acts + delay->count++; + *result = {}; + result->type = type; + return(result); +} + +inline Delayed_Action* +delayed_action(Delay *delay, Action_Type type, Panel* panel){ + Delayed_Action *result; + result = delayed_action(delay, type); + result->panel = panel; + return(result); +} + +inline Delayed_Action* +delayed_action(Delay *delay, Action_Type type, String string, Panel* panel){ + Delayed_Action *result; + result = delayed_action(delay, type); + result->string = string; + result->panel = panel; + return(result); +} + +inline Delayed_Action* +delayed_action(Delay *delay, Action_Type type, String string, Editing_File* file){ + Delayed_Action *result; + result = delayed_action(delay, type); + result->string = string; + result->file = file; + return(result); +} + +#define delayed_open(delay, ...) delayed_action(delay, DACT_OPEN, __VA_ARGS__) +#define delayed_save_as(delay, ...) delayed_action(delay, DACT_SAVE_AS, __VA_ARGS__) +#define delayed_save(delay, ...) delayed_action(delay, DACT_SAVE, __VA_ARGS__) +#define delayed_new(delay, ...) delayed_action(delay, DACT_NEW, __VA_ARGS__) +#define delayed_switch(delay, ...) delayed_action(delay, DACT_SWITCH, __VA_ARGS__) +#define delayed_try_kill(delay, ...) delayed_action(delay, DACT_TRY_KILL, __VA_ARGS__) +#define delayed_kill(delay, ...) delayed_action(delay, DACT_KILL, __VA_ARGS__) +#define delayed_close_minor(delay, ...) delayed_action(delay, DACT_CLOSE_MINOR, __VA_ARGS__) +#define delayed_close_major(delay, ...) delayed_action(delay, DACT_CLOSE_MAJOR, __VA_ARGS__) +#define delayed_theme_options(delay, ...) delayed_action(delay, DACT_THEME_OPTIONS, __VA_ARGS__) +#define delayed_keyboard_options(delay, ...) delayed_action(delay, DACT_KEYBOARD_OPTIONS, __VA_ARGS__) diff --git a/4ed_file.cpp b/4ed_file.cpp index 3b9f083b..bed5e548 100644 --- a/4ed_file.cpp +++ b/4ed_file.cpp @@ -374,5 +374,30 @@ hot_directory_first_match(Hot_Directory *hot_directory, return result; } +enum File_Sync_State{ + SYNC_GOOD, + SYNC_BEHIND_OS, + SYNC_UNSAVED +}; + +inline File_Sync_State +buffer_get_sync(Editing_File *file){ + File_Sync_State result = SYNC_GOOD; + if (file->state.last_4ed_write_time != file->state.last_sys_write_time) + result = SYNC_BEHIND_OS; + else if (file->state.last_4ed_edit_time > file->state.last_sys_write_time) + result = SYNC_UNSAVED; + return result; +} + +inline b32 +buffer_needs_save(Editing_File *file){ + b32 result = 0; + if (file->settings.unimportant == 0) + if (buffer_get_sync(file) == SYNC_UNSAVED) + result = 1; + return(result); +} + // BOTTOM diff --git a/4ed_file_view.cpp b/4ed_file_view.cpp index 86ef2c83..ecd5c15f 100644 --- a/4ed_file_view.cpp +++ b/4ed_file_view.cpp @@ -19,42 +19,6 @@ struct Incremental_Search{ i32 pos; }; -enum Action_Type{ - DACT_OPEN, - DACT_SAVE_AS, - DACT_SAVE, - DACT_NEW, - DACT_SWITCH, - DACT_TRY_KILL, - DACT_KILL, - DACT_CLOSE_MINOR, - DACT_CLOSE_MAJOR, - DACT_THEME_OPTIONS, - DACT_KEYBOARD_OPTIONS -}; - -struct Delayed_Action{ - Action_Type type; - String string; - Panel *panel; -}; - -struct Delay{ - Delayed_Action acts[8]; - i32 count, max; -}; - -inline void -delayed_action(Delay *delay, Action_Type type, - String string, Panel *panel){ - Assert(delay->count < delay->max); - Delayed_Action action; - action.type = type; - action.string = string; - action.panel = panel; - delay->acts[delay->count++] = action; -} - enum File_View_Widget_Type{ FWIDG_NONE, //FWIDG_SEARCH, @@ -2959,31 +2923,6 @@ step_file_view(System_Functions *system, View *view_, i32_Rect rect, return result; } -enum File_Sync_State{ - SYNC_GOOD, - SYNC_BEHIND_OS, - SYNC_UNSAVED -}; - -inline File_Sync_State -buffer_get_sync(Editing_File *file){ - File_Sync_State result = SYNC_GOOD; - if (file->state.last_4ed_write_time != file->state.last_sys_write_time) - result = SYNC_BEHIND_OS; - else if (file->state.last_4ed_edit_time > file->state.last_sys_write_time) - result = SYNC_UNSAVED; - return result; -} - -inline b32 -buffer_needs_save(Editing_File *file){ - b32 result = 0; - if (file->settings.unimportant == 0) - if (buffer_get_sync(file) == SYNC_UNSAVED) - result = 1; - return(result); -} - internal void draw_file_setup_bar(Style *style, i32 line_height, Interactive_Bar *bar, i32_Rect *rect){ bar->style = style->main.file_info_style; diff --git a/4ed_interactive_view.cpp b/4ed_interactive_view.cpp index 32d8643b..70b23868 100644 --- a/4ed_interactive_view.cpp +++ b/4ed_interactive_view.cpp @@ -55,14 +55,13 @@ interactive_view_complete(Interactive_View *view){ Panel *panel = view->view_base.panel; switch (view->action){ case INTV_OPEN: - delayed_action(view->delay, DACT_OPEN, - view->hot_directory->string, panel); - break; + delayed_open(view->delay, view->hot_directory->string, panel); + break; case INTV_SAVE_AS: - delayed_action(view->delay, DACT_SAVE_AS, view->hot_directory->string, panel); - delayed_action(view->delay, DACT_CLOSE_MINOR, {}, panel); - break; + delayed_save_as(view->delay, view->hot_directory->string, panel); + delayed_close_minor(view->delay, panel); + break; case INTV_NEW: delayed_action(view->delay, DACT_NEW, view->hot_directory->string, panel); diff --git a/4ed_metagen.cpp b/4ed_metagen.cpp index c9a82877..69c2e310 100644 --- a/4ed_metagen.cpp +++ b/4ed_metagen.cpp @@ -11,10 +11,43 @@ #define ArrayCount(a) (sizeof(a)/sizeof(*a)) +struct Struct_Field{ + char *type; + char *name; +}; + +void to_lower(char *src, char *dst){ + char *c, ch; + for (c = src; *c != 0; ++c){ + ch = *c; + if (ch >= 'A' && ch <= 'Z'){ + ch += ('a' - 'A'); + } + *dst++ = ch; + } + *dst = 0; +} + #include #include #include +void struct_begin(FILE *file, char *name){ + fprintf(file, "struct %s{\n", name); +} + +void struct_fields(FILE *file, Struct_Field *fields, int count){ + int i; + for (i = 0; i < count; ++i){ + fprintf(file, " %s %s;\n", fields[i].type, fields[i].name); + } +} + +void struct_end(FILE *file){ + fprintf(file, "};\n\n"); +} + + char *keys_that_need_codes[] = { "back", "up", @@ -48,9 +81,9 @@ char *keys_that_need_codes[] = { "f16", }; -void generate_keycode_enum(){ +char* generate_keycode_enum(){ FILE *file; - char filename[] = "4coder_keycodes.h"; + char *filename = "4coder_keycodes.h"; int i, count; unsigned char code = 1; @@ -66,17 +99,148 @@ void generate_keycode_enum(){ case '\t': code++; break; case 0x20: code = 0x7F; break; default: - fprintf(file, "key_%s = %d,\n", keys_that_need_codes[i++], code++); + fprintf(file, " key_%s = %d,\n", keys_that_need_codes[i++], code++); break; } } fprintf(file, "};\n"); fclose(file); - printf("gen success: %s\n", filename); + return(filename); +} + + +char daction_enum_name[] = "Action_Type"; +char *daction_enum[] = { + "OPEN", + "SAVE_AS", + "SAVE", + "NEW", + "SWITCH", + "TRY_KILL", + "KILL", + "CLOSE_MINOR", + "CLOSE_MAJOR", + "THEME_OPTIONS", + "KEYBOARD_OPTIONS" +}; + +char daction_name[] = "Delayed_Action"; +Struct_Field daction_fields[] = { + {"Action_Type", "type"}, +}; +Struct_Field daction_fields_primary[] = { + {"String", "string"}, + {"Panel*", "panel"}, + {"Editing_File*", "file"}, +}; +enum Daction_Field_Handle{ + dfph_null, + dfph_string, + dfph_panel, + dfph_file, +}; +Daction_Field_Handle dact_param_sets[] = { + dfph_panel, dfph_null, + dfph_string, dfph_panel, dfph_null, + dfph_string, dfph_file, dfph_null +}; + +char delay_name[] = "Delay"; +Struct_Field delay_fields[] = { + {"Delayed_Action*", "acts"}, + {"i32", "count"}, + {"i32", "max"}, +}; + +// TODO(allen): Make delay buffer expandable (general memory probably) +char delayed_action_function_top[] = +"inline Delayed_Action*\n" +"delayed_action(Delay *delay, Action_Type type"; + +char delayed_action_function_bottom[] = +"){\n" +" Delayed_Action *result;\n" +" Assert(delay->count < delay->max);\n" +" result = delay->acts + delay->count++;\n" +" *result = {};\n" +" result->type = type;\n" +" return(result);\n" +"}\n\n"; + +char delayed_action_special_param[] = ", %s %s"; + +char delayed_action_specialized_middle[] = +"){\n" +" Delayed_Action *result;\n" +" result = delayed_action(delay, type);\n"; + +char delayed_action_special_line[] = +" result->%s = %s;\n"; + +char delayed_action_specialized_bottom[] = +" return(result);\n" +"}\n\n"; + +char delayed_action_macro[] = +"#define delayed_%s(delay, ...) delayed_action(delay, DACT_%s, __VA_ARGS__)\n"; + +char* generate_delayed_action(){ + FILE *file; + char *filename = "4ed_delay.cpp"; + char scratch[256]; + int i,j; + + file = fopen(filename, "wb"); + + fprintf(file, "enum %s{\n", daction_enum_name); + for (i = 0; i < ArrayCount(daction_enum); ++i){ + fprintf(file, " DACT_%s,\n", daction_enum[i]); + } + fprintf(file, "};\n\n"); + + struct_begin(file, daction_name); + struct_fields(file, daction_fields, ArrayCount(daction_fields)); + struct_fields(file, daction_fields_primary, ArrayCount(daction_fields_primary)); + struct_end(file); + + struct_begin(file, delay_name); + struct_fields(file, delay_fields, ArrayCount(delay_fields)); + struct_end(file); + + fprintf(file, "%s%s", delayed_action_function_top, delayed_action_function_bottom); + + for (i = 0; i < ArrayCount(dact_param_sets); ++i){ + j = i; + fprintf(file, "%s", delayed_action_function_top); + for (; dact_param_sets[i] != dfph_null; ++i){ + Struct_Field field = daction_fields_primary[dact_param_sets[i] - 1]; + fprintf(file, delayed_action_special_param, field.type, field.name); + } + fprintf(file, "%s", delayed_action_specialized_middle); + for (; dact_param_sets[j] != dfph_null; ++j){ + Struct_Field field = daction_fields_primary[dact_param_sets[j] - 1]; + fprintf(file, delayed_action_special_line, field.name, field.name); + } + fprintf(file, "%s", delayed_action_specialized_bottom); + } + + for (i = 0; i < ArrayCount(daction_enum); ++i){ + to_lower(daction_enum[i], scratch); + fprintf(file, delayed_action_macro, scratch, daction_enum[i]); + } + + return(filename); } int main(){ - generate_keycode_enum(); + char *filename; + + + filename = generate_keycode_enum(); + printf("gen success: %s\n", filename); + + filename = generate_delayed_action(); + printf("gen success: %s\n", filename); } // BOTTOM