From 9c6fdde85d054cee0b03d410a3245f1f1c3388a5 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Thu, 25 Feb 2016 07:12:09 -0500 Subject: [PATCH] API tweaks --- 4coder_buffer_types.h | 19 +- 4coder_custom.cpp | 1074 ++++++++++++++++++++--------------------- 4coder_helper.h | 15 + 4coder_string.h | 5 + 4ed.cpp | 254 ++++------ 4ed_app_target.cpp | 10 + 4ed_file_view.cpp | 4 - win32_4ed.cpp | 11 +- 8 files changed, 662 insertions(+), 730 deletions(-) diff --git a/4coder_buffer_types.h b/4coder_buffer_types.h index 2b5dd3a8..43ea2dfc 100644 --- a/4coder_buffer_types.h +++ b/4coder_buffer_types.h @@ -12,6 +12,15 @@ #ifndef FRED_BUFFER_TYPES_H #define FRED_BUFFER_TYPES_H +typedef union Range{ + struct{ + int min, max; + }; + struct{ + int start, end; + }; +} Range; + typedef struct Full_Cursor{ int pos; int line, character; @@ -66,12 +75,10 @@ seek_unwrapped_xy(float x, float y, int round_down){ static Buffer_Seek seek_xy(float x, float y, int round_down, int unwrapped){ Buffer_Seek result; - if (unwrapped){ - result = seek_unwrapped_xy(x,y,round_down); - } - else{ - result = seek_wrapped_xy(x,y,round_down); - } + result.type = unwrapped?buffer_seek_unwrapped_xy:buffer_seek_wrapped_xy; + result.x = x; + result.y = y; + result.round_down = round_down; return(result); } diff --git a/4coder_custom.cpp b/4coder_custom.cpp index 090e3d71..330ab1d5 100644 --- a/4coder_custom.cpp +++ b/4coder_custom.cpp @@ -1,552 +1,522 @@ -/* - * Example use of customization API - */ - -#define FCPP_STRING_IMPLEMENTATION -#include "4coder_string.h" - -// NOTE(allen): See exec_command and surrounding code in 4coder_helper.h -// to decide whether you want macro translations, without them you will -// have to manipulate the command and parameter stack through -// "app->" which may be more or less clear depending on your use. -// -// I suggest you try disabling macro translation and getting your code working -// that way, because I'll be removing it entirely sometime soon -#define DisableMacroTranslations 0 - -#include "4coder_custom.h" -#include "4coder_helper.h" - -#ifndef literal -#define literal(s) s, (sizeof(s)-1) -#endif - -// NOTE(allen|a3.3): All of your custom ids should be enumerated -// as shown here, they may start at 0, and you can only have -// 2^24 of them so don't be wasteful! -enum My_Maps{ - my_code_map, - my_html_map -}; - -HOOK_SIG(my_start){ - exec_command(cmd_context, cmdid_open_panel_vsplit); - exec_command(cmd_context, cmdid_change_active_panel); -} - -char *get_extension(const char *filename, int len, int *extension_len){ - char *c = (char*)(filename + len - 1); - char *end = c; - while (*c != '.' && c > filename) --c; - *extension_len = (int)(end - c); - return c+1; -} - -bool str_match(const char *a, int len_a, const char *b, int len_b){ - bool result = 0; - if (len_a == len_b){ - char *end = (char*)(a + len_a); - while (a < end && *a == *b){ - ++a; ++b; - } - if (a == end) result = 1; - } - return result; -} - -HOOK_SIG(my_file_settings){ - Buffer_Summary buffer = app->get_active_buffer(cmd_context); - - // NOTE(allen|a3.4.2): Whenever you ask for a buffer, you should first check that - // the exists field is set to true. Reasons why the buffer might not exist: - // -The active panel does not contain a buffer and get_active_buffer was used - // -The index provided to get_buffer was out of range [0,max) or that index is associated to a dummy buffer - // -The name provided to get_buffer_by_name did not match any of the existing buffers - if (buffer.exists){ - int treat_as_code = 0; - - if (buffer.file_name && buffer.size < (16 << 20)){ - int extension_len; - char *extension = get_extension(buffer.file_name, buffer.file_name_len, &extension_len); - if (str_match(extension, extension_len, literal("cpp"))) treat_as_code = 1; - else if (str_match(extension, extension_len, literal("h"))) treat_as_code = 1; - else if (str_match(extension, extension_len, literal("c"))) treat_as_code = 1; - else if (str_match(extension, extension_len, literal("hpp"))) treat_as_code = 1; - } - - push_parameter(app, cmd_context, par_lex_as_cpp_file, treat_as_code); - push_parameter(app, cmd_context, par_wrap_lines, !treat_as_code); - push_parameter(app, cmd_context, par_key_mapid, (treat_as_code)?(my_code_map):(mapid_file)); - exec_command(cmd_context, cmdid_set_settings); - } -} - -CUSTOM_COMMAND_SIG(write_increment){ - char text[] = "++"; - int size = sizeof(text) - 1; - - // NOTE(allen|a3.4.2): In addition to checking whether the buffer exists after a query, - // if you're going to read from or write to the buffer, you should check ready. A buffer - // is usually ready, but when a buffer has just been opened it is possible that the contents - // haven't been filled yet. If the buffer is not ready trying to read or write it is invalid. - // (See my_file_settings for comments on the exists field). - Buffer_Summary buffer = app->get_active_buffer(cmd_context); - if (buffer.exists && buffer.ready){ - app->buffer_replace_range(cmd_context, &buffer, buffer.file_cursor_pos, buffer.file_cursor_pos, text, size); - } -} - -CUSTOM_COMMAND_SIG(write_decrement){ - char text[] = "--"; - int size = sizeof(text) - 1; - Buffer_Summary buffer = app->get_active_buffer(cmd_context); - if (buffer.exists && buffer.ready){ - app->buffer_replace_range(cmd_context, &buffer, buffer.file_cursor_pos, buffer.file_cursor_pos, text, size); - } -} - -CUSTOM_COMMAND_SIG(open_long_braces){ - File_View_Summary view; - Buffer_Summary buffer; - - view = app->get_active_file_view(cmd_context); - if (view.exists){ - buffer = app->get_active_buffer(cmd_context); - - char text[] = "{\n\n}"; - int size = sizeof(text) - 1; - int pos; - - if (buffer.exists && buffer.ready){ - pos = view.cursor.pos; - app->buffer_replace_range(cmd_context, &buffer, pos, pos, text, size); - app->view_set_cursor(cmd_context, &view, seek_pos(pos + 2), 1); - app->view_set_mark(cmd_context, &view, seek_pos(pos + 4)); - - push_parameter(app, cmd_context, par_range_start, pos); - push_parameter(app, cmd_context, par_range_end, pos + size); - push_parameter(app, cmd_context, par_clear_blank_lines, 0); - exec_command(cmd_context, cmdid_auto_tab_range); - } - } -} - -CUSTOM_COMMAND_SIG(ifdef_off){ - File_View_Summary view; - Buffer_Summary buffer; - - view = app->get_active_file_view(cmd_context); - if (view.exists){ - buffer = app->get_active_buffer(cmd_context); - - char text1[] = "#if 0\n"; - int size1 = sizeof(text1) - 1; - - char text2[] = "#endif\n"; - int size2 = sizeof(text2) - 1; - - int pos, c, m; - - if (buffer.exists && buffer.ready){ - c = view.cursor.pos; - m = view.mark.pos; - pos = (cbuffer_replace_range(cmd_context, &buffer, pos, pos, text1, size1); - - push_parameter(app, cmd_context, par_range_start, pos); - push_parameter(app, cmd_context, par_range_end, pos); - exec_command(cmd_context, cmdid_auto_tab_range); - - - app->refresh_file_view(cmd_context, &view); - c = view.cursor.pos; - m = view.mark.pos; - pos = (c>m)?(c):(m); - - - app->buffer_replace_range(cmd_context, &buffer, pos, pos, text2, size2); - - push_parameter(app, cmd_context, par_range_start, pos); - push_parameter(app, cmd_context, par_range_end, pos); - exec_command(cmd_context, cmdid_auto_tab_range); - } - } -} - -CUSTOM_COMMAND_SIG(backspace_word){ - File_View_Summary view; - Buffer_Summary buffer; - int pos2, pos1; - - view = app->get_active_file_view(cmd_context); - if (view.exists){ - pos2 = view.cursor.pos; - exec_command(cmd_context, cmdid_seek_alphanumeric_left); - app->refresh_file_view(cmd_context, &view); - pos1 = view.cursor.pos; - - if (pos1 < pos2){ - buffer = app->get_buffer(cmd_context, view.file_id); - app->buffer_replace_range(cmd_context, &buffer, pos1, pos2, 0, 0); - } - } -} - -CUSTOM_COMMAND_SIG(switch_to_compilation){ - File_View_Summary view; - Buffer_Summary buffer; - - char name[] = "*compilation*"; - int name_size = sizeof(name)-1; - - // TODO(allen): This will only work for file views for now. Extend the API - // a bit to handle a general view type which can be manipulated at least enough - // to change the specific type of view and set files even when the view didn't - // contain a file. - view = app->get_active_file_view(cmd_context); - if (view.exists){ - buffer = app->get_buffer_by_name(cmd_context, make_string(name, name_size)); - if (buffer.exists){ - app->view_set_file(cmd_context, &view, buffer.file_id); - } - } -} - -CUSTOM_COMMAND_SIG(move_up_10){ - File_View_Summary view; - float x, y; - - view = app->get_active_file_view(cmd_context); - if (view.exists){ - x = view.preferred_x; - - if (view.unwrapped_lines){ - y = view.cursor.unwrapped_y; - } - else{ - y = view.cursor.wrapped_y; - } - - y -= 10*view.line_height; - - app->view_set_cursor(cmd_context, &view, seek_xy(x, y, 0, view.unwrapped_lines), 0); - } -} - -CUSTOM_COMMAND_SIG(move_down_10){ - File_View_Summary view; - float x, y; - - view = app->get_active_file_view(cmd_context); - if (view.exists){ - x = view.preferred_x; - - if (view.unwrapped_lines){ - y = view.cursor.wrapped_y; - } - else{ - y = view.cursor.wrapped_y; - } - - y += 10*view.line_height; - - app->view_set_cursor(cmd_context, &view, seek_xy(x, y, 0, view.unwrapped_lines), 0); - } -} - -CUSTOM_COMMAND_SIG(switch_to_file_in_quotes){ - File_View_Summary view; - Buffer_Summary buffer; - char short_file_name[128]; - int pos, start, end, size; - - view = app->get_active_file_view(cmd_context); - if (view.exists){ - buffer = app->get_buffer(cmd_context, view.file_id); - if (buffer.exists && buffer.ready){ - pos = view.cursor.pos; - app->buffer_seek_delimiter(cmd_context, &buffer, pos, '"', 1, &end); - app->buffer_seek_delimiter(cmd_context, &buffer, pos, '"', 0, &start); - - ++start; - - size = end - start; - if (size < sizeof(short_file_name)){ - char file_name_[256]; - String file_name = make_fixed_width_string(file_name_); - - app->buffer_read_range(cmd_context, &buffer, start, end, short_file_name); - - copy(&file_name, make_string(buffer.file_name, buffer.file_name_len)); - truncate_to_path_of_directory(&file_name); - append(&file_name, make_string(short_file_name, size)); - - buffer = app->get_buffer_by_name(cmd_context, file_name); - if (buffer.exists){ - app->view_set_file(cmd_context, &view, buffer.file_id); - } - else{ - push_parameter(app, cmd_context, par_name, expand_str(file_name)); - exec_command(cmd_context, cmdid_interactive_open); - } - } - } - } -} - -CUSTOM_COMMAND_SIG(open_in_other){ - exec_command(cmd_context, cmdid_change_active_panel); - exec_command(cmd_context, cmdid_interactive_open); -} - -CUSTOM_COMMAND_SIG(open_my_files){ - // NOTE(allen|a3.1): The command cmdid_interactive_open can now open - // a file specified on the parameter stack. If the file does not exist - // cmdid_interactive_open behaves as usual. - push_parameter(app, cmd_context, par_name, literal("w:/4ed/data/test/basic.cpp")); - exec_command(cmd_context, cmdid_interactive_open); - - exec_command(cmd_context, cmdid_change_active_panel); - - char my_file[256]; - int my_file_len; - - my_file_len = sizeof("w:/4ed/data/test/basic.txt") - 1; - for (int i = 0; i < my_file_len; ++i){ - my_file[i] = ("w:/4ed/data/test/basic.txt")[i]; - } - - // NOTE(allen|a3.1): null terminators are not needed for strings. - push_parameter(app, cmd_context, par_name, my_file, my_file_len); - exec_command(cmd_context, cmdid_interactive_open); - - exec_command(cmd_context, cmdid_change_active_panel); -} - -CUSTOM_COMMAND_SIG(build_at_launch_location){ - // NOTE(allen|a3.3): An example of calling build by setting all - // parameters directly. This only works if build.bat can be called - // from the starting directory - push_parameter(app, cmd_context, par_cli_overlap_with_conflict, 1); - push_parameter(app, cmd_context, par_name, literal("*compilation*")); - push_parameter(app, cmd_context, par_cli_path, literal(".")); - push_parameter(app, cmd_context, par_cli_command, literal("build")); - exec_command(cmd_context, cmdid_build); -} - -CUSTOM_COMMAND_SIG(build_search){ - // NOTE(allen|a3.3): An example of traversing the filesystem through parent - // directories looking for a file, in this case a batch file to execute. - // - // - // Step 1: push_directory returns a String containing the current "hot" directory - // (whatever directory you most recently visited in the 4coder file browsing interface) - // - // Step 2: app->directory_has_file queries the file system to see if "build.bat" exists - // If it does exist several parameters are pushed: - // - par_cli_overlap_with_conflict: whether to launch this process if an existing process - // is already being used for output on the same buffer - // - // - par_name: the name of the buffer to fill with the output from the process - // - // - par_cli_path: sets the path from which the command is executed - // - // - par_cli_command: sets the actual command to be executed, this can be almost any command - // that you could execute through a command line interface - // - // - // To set par_cli_path: push_parameter makes a copy of the dir string on the stack - // because the string allocated by push_directory is going to change again - // To set par_cli_command: app->push_parameter does not make a copy of the dir because - // dir isn't going to change again. - // - // Step 3: If the batch file did not exist try to move to the parent directory using - // app->directory_cd. The cd function can also be used to navigate to subdirectories. - // It returns true if it can actually move in the specified direction, and false otherwise. - - int keep_going = 1; - String dir = push_directory(app, cmd_context); - while (keep_going){ - if (app->directory_has_file(dir, "build.bat")){ - push_parameter(app, cmd_context, par_cli_overlap_with_conflict, 0); - push_parameter(app, cmd_context, par_name, literal("*compilation*")); - push_parameter(app, cmd_context, par_cli_path, dir.str, dir.size); - - if (append(&dir, "build")){ -#if 1 - // NOTE(allen): This version avoids an unecessary copy, both equivalents are - // included to demonstrate how using push_parameter without the helper looks. - app->push_parameter(cmd_context, - dynamic_int(par_cli_command), - dynamic_string(dir.str, dir.size)); -#else - push_parameter(cmd_context, par_cli_command, dir.str, dir.size); -#endif - - exec_command(cmd_context, cmdid_build); - } - else{ - clear_parameters(cmd_context); - } - - return; - } - - if (app->directory_cd(&dir, "..") == 0){ - keep_going = 0; - } - } - - // TODO(allen): feedback message - couldn't find build.bat -} - -CUSTOM_COMMAND_SIG(write_and_auto_tab){ - exec_command(cmd_context, cmdid_write_character); - exec_command(cmd_context, cmdid_auto_tab_line_at_cursor); -} - -extern "C" GET_BINDING_DATA(get_bindings){ - Bind_Helper context_actual = begin_bind_helper(data, size); - Bind_Helper *context = &context_actual; - - // NOTE(allen|a3.1): Right now hooks have no loyalties to maps, all hooks are - // global and once set they always apply, regardless of what map is active. - set_hook(context, hook_start, my_start); - set_hook(context, hook_open_file, my_file_settings); - - begin_map(context, mapid_global); - - bind(context, 'p', MDFR_CTRL, cmdid_open_panel_vsplit); - bind(context, '_', MDFR_CTRL, cmdid_open_panel_hsplit); - bind(context, 'P', MDFR_CTRL, cmdid_close_panel); - bind(context, 'n', MDFR_CTRL, cmdid_interactive_new); - bind(context, 'o', MDFR_CTRL, cmdid_interactive_open); - bind(context, ',', MDFR_CTRL, cmdid_change_active_panel); - bind(context, 'k', MDFR_CTRL, cmdid_interactive_kill_buffer); - bind(context, 'i', MDFR_CTRL, cmdid_interactive_switch_buffer); - bind(context, 'c', MDFR_ALT, cmdid_open_color_tweaker); - bind(context, 'x', MDFR_ALT, cmdid_open_menu); - bind(context, 'o', MDFR_ALT, open_in_other); - - // NOTE(allen): These callbacks may not actually be useful to you, but - // go look at them and see what they do. - bind(context, 'M', MDFR_ALT | MDFR_CTRL, open_my_files); - bind(context, 'M', MDFR_ALT, build_at_launch_location); - bind(context, 'm', MDFR_ALT, build_search); - - end_map(context); - - - begin_map(context, my_code_map); - - // NOTE(allen|a3.1): Set this map (my_code_map == mapid_user_custom) to - // inherit from mapid_file. When searching if a key is bound - // in this map, if it is not found here it will then search mapid_file. - // - // If this is not set, it defaults to mapid_global. - inherit_map(context, mapid_file); - - // NOTE(allen|a3.1): Children can override parent's bindings. - bind(context, codes->right, MDFR_CTRL, cmdid_seek_alphanumeric_or_camel_right); - bind(context, codes->left, MDFR_CTRL, cmdid_seek_alphanumeric_or_camel_left); - - // NOTE(allen|a3.2): Specific keys can override vanilla keys, - // and write character writes whichever character corresponds - // to the key that triggered the command. - bind(context, '\n', 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, '#', MDFR_NONE, write_and_auto_tab); - - bind(context, '\t', MDFR_NONE, cmdid_word_complete); - bind(context, '\t', MDFR_CTRL, cmdid_auto_tab_range); - bind(context, '\t', MDFR_SHIFT, cmdid_auto_tab_line_at_cursor); - - bind(context, '\n', MDFR_SHIFT, write_and_auto_tab); - bind(context, ' ', MDFR_SHIFT, cmdid_write_character); - - bind(context, '=', MDFR_CTRL, write_increment); - bind(context, '-', MDFR_CTRL, write_decrement); - bind(context, '[', MDFR_CTRL, open_long_braces); - bind(context, 'i', MDFR_ALT, ifdef_off); - bind(context, '1', MDFR_ALT, switch_to_file_in_quotes); - - end_map(context); - - - begin_map(context, mapid_file); - - // NOTE(allen|a3.1): Binding this essentially binds all key combos that - // would normally insert a character into a buffer. - // Or apply this rule (which always works): if the code for the key - // is not in the codes struct, it is a vanilla key. - // It is possible to override this binding for individual keys. - bind_vanilla_keys(context, cmdid_write_character); - - bind(context, codes->left, MDFR_NONE, cmdid_move_left); - bind(context, codes->right, MDFR_NONE, cmdid_move_right); - bind(context, codes->del, MDFR_NONE, cmdid_delete); - bind(context, codes->back, MDFR_NONE, cmdid_backspace); - bind(context, codes->up, MDFR_NONE, cmdid_move_up); - bind(context, codes->down, MDFR_NONE, cmdid_move_down); - bind(context, codes->end, MDFR_NONE, cmdid_seek_end_of_line); - bind(context, codes->home, MDFR_NONE, cmdid_seek_beginning_of_line); - bind(context, codes->page_up, MDFR_NONE, cmdid_page_up); - bind(context, codes->page_down, MDFR_NONE, cmdid_page_down); - - bind(context, codes->right, MDFR_CTRL, cmdid_seek_whitespace_right); - bind(context, codes->left, MDFR_CTRL, cmdid_seek_whitespace_left); - bind(context, codes->up, MDFR_CTRL, cmdid_seek_whitespace_up); - bind(context, codes->down, MDFR_CTRL, cmdid_seek_whitespace_down); - - bind(context, codes->up, MDFR_ALT, move_up_10); - bind(context, codes->down, MDFR_ALT, move_down_10); - - bind(context, codes->back, MDFR_CTRL, backspace_word); - - bind(context, ' ', MDFR_CTRL, cmdid_set_mark); - bind(context, 'm', MDFR_CTRL, cmdid_cursor_mark_swap); - bind(context, 'c', MDFR_CTRL, cmdid_copy); - bind(context, 'x', MDFR_CTRL, cmdid_cut); - bind(context, 'v', MDFR_CTRL, cmdid_paste); - bind(context, 'V', MDFR_CTRL, cmdid_paste_next); - bind(context, 'Z', MDFR_CTRL, cmdid_timeline_scrub); - bind(context, 'z', MDFR_CTRL, cmdid_undo); - bind(context, 'y', MDFR_CTRL, cmdid_redo); - bind(context, codes->left, MDFR_ALT, cmdid_increase_rewind_speed); - bind(context, codes->right, MDFR_ALT, cmdid_increase_fastforward_speed); - bind(context, codes->down, MDFR_ALT, cmdid_stop_rewind_fastforward); - bind(context, 'h', MDFR_CTRL, cmdid_history_backward); - bind(context, 'H', MDFR_CTRL, cmdid_history_forward); - bind(context, 'd', MDFR_CTRL, cmdid_delete_range); - bind(context, 'l', MDFR_CTRL, cmdid_toggle_line_wrap); - bind(context, 'L', MDFR_CTRL, cmdid_toggle_endline_mode); - bind(context, 'u', MDFR_CTRL, cmdid_to_uppercase); - bind(context, 'j', MDFR_CTRL, cmdid_to_lowercase); - bind(context, '?', MDFR_CTRL, cmdid_toggle_show_whitespace); - - bind(context, '~', MDFR_CTRL, cmdid_clean_all_lines); - bind(context, '1', MDFR_CTRL, cmdid_eol_dosify); - bind(context, '!', MDFR_CTRL, cmdid_eol_nixify); - - bind(context, 'f', MDFR_CTRL, cmdid_search); - bind(context, 'r', MDFR_CTRL, cmdid_reverse_search); - bind(context, 'g', MDFR_CTRL, cmdid_goto_line); - - bind(context, 'K', MDFR_CTRL, cmdid_kill_buffer); - bind(context, 'O', MDFR_CTRL, cmdid_reopen); - bind(context, 'w', MDFR_CTRL, cmdid_interactive_save_as); - bind(context, 's', MDFR_CTRL, cmdid_save); - - bind(context, ',', MDFR_ALT, switch_to_compilation); - - end_map(context); - end_bind_helper(context); - - return context->write_total; -} - - +/* + * Example use of customization API + */ + +#define FCPP_STRING_IMPLEMENTATION +#include "4coder_string.h" + +// NOTE(allen): See exec_command and surrounding code in 4coder_helper.h +// to decide whether you want macro translations, without them you will +// have to manipulate the command and parameter stack through +// "app->" which may be more or less clear depending on your use. +// +// I suggest you try disabling macro translation and getting your code working +// that way, because I'll be removing it entirely sometime soon +#define DisableMacroTranslations 0 + +#include "4coder_custom.h" +#include "4coder_helper.h" + +#ifndef literal +#define literal(s) s, (sizeof(s)-1) +#endif + +// NOTE(allen|a3.3): All of your custom ids should be enumerated +// as shown here, they may start at 0, and you can only have +// 2^24 of them so don't be wasteful! +enum My_Maps{ + my_code_map, + my_html_map +}; + +HOOK_SIG(my_start){ + exec_command(cmd_context, cmdid_open_panel_vsplit); + exec_command(cmd_context, cmdid_change_active_panel); +} + +char *get_extension(const char *filename, int len, int *extension_len){ + char *c = (char*)(filename + len - 1); + char *end = c; + while (*c != '.' && c > filename) --c; + *extension_len = (int)(end - c); + return c+1; +} + +bool str_match(const char *a, int len_a, const char *b, int len_b){ + bool result = 0; + if (len_a == len_b){ + char *end = (char*)(a + len_a); + while (a < end && *a == *b){ + ++a; ++b; + } + if (a == end) result = 1; + } + return result; +} + +HOOK_SIG(my_file_settings){ + Buffer_Summary buffer = app->get_active_buffer(cmd_context); + + // NOTE(allen|a3.4.2): Whenever you ask for a buffer, you can check that + // the exists field is set to true. Reasons why the buffer might not exist: + // -The active panel does not contain a buffer and get_active_buffer was used + // -The index provided to get_buffer was out of range [0,max) or that index is associated to a dummy buffer + // -The name provided to get_buffer_by_name did not match any of the existing buffers + if (buffer.exists){ + int treat_as_code = 0; + + if (buffer.file_name && buffer.size < (16 << 20)){ + int extension_len; + char *extension = get_extension(buffer.file_name, buffer.file_name_len, &extension_len); + if (str_match(extension, extension_len, literal("cpp"))) treat_as_code = 1; + else if (str_match(extension, extension_len, literal("h"))) treat_as_code = 1; + else if (str_match(extension, extension_len, literal("c"))) treat_as_code = 1; + else if (str_match(extension, extension_len, literal("hpp"))) treat_as_code = 1; + } + + push_parameter(app, cmd_context, par_lex_as_cpp_file, treat_as_code); + push_parameter(app, cmd_context, par_wrap_lines, !treat_as_code); + push_parameter(app, cmd_context, par_key_mapid, (treat_as_code)?(my_code_map):(mapid_file)); + exec_command(cmd_context, cmdid_set_settings); + } +} + +CUSTOM_COMMAND_SIG(write_increment){ + char text[] = "++"; + int size = sizeof(text) - 1; + Buffer_Summary buffer = app->get_active_buffer(cmd_context); + app->buffer_replace_range(cmd_context, &buffer, buffer.file_cursor_pos, buffer.file_cursor_pos, text, size); +} + +CUSTOM_COMMAND_SIG(write_decrement){ + char text[] = "--"; + int size = sizeof(text) - 1; + Buffer_Summary buffer = app->get_active_buffer(cmd_context); + app->buffer_replace_range(cmd_context, &buffer, buffer.file_cursor_pos, buffer.file_cursor_pos, text, size); +} + +CUSTOM_COMMAND_SIG(open_long_braces){ + File_View_Summary view; + Buffer_Summary buffer; + char text[] = "{\n\n}"; + int size = sizeof(text) - 1; + int pos; + + view = app->get_active_file_view(cmd_context); + buffer = app->get_buffer(cmd_context, view.file_id); + + pos = view.cursor.pos; + app->buffer_replace_range(cmd_context, &buffer, pos, pos, text, size); + app->view_set_cursor(cmd_context, &view, seek_pos(pos + 2), 1); + app->view_set_mark(cmd_context, &view, seek_pos(pos + 4)); + + push_parameter(app, cmd_context, par_range_start, pos); + push_parameter(app, cmd_context, par_range_end, pos + size); + push_parameter(app, cmd_context, par_clear_blank_lines, 0); + exec_command(cmd_context, cmdid_auto_tab_range); +} + +CUSTOM_COMMAND_SIG(ifdef_off){ + File_View_Summary view; + Buffer_Summary buffer; + + char text1[] = "#if 0\n"; + int size1 = sizeof(text1) - 1; + + char text2[] = "#endif\n"; + int size2 = sizeof(text2) - 1; + + Range range; + int pos; + + view = app->get_active_file_view(cmd_context); + buffer = app->get_active_buffer(cmd_context); + + range = get_range(&view); + pos = range.min; + + app->buffer_replace_range(cmd_context, &buffer, pos, pos, text1, size1); + + push_parameter(app, cmd_context, par_range_start, pos); + push_parameter(app, cmd_context, par_range_end, pos); + exec_command(cmd_context, cmdid_auto_tab_range); + + app->refresh_file_view(cmd_context, &view); + range = get_range(&view); + pos = range.max; + + app->buffer_replace_range(cmd_context, &buffer, pos, pos, text2, size2); + + push_parameter(app, cmd_context, par_range_start, pos); + push_parameter(app, cmd_context, par_range_end, pos); + exec_command(cmd_context, cmdid_auto_tab_range); +} + +CUSTOM_COMMAND_SIG(backspace_word){ + File_View_Summary view; + Buffer_Summary buffer; + int pos2, pos1; + + view = app->get_active_file_view(cmd_context); + + pos2 = view.cursor.pos; + exec_command(cmd_context, cmdid_seek_alphanumeric_left); + app->refresh_file_view(cmd_context, &view); + pos1 = view.cursor.pos; + + buffer = app->get_buffer(cmd_context, view.file_id); + app->buffer_replace_range(cmd_context, &buffer, pos1, pos2, 0, 0); +} + +CUSTOM_COMMAND_SIG(switch_to_compilation){ + File_View_Summary view; + Buffer_Summary buffer; + + char name[] = "*compilation*"; + int name_size = sizeof(name)-1; + + // TODO(allen): This will only work for file views for now. Extend the API + // a bit to handle a general view type which can be manipulated at least enough + // to change the specific type of view and set files even when the view didn't + // contain a file. + view = app->get_active_file_view(cmd_context); + buffer = app->get_buffer_by_name(cmd_context, make_string(name, name_size)); + + app->view_set_file(cmd_context, &view, buffer.file_id); +} + +CUSTOM_COMMAND_SIG(move_up_10){ + File_View_Summary view; + float x, y; + + view = app->get_active_file_view(cmd_context); + x = view.preferred_x; + + if (view.unwrapped_lines){ + y = view.cursor.unwrapped_y; + } + else{ + y = view.cursor.wrapped_y; + } + + y -= 10*view.line_height; + + app->view_set_cursor(cmd_context, &view, seek_xy(x, y, 0, view.unwrapped_lines), 0); +} + +CUSTOM_COMMAND_SIG(move_down_10){ + File_View_Summary view; + float x, y; + + view = app->get_active_file_view(cmd_context); + x = view.preferred_x; + + if (view.unwrapped_lines){ + y = view.cursor.wrapped_y; + } + else{ + y = view.cursor.wrapped_y; + } + + y += 10*view.line_height; + + app->view_set_cursor(cmd_context, &view, seek_xy(x, y, 0, view.unwrapped_lines), 0); +} + +CUSTOM_COMMAND_SIG(switch_to_file_in_quotes){ + File_View_Summary view; + Buffer_Summary buffer; + char short_file_name[128]; + int pos, start, end, size; + + view = app->get_active_file_view(cmd_context); + if (view.exists){ + buffer = app->get_buffer(cmd_context, view.file_id); + if (buffer.ready){ + pos = view.cursor.pos; + app->buffer_seek_delimiter(cmd_context, &buffer, pos, '"', 1, &end); + app->buffer_seek_delimiter(cmd_context, &buffer, pos, '"', 0, &start); + + ++start; + + size = end - start; + if (size < sizeof(short_file_name)){ + char file_name_[256]; + String file_name = make_fixed_width_string(file_name_); + + app->buffer_read_range(cmd_context, &buffer, start, end, short_file_name); + + copy(&file_name, make_string(buffer.file_name, buffer.file_name_len)); + truncate_to_path_of_directory(&file_name); + append(&file_name, make_string(short_file_name, size)); + + buffer = app->get_buffer_by_name(cmd_context, file_name); + exec_command(cmd_context, cmdid_change_active_panel); + view = app->get_active_file_view(cmd_context); + if (buffer.exists){ + app->view_set_file(cmd_context, &view, buffer.file_id); + } + else{ + push_parameter(app, cmd_context, par_name, expand_str(file_name)); + exec_command(cmd_context, cmdid_interactive_open); + } + } + } + } +} + +CUSTOM_COMMAND_SIG(open_in_other){ + exec_command(cmd_context, cmdid_change_active_panel); + exec_command(cmd_context, cmdid_interactive_open); +} + +CUSTOM_COMMAND_SIG(open_my_files){ + // NOTE(allen|a3.1): The command cmdid_interactive_open can now open + // a file specified on the parameter stack. If the file does not exist + // cmdid_interactive_open behaves as usual. + push_parameter(app, cmd_context, par_name, literal("w:/4ed/data/test/basic.cpp")); + exec_command(cmd_context, cmdid_interactive_open); + + exec_command(cmd_context, cmdid_change_active_panel); + + char my_file[256]; + int my_file_len; + + my_file_len = sizeof("w:/4ed/data/test/basic.txt") - 1; + for (int i = 0; i < my_file_len; ++i){ + my_file[i] = ("w:/4ed/data/test/basic.txt")[i]; + } + + // NOTE(allen|a3.1): null terminators are not needed for strings. + push_parameter(app, cmd_context, par_name, my_file, my_file_len); + exec_command(cmd_context, cmdid_interactive_open); + + exec_command(cmd_context, cmdid_change_active_panel); +} + +CUSTOM_COMMAND_SIG(build_at_launch_location){ + // NOTE(allen|a3.3): An example of calling build by setting all + // parameters directly. This only works if build.bat can be called + // from the starting directory + push_parameter(app, cmd_context, par_cli_overlap_with_conflict, 1); + push_parameter(app, cmd_context, par_name, literal("*compilation*")); + push_parameter(app, cmd_context, par_cli_path, literal(".")); + push_parameter(app, cmd_context, par_cli_command, literal("build")); + exec_command(cmd_context, cmdid_build); +} + +CUSTOM_COMMAND_SIG(build_search){ + // NOTE(allen|a3.3): An example of traversing the filesystem through parent + // directories looking for a file, in this case a batch file to execute. + // + // + // Step 1: push_directory returns a String containing the current "hot" directory + // (whatever directory you most recently visited in the 4coder file browsing interface) + // + // Step 2: app->directory_has_file queries the file system to see if "build.bat" exists + // If it does exist several parameters are pushed: + // - par_cli_overlap_with_conflict: whether to launch this process if an existing process + // is already being used for output on the same buffer + // + // - par_name: the name of the buffer to fill with the output from the process + // + // - par_cli_path: sets the path from which the command is executed + // + // - par_cli_command: sets the actual command to be executed, this can be almost any command + // that you could execute through a command line interface + // + // + // To set par_cli_path: push_parameter makes a copy of the dir string on the stack + // because the string allocated by push_directory is going to change again + // To set par_cli_command: app->push_parameter does not make a copy of the dir because + // dir isn't going to change again. + // + // Step 3: If the batch file did not exist try to move to the parent directory using + // app->directory_cd. The cd function can also be used to navigate to subdirectories. + // It returns true if it can actually move in the specified direction, and false otherwise. + + int keep_going = 1; + String dir = push_directory(app, cmd_context); + while (keep_going){ + if (app->directory_has_file(dir, "build.bat")){ + push_parameter(app, cmd_context, par_cli_overlap_with_conflict, 0); + push_parameter(app, cmd_context, par_name, literal("*compilation*")); + push_parameter(app, cmd_context, par_cli_path, dir.str, dir.size); + + if (append(&dir, "build")){ +#if 1 + // NOTE(allen): This version avoids an unecessary copy, both equivalents are + // included to demonstrate how using push_parameter without the helper looks. + app->push_parameter(cmd_context, + dynamic_int(par_cli_command), + dynamic_string(dir.str, dir.size)); +#else + push_parameter(cmd_context, par_cli_command, dir.str, dir.size); +#endif + + exec_command(cmd_context, cmdid_build); + } + else{ + clear_parameters(cmd_context); + } + + return; + } + + if (app->directory_cd(&dir, "..") == 0){ + keep_going = 0; + } + } + + // TODO(allen): feedback message - couldn't find build.bat +} + +CUSTOM_COMMAND_SIG(write_and_auto_tab){ + exec_command(cmd_context, cmdid_write_character); + exec_command(cmd_context, cmdid_auto_tab_line_at_cursor); +} + +extern "C" GET_BINDING_DATA(get_bindings){ + Bind_Helper context_actual = begin_bind_helper(data, size); + Bind_Helper *context = &context_actual; + + // NOTE(allen|a3.1): Right now hooks have no loyalties to maps, all hooks are + // global and once set they always apply, regardless of what map is active. + set_hook(context, hook_start, my_start); + set_hook(context, hook_open_file, my_file_settings); + + begin_map(context, mapid_global); + + bind(context, 'p', MDFR_CTRL, cmdid_open_panel_vsplit); + bind(context, '_', MDFR_CTRL, cmdid_open_panel_hsplit); + bind(context, 'P', MDFR_CTRL, cmdid_close_panel); + bind(context, 'n', MDFR_CTRL, cmdid_interactive_new); + bind(context, 'o', MDFR_CTRL, cmdid_interactive_open); + bind(context, ',', MDFR_CTRL, cmdid_change_active_panel); + bind(context, 'k', MDFR_CTRL, cmdid_interactive_kill_buffer); + bind(context, 'i', MDFR_CTRL, cmdid_interactive_switch_buffer); + bind(context, 'c', MDFR_ALT, cmdid_open_color_tweaker); + bind(context, 'x', MDFR_ALT, cmdid_open_menu); + bind(context, 'o', MDFR_ALT, open_in_other); + + // NOTE(allen): These callbacks may not actually be useful to you, but + // go look at them and see what they do. + bind(context, 'M', MDFR_ALT | MDFR_CTRL, open_my_files); + bind(context, 'M', MDFR_ALT, build_at_launch_location); + bind(context, 'm', MDFR_ALT, build_search); + + end_map(context); + + + begin_map(context, my_code_map); + + // NOTE(allen|a3.1): Set this map (my_code_map == mapid_user_custom) to + // inherit from mapid_file. When searching if a key is bound + // in this map, if it is not found here it will then search mapid_file. + // + // If this is not set, it defaults to mapid_global. + inherit_map(context, mapid_file); + + // NOTE(allen|a3.1): Children can override parent's bindings. + bind(context, codes->right, MDFR_CTRL, cmdid_seek_alphanumeric_or_camel_right); + bind(context, codes->left, MDFR_CTRL, cmdid_seek_alphanumeric_or_camel_left); + + // NOTE(allen|a3.2): Specific keys can override vanilla keys, + // and write character writes whichever character corresponds + // to the key that triggered the command. + bind(context, '\n', 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, '#', MDFR_NONE, write_and_auto_tab); + + bind(context, '\t', MDFR_NONE, cmdid_word_complete); + bind(context, '\t', MDFR_CTRL, cmdid_auto_tab_range); + bind(context, '\t', MDFR_SHIFT, cmdid_auto_tab_line_at_cursor); + + bind(context, '\n', MDFR_SHIFT, write_and_auto_tab); + bind(context, ' ', MDFR_SHIFT, cmdid_write_character); + + bind(context, '=', MDFR_CTRL, write_increment); + bind(context, '-', MDFR_CTRL, write_decrement); + bind(context, '[', MDFR_CTRL, open_long_braces); + bind(context, 'i', MDFR_ALT, ifdef_off); + bind(context, '1', MDFR_ALT, switch_to_file_in_quotes); + + end_map(context); + + + begin_map(context, mapid_file); + + // NOTE(allen|a3.1): Binding this essentially binds all key combos that + // would normally insert a character into a buffer. + // Or apply this rule (which always works): if the code for the key + // is not in the codes struct, it is a vanilla key. + // It is possible to override this binding for individual keys. + bind_vanilla_keys(context, cmdid_write_character); + + bind(context, codes->left, MDFR_NONE, cmdid_move_left); + bind(context, codes->right, MDFR_NONE, cmdid_move_right); + bind(context, codes->del, MDFR_NONE, cmdid_delete); + bind(context, codes->back, MDFR_NONE, cmdid_backspace); + bind(context, codes->up, MDFR_NONE, cmdid_move_up); + bind(context, codes->down, MDFR_NONE, cmdid_move_down); + bind(context, codes->end, MDFR_NONE, cmdid_seek_end_of_line); + bind(context, codes->home, MDFR_NONE, cmdid_seek_beginning_of_line); + bind(context, codes->page_up, MDFR_NONE, cmdid_page_up); + bind(context, codes->page_down, MDFR_NONE, cmdid_page_down); + + bind(context, codes->right, MDFR_CTRL, cmdid_seek_whitespace_right); + bind(context, codes->left, MDFR_CTRL, cmdid_seek_whitespace_left); + bind(context, codes->up, MDFR_CTRL, cmdid_seek_whitespace_up); + bind(context, codes->down, MDFR_CTRL, cmdid_seek_whitespace_down); + + bind(context, codes->up, MDFR_ALT, move_up_10); + bind(context, codes->down, MDFR_ALT, move_down_10); + + bind(context, codes->back, MDFR_CTRL, backspace_word); + + bind(context, ' ', MDFR_CTRL, cmdid_set_mark); + bind(context, 'm', MDFR_CTRL, cmdid_cursor_mark_swap); + bind(context, 'c', MDFR_CTRL, cmdid_copy); + bind(context, 'x', MDFR_CTRL, cmdid_cut); + bind(context, 'v', MDFR_CTRL, cmdid_paste); + bind(context, 'V', MDFR_CTRL, cmdid_paste_next); + bind(context, 'Z', MDFR_CTRL, cmdid_timeline_scrub); + bind(context, 'z', MDFR_CTRL, cmdid_undo); + bind(context, 'y', MDFR_CTRL, cmdid_redo); + bind(context, codes->left, MDFR_ALT, cmdid_increase_rewind_speed); + bind(context, codes->right, MDFR_ALT, cmdid_increase_fastforward_speed); + bind(context, codes->down, MDFR_ALT, cmdid_stop_rewind_fastforward); + bind(context, 'h', MDFR_CTRL, cmdid_history_backward); + bind(context, 'H', MDFR_CTRL, cmdid_history_forward); + bind(context, 'd', MDFR_CTRL, cmdid_delete_range); + bind(context, 'l', MDFR_CTRL, cmdid_toggle_line_wrap); + bind(context, 'L', MDFR_CTRL, cmdid_toggle_endline_mode); + bind(context, 'u', MDFR_CTRL, cmdid_to_uppercase); + bind(context, 'j', MDFR_CTRL, cmdid_to_lowercase); + bind(context, '?', MDFR_CTRL, cmdid_toggle_show_whitespace); + + bind(context, '~', MDFR_CTRL, cmdid_clean_all_lines); + bind(context, '1', MDFR_CTRL, cmdid_eol_dosify); + bind(context, '!', MDFR_CTRL, cmdid_eol_nixify); + + bind(context, 'f', MDFR_CTRL, cmdid_search); + bind(context, 'r', MDFR_CTRL, cmdid_reverse_search); + bind(context, 'g', MDFR_CTRL, cmdid_goto_line); + + bind(context, 'K', MDFR_CTRL, cmdid_kill_buffer); + bind(context, 'O', MDFR_CTRL, cmdid_reopen); + bind(context, 'w', MDFR_CTRL, cmdid_interactive_save_as); + bind(context, 's', MDFR_CTRL, cmdid_save); + + bind(context, ',', MDFR_ALT, switch_to_compilation); + + end_map(context); + end_bind_helper(context); + + return context->write_total; +} + + diff --git a/4coder_helper.h b/4coder_helper.h index ef80ee72..992c0b2c 100644 --- a/4coder_helper.h +++ b/4coder_helper.h @@ -215,6 +215,21 @@ push_directory(Application_Links *app, void *cmd_context){ #define expand_string(d) ((d).str), ((d).size) +inline Range +get_range(File_View_Summary *view){ + Range range; + if (view->cursor.pos < view->mark.pos){ + range.min = view->cursor.pos; + range.max = view->mark.pos; + } + else{ + range.max = view->cursor.pos; + range.min = view->mark.pos; + } + + return(range); +} + #if DisableMacroTranslations == 0 inline void diff --git a/4coder_string.h b/4coder_string.h index eac3de03..28db3936 100644 --- a/4coder_string.h +++ b/4coder_string.h @@ -273,6 +273,9 @@ tailstr(String str){ #ifdef FCPP_STRING_IMPLEMENTATION +#ifndef FCPP_DID_STRING_IMPLEMENTATION +#define FCPP_DID_STRING_IMPLEMENTATION + FCPP_LINK int str_size(char *str){ int i = 0; @@ -1171,6 +1174,8 @@ wildcard_match(Absolutes *absolutes, String x, int case_sensitive){ return wildcard_match(absolutes, x.str, case_sensitive); } +#endif // #ifndef FCPP_DID_STRING_IMPLEMENTATION + #undef FCPP_STRING_IMPLEMENTATION #endif // #ifdef FCPP_STRING_IMPLEMENTATION diff --git a/4ed.cpp b/4ed.cpp index 05cabe30..0aa810d3 100644 --- a/4ed.cpp +++ b/4ed.cpp @@ -1086,13 +1086,6 @@ COMMAND_DECL(save){ USE_PANEL(panel); delayed_action(delay, DACT_SAVE, file->name.source_path, panel); -#if 0 - String *file_path = &file->name.source_path; - if (file_path->size > 0){ - i32 sys_id = file_save(system, exchange, mem, file, file_path->str); - app_push_file_binding(vars, sys_id, get_file_id(working_set, file)); - } -#endif } COMMAND_DECL(interactive_save_as){ @@ -2533,92 +2526,12 @@ setup_ui_commands(Command_Map *commands, Partition *part, Key_Codes *codes, Comm internal void setup_file_commands(Command_Map *commands, Partition *part, Key_Codes *codes, Command_Map *parent){ - map_init(commands, part, 101, parent); - - commands->vanilla_keyboard_default.function = command_write_character; - - map_add(commands, codes->left, MDFR_NONE, command_move_left); - map_add(commands, codes->right, MDFR_NONE, command_move_right); - map_add(commands, codes->del, MDFR_NONE, command_delete); - map_add(commands, codes->back, MDFR_NONE, command_backspace); - map_add(commands, codes->up, MDFR_NONE, command_move_up); - map_add(commands, codes->down, MDFR_NONE, command_move_down); - map_add(commands, codes->end, MDFR_NONE, command_seek_end_of_line); - map_add(commands, codes->home, MDFR_NONE, command_seek_beginning_of_line); - map_add(commands, codes->page_up, MDFR_NONE, command_page_up); - map_add(commands, codes->page_down, MDFR_NONE, command_page_down); - - map_add(commands, codes->right, MDFR_CTRL, command_seek_alphanumeric_or_camel_right); - map_add(commands, codes->left, MDFR_CTRL, command_seek_alphanumeric_or_camel_left); - map_add(commands, codes->up, MDFR_CTRL, command_seek_whitespace_up); - map_add(commands, codes->down, MDFR_CTRL, command_seek_whitespace_down); - - map_add(commands, ' ', MDFR_CTRL, command_set_mark); - map_add(commands, 'm', MDFR_CTRL, command_cursor_mark_swap); - map_add(commands, 'c', MDFR_CTRL, command_copy); - map_add(commands, 'x', MDFR_CTRL, command_cut); - map_add(commands, 'v', MDFR_CTRL, command_paste); - map_add(commands, 'V', MDFR_CTRL, command_paste_next); - map_add(commands, 'z', MDFR_CTRL, command_undo); - map_add(commands, 'y', MDFR_CTRL, command_redo); - map_add(commands, 'Z', MDFR_CTRL, command_timeline_scrub); - map_add(commands, codes->left, MDFR_ALT, command_increase_rewind_speed); - map_add(commands, codes->right, MDFR_ALT, command_increase_fastforward_speed); - map_add(commands, codes->down, MDFR_ALT, command_stop_rewind_fastforward); - map_add(commands, 'h', MDFR_CTRL, command_history_backward); - map_add(commands, 'H', MDFR_CTRL, command_history_forward); - map_add(commands, 'd', MDFR_CTRL, command_delete_range); - map_add(commands, 'l', MDFR_CTRL, command_toggle_line_wrap); - map_add(commands, '?', MDFR_CTRL, command_toggle_show_whitespace); - map_add(commands, '|', MDFR_CTRL, command_toggle_tokens); - map_add(commands, 'u', MDFR_CTRL, command_to_uppercase); - map_add(commands, 'j', MDFR_CTRL, command_to_lowercase); - map_add(commands, '~', MDFR_CTRL, command_clean_all_lines); - - map_add(commands, 'f', MDFR_CTRL, command_search); - map_add(commands, 'r', MDFR_CTRL, command_reverse_search); - map_add(commands, 'g', MDFR_CTRL, command_goto_line); - - map_add(commands, '\n', MDFR_NONE, compose_write_auto_tab_line); - map_add(commands, '}', MDFR_NONE, compose_write_auto_tab_line); - map_add(commands, ')', MDFR_NONE, compose_write_auto_tab_line); - map_add(commands, ']', MDFR_NONE, compose_write_auto_tab_line); - map_add(commands, ';', MDFR_NONE, compose_write_auto_tab_line); - map_add(commands, '#', MDFR_NONE, compose_write_auto_tab_line); - - map_add(commands, '\t', MDFR_NONE, command_word_complete); - map_add(commands, '\t', MDFR_CTRL, command_auto_tab_range); - map_add(commands, '\t', MDFR_SHIFT, command_auto_tab_line_at_cursor); - - map_add(commands, 'K', MDFR_CTRL, command_kill_buffer); - map_add(commands, 'O', MDFR_CTRL, command_reopen); - map_add(commands, 's', MDFR_CTRL, command_save); - map_add(commands, 'w', MDFR_CTRL, command_interactive_save_as); - -#if UseFileHistoryDump - map_add(commands, 'h', MDFR_ALT, command_save_history); -#endif + map_init(commands, part, 10, parent); } internal void setup_top_commands(Command_Map *commands, Partition *part, Key_Codes *codes, Command_Map *parent){ - map_init(commands, part, 51, parent); - -#if FRED_INTERNAL - map_add(commands, 'd', MDFR_ALT, command_open_debug_view); -#endif - - map_add(commands, 'p', MDFR_CTRL, command_open_panel_vsplit); - map_add(commands, '-', MDFR_CTRL, command_open_panel_hsplit); - map_add(commands, 'P', MDFR_CTRL, command_close_panel); - map_add(commands, 'n', MDFR_CTRL, command_interactive_new); - map_add(commands, 'o', MDFR_CTRL, command_interactive_open); - map_add(commands, ',', MDFR_CTRL, command_change_active_panel); - map_add(commands, 'k', MDFR_CTRL, command_interactive_kill_buffer); - map_add(commands, 'i', MDFR_CTRL, command_interactive_switch_buffer); - map_add(commands, 'c', MDFR_ALT, command_open_color_tweaker); - map_add(commands, 'x', MDFR_ALT, command_open_menu); - map_add(commands, 'm', MDFR_ALT, command_build_here); + map_init(commands, part, 5, parent); } internal void @@ -3091,9 +3004,16 @@ app_setup_memory(Application_Memory *memory){ internal i32 execute_special_tool(void *memory, i32 size, Command_Line_Parameters clparams){ - char message[] = "Hell World!"; - i32 result = sizeof(message) - 1; + i32 result; + char message[] = "tool was not specified or is invalid"; + result = sizeof(message) - 1; memcpy(memory, message, result); + if (clparams.argc > 2){ + if (match(clparams.argv[2], "version")){ + result = sizeof(VERSION) - 1; + memcpy(memory, VERSION, result); + } + } return(result); } @@ -3179,37 +3099,38 @@ App_Init_Sig(app_init){ setup_command_table(); Command_Map *global = &vars->map_top; - if (vars->config_api.get_bindings){ - i32 size = partition_remaining(partition); - void *data = partition_current(partition); - - // TODO(allen): Use a giant bubble of general memory for this. + Assert(vars->config_api.get_bindings != 0); + + i32 size = partition_remaining(partition); + void *data = partition_current(partition); + + // TODO(allen): Use a giant bubble of general memory for this. // So that it doesn't interfere with the command maps as they allocate // their own memory. - i32 wanted_size = vars->config_api.get_bindings(data, size, codes); - - b32 did_top = 0; - b32 did_file = 0; - if (wanted_size <= size){ - partition_allocate(partition, wanted_size); - - Binding_Unit *unit = (Binding_Unit*)data; - 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; - - vars->map_id_table = - push_array(&vars->mem.part, i32, user_map_count); - - vars->user_maps = - push_array(&vars->mem.part, Command_Map, user_map_count); + i32 wanted_size = vars->config_api.get_bindings(data, size, codes); - vars->user_map_count = user_map_count; - - Command_Map *mapptr = 0; - for (++unit; unit < end; ++unit){ - switch (unit->type){ + b32 did_top = 0; + b32 did_file = 0; + if (wanted_size <= size){ + partition_allocate(partition, wanted_size); + + Binding_Unit *unit = (Binding_Unit*)data; + 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; + + vars->map_id_table = + push_array(&vars->mem.part, i32, user_map_count); + + vars->user_maps = + push_array(&vars->mem.part, Command_Map, user_map_count); + + vars->user_map_count = user_map_count; + + Command_Map *mapptr = 0; + for (++unit; unit < end; ++unit){ + switch (unit->type){ case unit_map_begin: { int table_max = unit->map_begin.bind_count * 3 / 2; @@ -3232,53 +3153,53 @@ App_Init_Sig(app_init){ } else mapptr = 0; }break; - + case unit_inherit: - if (mapptr){ - Command_Map *parent = 0; - int mapid = unit->map_inherit.mapid; - if (mapid == mapid_global) parent = &vars->map_top; - else if (mapid == mapid_file) parent = &vars->map_file; - else if (mapid < mapid_global){ - i32 index = app_get_or_add_map_index(vars, mapid); - if (index < user_map_count) parent = vars->user_maps + index; - else parent = 0; - } - mapptr->parent = parent; - }break; - + if (mapptr){ + Command_Map *parent = 0; + int mapid = unit->map_inherit.mapid; + if (mapid == mapid_global) parent = &vars->map_top; + else if (mapid == mapid_file) parent = &vars->map_file; + else if (mapid < mapid_global){ + i32 index = app_get_or_add_map_index(vars, mapid); + if (index < user_map_count) parent = vars->user_maps + index; + else parent = 0; + } + mapptr->parent = parent; + }break; + case unit_binding: - if (mapptr){ - 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 && unit->binding.modifiers == 0){ - mapptr->vanilla_keyboard_default.function = func; - } - else{ - map_add(mapptr, unit->binding.code, unit->binding.modifiers, func); - } + if (mapptr){ + 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 && unit->binding.modifiers == 0){ + mapptr->vanilla_keyboard_default.function = func; + } + else{ + map_add(mapptr, unit->binding.code, unit->binding.modifiers, func); } } - break; - + } + break; + case unit_callback: - if (mapptr){ - Command_Function func = command_user_callback; - Custom_Command_Function *custom = unit->callback.func; - if (func){ - if (unit->callback.code == 0 && unit->callback.modifiers == 0){ - mapptr->vanilla_keyboard_default.function = func; - mapptr->vanilla_keyboard_default.custom = custom; - } - else{ - map_add(mapptr, unit->callback.code, unit->callback.modifiers, func, custom); - } + if (mapptr){ + Command_Function func = command_user_callback; + Custom_Command_Function *custom = unit->callback.func; + if (func){ + if (unit->callback.code == 0 && unit->callback.modifiers == 0){ + mapptr->vanilla_keyboard_default.function = func; + mapptr->vanilla_keyboard_default.custom = custom; + } + else{ + map_add(mapptr, unit->callback.code, unit->callback.modifiers, func, custom); } } - break; - + } + break; + case unit_hook: { int hook_id = unit->hook.hook_id; @@ -3286,19 +3207,18 @@ App_Init_Sig(app_init){ vars->hooks[hook_id] = unit->hook.func; } }break; - } } } } - - if (!did_top) setup_top_commands(&vars->map_top, &vars->mem.part, codes, global); - if (!did_file) setup_file_commands(&vars->map_file, &vars->mem.part, codes, global); - } - else{ - setup_top_commands(&vars->map_top, &vars->mem.part, codes, global); - setup_file_commands(&vars->map_file, &vars->mem.part, codes, global); } + if (!did_top) setup_top_commands(&vars->map_top, &vars->mem.part, codes, global); + if (!did_file) setup_file_commands(&vars->map_file, &vars->mem.part, codes, global); + +#if !defined(FRED_SUPER) + vars->hooks[hook_start] = 0; +#endif + setup_ui_commands(&vars->map_ui, &vars->mem.part, codes, global); #if FRED_INTERNAL setup_debug_commands(&vars->map_debug, &vars->mem.part, codes, global); diff --git a/4ed_app_target.cpp b/4ed_app_target.cpp index 1058a1e2..ffbb0810 100644 --- a/4ed_app_target.cpp +++ b/4ed_app_target.cpp @@ -9,6 +9,16 @@ // TOP +#define VERSION_NUMBER "alpha 3.4.3" + +#ifdef FRED_SUPER +#define VERSION_TYPE " super!" +#else +#define VERSION_TYPE "" +#endif + +#define VERSION VERSION_NUMBER VERSION_TYPE + #include "4ed_config.h" #define BUFFER_EXPERIMENT_SCALPEL 0 diff --git a/4ed_file_view.cpp b/4ed_file_view.cpp index 031e5afe..cd7a8968 100644 --- a/4ed_file_view.cpp +++ b/4ed_file_view.cpp @@ -9,10 +9,6 @@ // TOP -struct Range{ - i32 start, end; -}; - struct File_View_Mode{ b8 rewrite; }; diff --git a/win32_4ed.cpp b/win32_4ed.cpp index 3cd928c6..86229644 100644 --- a/win32_4ed.cpp +++ b/win32_4ed.cpp @@ -24,7 +24,12 @@ #include "4ed_dll_reader.h" -#include "4coder_custom.h" +#include "4coder_custom.cpp" + +#undef exec_command +#undef exec_command_keep_stack +#undef clear_parameters + #include "4ed_system.h" #include "4ed_rendering.h" #include "4ed.h" @@ -1601,6 +1606,10 @@ main(int argc, char **argv){ GetProcAddress(win32vars.custom, "get_bindings"); } #endif + + if (win32vars.custom_api.get_bindings == 0){ + win32vars.custom_api.get_bindings = (Get_Binding_Data_Function*)get_bindings; + } Thread_Context background[4]; memset(background, 0, sizeof(background));