API refinement

This commit is contained in:
Allen Webster 2016-02-24 19:04:08 -05:00
parent 353dcce12b
commit dca0269d8e
6 changed files with 647 additions and 439 deletions

View File

@ -63,6 +63,18 @@ seek_unwrapped_xy(float x, float y, int round_down){
return(result); return(result);
} }
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);
}
return(result);
}
static Buffer_Seek static Buffer_Seek
seek_line_char(int line, int character){ seek_line_char(int line, int character){
Buffer_Seek result; Buffer_Seek result;

View File

@ -1,432 +1,552 @@
/* /*
* Example use of customization API * Example use of customization API
*/ */
#define FCPP_STRING_IMPLEMENTATION #define FCPP_STRING_IMPLEMENTATION
#include "4coder_string.h" #include "4coder_string.h"
// NOTE(allen): See exec_command and surrounding code in 4coder_helper.h // NOTE(allen): See exec_command and surrounding code in 4coder_helper.h
// to decide whether you want macro translations, without them you will // to decide whether you want macro translations, without them you will
// have to manipulate the command and parameter stack through // have to manipulate the command and parameter stack through
// "app->" which may be more or less clear depending on your use. // "app->" which may be more or less clear depending on your use.
// //
// I suggest you try disabling macro translation and getting your code working // I suggest you try disabling macro translation and getting your code working
// that way, because I'll be removing it entirely sometime soon // that way, because I'll be removing it entirely sometime soon
#define DisableMacroTranslations 0 #define DisableMacroTranslations 0
#include "4coder_custom.h" #include "4coder_custom.h"
#include "4coder_helper.h" #include "4coder_helper.h"
#ifndef literal #ifndef literal
#define literal(s) s, (sizeof(s)-1) #define literal(s) s, (sizeof(s)-1)
#endif #endif
// NOTE(allen|a3.3): All of your custom ids should be enumerated // 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 // as shown here, they may start at 0, and you can only have
// 2^24 of them so don't be wasteful! // 2^24 of them so don't be wasteful!
enum My_Maps{ enum My_Maps{
my_code_map, my_code_map,
my_html_map my_html_map
}; };
HOOK_SIG(my_start){ HOOK_SIG(my_start){
exec_command(cmd_context, cmdid_open_panel_vsplit); exec_command(cmd_context, cmdid_open_panel_vsplit);
exec_command(cmd_context, cmdid_change_active_panel); exec_command(cmd_context, cmdid_change_active_panel);
} }
char *get_extension(const char *filename, int len, int *extension_len){ char *get_extension(const char *filename, int len, int *extension_len){
char *c = (char*)(filename + len - 1); char *c = (char*)(filename + len - 1);
char *end = c; char *end = c;
while (*c != '.' && c > filename) --c; while (*c != '.' && c > filename) --c;
*extension_len = (int)(end - c); *extension_len = (int)(end - c);
return c+1; return c+1;
} }
bool str_match(const char *a, int len_a, const char *b, int len_b){ bool str_match(const char *a, int len_a, const char *b, int len_b){
bool result = 0; bool result = 0;
if (len_a == len_b){ if (len_a == len_b){
char *end = (char*)(a + len_a); char *end = (char*)(a + len_a);
while (a < end && *a == *b){ while (a < end && *a == *b){
++a; ++b; ++a; ++b;
} }
if (a == end) result = 1; if (a == end) result = 1;
} }
return result; return result;
} }
HOOK_SIG(my_file_settings){ HOOK_SIG(my_file_settings){
Buffer_Summary buffer = app->get_active_buffer(cmd_context); 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 // 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 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 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 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 // -The name provided to get_buffer_by_name did not match any of the existing buffers
if (buffer.exists){ if (buffer.exists){
int treat_as_code = 0; int treat_as_code = 0;
if (buffer.file_name && buffer.size < (16 << 20)){ if (buffer.file_name && buffer.size < (16 << 20)){
int extension_len; int extension_len;
char *extension = get_extension(buffer.file_name, buffer.file_name_len, &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; 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("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("c"))) treat_as_code = 1;
else if (str_match(extension, extension_len, literal("hpp"))) 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_lex_as_cpp_file, treat_as_code);
push_parameter(app, cmd_context, par_wrap_lines, !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)); push_parameter(app, cmd_context, par_key_mapid, (treat_as_code)?(my_code_map):(mapid_file));
exec_command(cmd_context, cmdid_set_settings); exec_command(cmd_context, cmdid_set_settings);
} }
} }
CUSTOM_COMMAND_SIG(write_increment){ CUSTOM_COMMAND_SIG(write_increment){
Buffer_Summary buffer = app->get_active_buffer(cmd_context); 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 // NOTE(allen|a3.4.2): In addition to checking whether the buffer exists after a query,
// is usually ready, but when a buffer has just been opened it is possible that the contents // if you're going to read from or write to the buffer, you should check ready. A buffer
// haven't been filled yet. If the buffer is not ready trying to read or write it is invalid. // is usually ready, but when a buffer has just been opened it is possible that the contents
// (See my_file_settings for comments on the exists field). // haven't been filled yet. If the buffer is not ready trying to read or write it is invalid.
char text[] = "++"; // (See my_file_settings for comments on the exists field).
int size = sizeof(text) - 1; Buffer_Summary buffer = app->get_active_buffer(cmd_context);
if (buffer.exists && buffer.ready){ if (buffer.exists && buffer.ready){
app->buffer_replace_range(cmd_context, &buffer, buffer.file_cursor_pos, buffer.file_cursor_pos, text, size); app->buffer_replace_range(cmd_context, &buffer, buffer.file_cursor_pos, buffer.file_cursor_pos, text, size);
} }
} }
CUSTOM_COMMAND_SIG(write_decrement){ CUSTOM_COMMAND_SIG(write_decrement){
Buffer_Summary buffer = app->get_active_buffer(cmd_context); char text[] = "--";
char text[] = "--"; int size = sizeof(text) - 1;
int size = sizeof(text) - 1; Buffer_Summary buffer = app->get_active_buffer(cmd_context);
if (buffer.exists && buffer.ready){ if (buffer.exists && buffer.ready){
app->buffer_replace_range(cmd_context, &buffer, buffer.file_cursor_pos, buffer.file_cursor_pos, text, size); app->buffer_replace_range(cmd_context, &buffer, buffer.file_cursor_pos, buffer.file_cursor_pos, text, size);
} }
} }
CUSTOM_COMMAND_SIG(open_long_braces){ CUSTOM_COMMAND_SIG(open_long_braces){
File_View_Summary view; File_View_Summary view;
Buffer_Summary buffer; Buffer_Summary buffer;
view = app->get_active_file_view(cmd_context); view = app->get_active_file_view(cmd_context);
if (view.exists){ if (view.exists){
buffer = app->get_active_buffer(cmd_context); buffer = app->get_active_buffer(cmd_context);
char text[] = "{\n\n}"; char text[] = "{\n\n}";
int size = sizeof(text) - 1; int size = sizeof(text) - 1;
int pos; int pos;
if (buffer.exists && buffer.ready){ if (buffer.exists && buffer.ready){
pos = view.cursor.pos; pos = view.cursor.pos;
app->buffer_replace_range(cmd_context, &buffer, pos, pos, text, size); 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_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_range_start, pos);
push_parameter(app, cmd_context, par_clear_blank_lines, 0); push_parameter(app, cmd_context, par_range_end, pos + size);
exec_command(cmd_context, cmdid_auto_tab_range); 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; CUSTOM_COMMAND_SIG(ifdef_off){
Buffer_Summary buffer; File_View_Summary view;
Buffer_Summary buffer;
view = app->get_active_file_view(cmd_context);
if (view.exists){ view = app->get_active_file_view(cmd_context);
buffer = app->get_active_buffer(cmd_context); if (view.exists){
buffer = app->get_active_buffer(cmd_context);
char text1[] = "#if 0\n";
int size1 = sizeof(text1) - 1; char text1[] = "#if 0\n";
int size1 = sizeof(text1) - 1;
char text2[] = "#endif\n";
int size2 = sizeof(text2) - 1; char text2[] = "#endif\n";
int size2 = sizeof(text2) - 1;
int pos, c, m;
int pos, c, m;
if (buffer.exists && buffer.ready){
c = view.cursor.pos; if (buffer.exists && buffer.ready){
m = view.mark.pos; c = view.cursor.pos;
pos = (c<m)?(c):(m); m = view.mark.pos;
pos = (c<m)?(c):(m);
app->buffer_replace_range(cmd_context, &buffer, pos, pos, text1, size1);
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); push_parameter(app, cmd_context, par_range_start, pos);
exec_command(cmd_context, cmdid_auto_tab_range); 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; app->refresh_file_view(cmd_context, &view);
m = view.mark.pos; c = view.cursor.pos;
pos = (c>m)?(c):(m); m = view.mark.pos;
pos = (c>m)?(c):(m);
app->buffer_replace_range(cmd_context, &buffer, pos, pos, text2, size2);
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); push_parameter(app, cmd_context, par_range_start, pos);
exec_command(cmd_context, cmdid_auto_tab_range); push_parameter(app, cmd_context, par_range_end, pos);
} exec_command(cmd_context, cmdid_auto_tab_range);
} }
} }
}
CUSTOM_COMMAND_SIG(open_in_other){
exec_command(cmd_context, cmdid_change_active_panel); CUSTOM_COMMAND_SIG(backspace_word){
exec_command(cmd_context, cmdid_interactive_open); File_View_Summary view;
} Buffer_Summary buffer;
int pos2, pos1;
CUSTOM_COMMAND_SIG(open_my_files){
// NOTE(allen|a3.1): The command cmdid_interactive_open can now open view = app->get_active_file_view(cmd_context);
// a file specified on the parameter stack. If the file does not exist if (view.exists){
// cmdid_interactive_open behaves as usual. pos2 = view.cursor.pos;
push_parameter(app, cmd_context, par_name, literal("w:/4ed/data/test/basic.cpp")); exec_command(cmd_context, cmdid_seek_alphanumeric_left);
exec_command(cmd_context, cmdid_interactive_open); app->refresh_file_view(cmd_context, &view);
pos1 = view.cursor.pos;
exec_command(cmd_context, cmdid_change_active_panel);
if (pos1 < pos2){
char my_file[256]; buffer = app->get_buffer(cmd_context, view.file_id);
int my_file_len; app->buffer_replace_range(cmd_context, &buffer, pos1, pos2, 0, 0);
}
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];
} CUSTOM_COMMAND_SIG(switch_to_compilation){
File_View_Summary view;
// NOTE(allen|a3.1): null terminators are not needed for strings. Buffer_Summary buffer;
push_parameter(app, cmd_context, par_name, my_file, my_file_len);
exec_command(cmd_context, cmdid_interactive_open); char name[] = "*compilation*";
int name_size = sizeof(name)-1;
exec_command(cmd_context, cmdid_change_active_panel);
} // 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
CUSTOM_COMMAND_SIG(build_at_launch_location){ // to change the specific type of view and set files even when the view didn't
// NOTE(allen|a3.3): An example of calling build by setting all // contain a file.
// parameters directly. This only works if build.bat can be called view = app->get_active_file_view(cmd_context);
// from the starting directory if (view.exists){
push_parameter(app, cmd_context, par_cli_overlap_with_conflict, 1); buffer = app->get_buffer_by_name(cmd_context, make_string(name, name_size));
push_parameter(app, cmd_context, par_target_buffer_name, literal("*compilation*")); if (buffer.exists){
push_parameter(app, cmd_context, par_cli_path, literal(".")); app->view_set_file(cmd_context, &view, buffer.file_id);
push_parameter(app, cmd_context, par_cli_command, literal("build")); }
exec_command(cmd_context, cmdid_build); }
} }
CUSTOM_COMMAND_SIG(build_search){ CUSTOM_COMMAND_SIG(move_up_10){
// NOTE(allen|a3.3): An example of traversing the filesystem through parent File_View_Summary view;
// directories looking for a file, in this case a batch file to execute. float x, y;
//
// view = app->get_active_file_view(cmd_context);
// Step 1: push_directory returns a String containing the current "hot" directory if (view.exists){
// (whatever directory you most recently visited in the 4coder file browsing interface) x = view.preferred_x;
//
// Step 2: app->directory_has_file queries the file system to see if "build.bat" exists if (view.unwrapped_lines){
// If it does exist several parameters are pushed: y = view.cursor.unwrapped_y;
// - par_cli_overlap_with_conflict: whether to launch this process if an existing process }
// is already being used for output on the same buffer else{
// y = view.cursor.wrapped_y;
// - par_target_buffer_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 y -= 10*view.line_height;
//
// - par_cli_command: sets the actual command to be executed, this can be almost any command app->view_set_cursor(cmd_context, &view, seek_xy(x, y, 0, view.unwrapped_lines), 0);
// 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 CUSTOM_COMMAND_SIG(move_down_10){
// because the string allocated by push_directory is going to change again File_View_Summary view;
// To set par_cli_command: app->push_parameter does not make a copy of the dir because float x, y;
// dir isn't going to change again.
// view = app->get_active_file_view(cmd_context);
// Step 3: If the batch file did not exist try to move to the parent directory using if (view.exists){
// app->directory_cd. The cd function can also be used to navigate to subdirectories. x = view.preferred_x;
// It returns true if it can actually move in the specified direction, and false otherwise.
if (view.unwrapped_lines){
int keep_going = 1; y = view.cursor.wrapped_y;
String dir = push_directory(app, cmd_context); }
while (keep_going){ else{
if (app->directory_has_file(dir, "build.bat")){ y = view.cursor.wrapped_y;
push_parameter(app, cmd_context, par_cli_overlap_with_conflict, 0); }
push_parameter(app, cmd_context, par_target_buffer_name, literal("*compilation*"));
push_parameter(app, cmd_context, par_cli_path, dir.str, dir.size); y += 10*view.line_height;
if (append(&dir, "build")){ app->view_set_cursor(cmd_context, &view, seek_xy(x, y, 0, view.unwrapped_lines), 0);
#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, CUSTOM_COMMAND_SIG(switch_to_file_in_quotes){
dynamic_int(par_cli_command), File_View_Summary view;
dynamic_string(dir.str, dir.size)); Buffer_Summary buffer;
#else char short_file_name[128];
push_parameter(cmd_context, par_cli_command, dir.str, dir.size); int pos, start, end, size;
#endif
view = app->get_active_file_view(cmd_context);
exec_command(cmd_context, cmdid_build); if (view.exists){
} buffer = app->get_buffer(cmd_context, view.file_id);
else{ if (buffer.exists && buffer.ready){
clear_parameters(cmd_context); pos = view.cursor.pos;
} app->buffer_seek_delimiter(cmd_context, &buffer, pos, '"', 1, &end);
app->buffer_seek_delimiter(cmd_context, &buffer, pos, '"', 0, &start);
return;
} ++start;
if (app->directory_cd(&dir, "..") == 0){ size = end - start;
keep_going = 0; if (size < sizeof(short_file_name)){
} char file_name_[256];
} String file_name = make_fixed_width_string(file_name_);
// TODO(allen): feedback message - couldn't find build.bat app->buffer_read_range(cmd_context, &buffer, start, end, short_file_name);
}
copy(&file_name, make_string(buffer.file_name, buffer.file_name_len));
CUSTOM_COMMAND_SIG(write_and_auto_tab){ truncate_to_path_of_directory(&file_name);
exec_command(cmd_context, cmdid_write_character); append(&file_name, make_string(short_file_name, size));
exec_command(cmd_context, cmdid_auto_tab_line_at_cursor);
} buffer = app->get_buffer_by_name(cmd_context, file_name);
if (buffer.exists){
// NOTE(allen|a3.4.1): How one might go about writing things like cut_line app->view_set_file(cmd_context, &view, buffer.file_id);
// same idea works for cut word and other such composite commands. }
CUSTOM_COMMAND_SIG(cut_line){ else{
exec_command(cmd_context, cmdid_seek_beginning_of_line); push_parameter(app, cmd_context, par_name, expand_str(file_name));
exec_command(cmd_context, cmdid_set_mark); exec_command(cmd_context, cmdid_interactive_open);
exec_command(cmd_context, cmdid_seek_end_of_line); }
exec_command(cmd_context, cmdid_cut); }
exec_command(cmd_context, cmdid_delete); }
} }
}
extern "C" GET_BINDING_DATA(get_bindings){
Bind_Helper context_actual = begin_bind_helper(data, size); CUSTOM_COMMAND_SIG(open_in_other){
Bind_Helper *context = &context_actual; exec_command(cmd_context, cmdid_change_active_panel);
exec_command(cmd_context, cmdid_interactive_open);
// 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); CUSTOM_COMMAND_SIG(open_my_files){
set_hook(context, hook_open_file, my_file_settings); // 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
begin_map(context, mapid_global); // cmdid_interactive_open behaves as usual.
push_parameter(app, cmd_context, par_name, literal("w:/4ed/data/test/basic.cpp"));
bind(context, 'p', MDFR_CTRL, cmdid_open_panel_vsplit); exec_command(cmd_context, cmdid_interactive_open);
bind(context, '-', MDFR_CTRL, cmdid_open_panel_hsplit);
bind(context, 'P', MDFR_CTRL, cmdid_close_panel); exec_command(cmd_context, cmdid_change_active_panel);
bind(context, 'n', MDFR_CTRL, cmdid_interactive_new);
bind(context, 'o', MDFR_CTRL, cmdid_interactive_open); char my_file[256];
bind(context, ',', MDFR_CTRL, cmdid_change_active_panel); int my_file_len;
bind(context, 'k', MDFR_CTRL, cmdid_interactive_kill_buffer);
bind(context, 'i', MDFR_CTRL, cmdid_interactive_switch_buffer); my_file_len = sizeof("w:/4ed/data/test/basic.txt") - 1;
bind(context, 'c', MDFR_ALT, cmdid_open_color_tweaker); for (int i = 0; i < my_file_len; ++i){
bind(context, 'x', MDFR_ALT, cmdid_open_menu); my_file[i] = ("w:/4ed/data/test/basic.txt")[i];
bind(context, 'o', MDFR_ALT, open_in_other); }
// NOTE(allen): These callbacks may not actually be useful to you, but // NOTE(allen|a3.1): null terminators are not needed for strings.
// go look at them and see what they do. push_parameter(app, cmd_context, par_name, my_file, my_file_len);
bind(context, 'M', MDFR_ALT | MDFR_CTRL, open_my_files); exec_command(cmd_context, cmdid_interactive_open);
bind(context, 'M', MDFR_ALT, build_at_launch_location);
bind(context, 'm', MDFR_ALT, build_search); exec_command(cmd_context, cmdid_change_active_panel);
}
end_map(context);
CUSTOM_COMMAND_SIG(build_at_launch_location){
// NOTE(allen|a3.3): An example of calling build by setting all
begin_map(context, my_code_map); // parameters directly. This only works if build.bat can be called
// from the starting directory
// NOTE(allen|a3.1): Set this map (my_code_map == mapid_user_custom) to push_parameter(app, cmd_context, par_cli_overlap_with_conflict, 1);
// inherit from mapid_file. When searching if a key is bound push_parameter(app, cmd_context, par_name, literal("*compilation*"));
// in this map, if it is not found here it will then search mapid_file. push_parameter(app, cmd_context, par_cli_path, literal("."));
// push_parameter(app, cmd_context, par_cli_command, literal("build"));
// If this is not set, it defaults to mapid_global. exec_command(cmd_context, cmdid_build);
inherit_map(context, mapid_file); }
// NOTE(allen|a3.1): Children can override parent's bindings. CUSTOM_COMMAND_SIG(build_search){
bind(context, codes->right, MDFR_CTRL, cmdid_seek_alphanumeric_or_camel_right); // NOTE(allen|a3.3): An example of traversing the filesystem through parent
bind(context, codes->left, MDFR_CTRL, cmdid_seek_alphanumeric_or_camel_left); // directories looking for a file, in this case a batch file to execute.
//
// NOTE(allen|a3.2): Specific keys can override vanilla keys, //
// and write character writes whichever character corresponds // Step 1: push_directory returns a String containing the current "hot" directory
// to the key that triggered the command. // (whatever directory you most recently visited in the 4coder file browsing interface)
bind(context, '\n', MDFR_NONE, write_and_auto_tab); //
bind(context, '}', MDFR_NONE, write_and_auto_tab); // Step 2: app->directory_has_file queries the file system to see if "build.bat" exists
bind(context, ')', MDFR_NONE, write_and_auto_tab); // If it does exist several parameters are pushed:
bind(context, ']', MDFR_NONE, write_and_auto_tab); // - par_cli_overlap_with_conflict: whether to launch this process if an existing process
bind(context, ';', MDFR_NONE, write_and_auto_tab); // is already being used for output on the same buffer
bind(context, '#', MDFR_NONE, write_and_auto_tab); //
// - par_target_buffer_name: the name of the buffer to fill with the output from the process
bind(context, '\t', MDFR_NONE, cmdid_word_complete); //
bind(context, '\t', MDFR_CTRL, cmdid_auto_tab_range); // - par_cli_path: sets the path from which the command is executed
bind(context, '\t', MDFR_SHIFT, cmdid_auto_tab_line_at_cursor); //
// - par_cli_command: sets the actual command to be executed, this can be almost any command
bind(context, '\n', MDFR_SHIFT, write_and_auto_tab); // that you could execute through a command line interface
bind(context, ' ', MDFR_SHIFT, cmdid_write_character); //
//
bind(context, '=', MDFR_CTRL, write_increment); // To set par_cli_path: push_parameter makes a copy of the dir string on the stack
bind(context, '-', MDFR_CTRL, write_decrement); // because the string allocated by push_directory is going to change again
bind(context, '[', MDFR_CTRL, open_long_braces); // To set par_cli_command: app->push_parameter does not make a copy of the dir because
bind(context, 'i', MDFR_ALT, ifdef_off); // dir isn't going to change again.
//
end_map(context); // 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.
begin_map(context, mapid_file);
int keep_going = 1;
// NOTE(allen|a3.1): Binding this essentially binds all key combos that String dir = push_directory(app, cmd_context);
// would normally insert a character into a buffer. while (keep_going){
// Or apply this rule (which always works): if the code for the key if (app->directory_has_file(dir, "build.bat")){
// is not in the codes struct, it is a vanilla key. push_parameter(app, cmd_context, par_cli_overlap_with_conflict, 0);
// It is possible to override this binding for individual keys. push_parameter(app, cmd_context, par_target_buffer_name, literal("*compilation*"));
bind_vanilla_keys(context, cmdid_write_character); push_parameter(app, cmd_context, par_cli_path, dir.str, dir.size);
bind(context, codes->left, MDFR_NONE, cmdid_move_left); if (append(&dir, "build")){
bind(context, codes->right, MDFR_NONE, cmdid_move_right); #if 1
bind(context, codes->del, MDFR_NONE, cmdid_delete); // NOTE(allen): This version avoids an unecessary copy, both equivalents are
bind(context, codes->back, MDFR_NONE, cmdid_backspace); // included to demonstrate how using push_parameter without the helper looks.
bind(context, codes->up, MDFR_NONE, cmdid_move_up); app->push_parameter(cmd_context,
bind(context, codes->down, MDFR_NONE, cmdid_move_down); dynamic_int(par_cli_command),
bind(context, codes->end, MDFR_NONE, cmdid_seek_end_of_line); dynamic_string(dir.str, dir.size));
bind(context, codes->home, MDFR_NONE, cmdid_seek_beginning_of_line); #else
bind(context, codes->page_up, MDFR_NONE, cmdid_page_up); push_parameter(cmd_context, par_cli_command, dir.str, dir.size);
bind(context, codes->page_down, MDFR_NONE, cmdid_page_down); #endif
bind(context, codes->right, MDFR_CTRL, cmdid_seek_whitespace_right); exec_command(cmd_context, cmdid_build);
bind(context, codes->left, MDFR_CTRL, cmdid_seek_whitespace_left); }
bind(context, codes->up, MDFR_CTRL, cmdid_seek_whitespace_up); else{
bind(context, codes->down, MDFR_CTRL, cmdid_seek_whitespace_down); clear_parameters(cmd_context);
}
bind(context, ' ', MDFR_CTRL, cmdid_set_mark);
bind(context, 'm', MDFR_CTRL, cmdid_cursor_mark_swap); return;
bind(context, 'c', MDFR_CTRL, cmdid_copy); }
bind(context, 'x', MDFR_CTRL, cmdid_cut);
bind(context, 'v', MDFR_CTRL, cmdid_paste); if (app->directory_cd(&dir, "..") == 0){
bind(context, 'V', MDFR_CTRL, cmdid_paste_next); keep_going = 0;
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); // TODO(allen): feedback message - couldn't find build.bat
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); CUSTOM_COMMAND_SIG(write_and_auto_tab){
bind(context, 'H', MDFR_CTRL, cmdid_history_forward); exec_command(cmd_context, cmdid_write_character);
bind(context, 'd', MDFR_CTRL, cmdid_delete_range); exec_command(cmd_context, cmdid_auto_tab_line_at_cursor);
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); extern "C" GET_BINDING_DATA(get_bindings){
bind(context, 'j', MDFR_CTRL, cmdid_to_lowercase); Bind_Helper context_actual = begin_bind_helper(data, size);
bind(context, '?', MDFR_CTRL, cmdid_toggle_show_whitespace); Bind_Helper *context = &context_actual;
bind(context, '~', MDFR_CTRL, cmdid_clean_all_lines); // NOTE(allen|a3.1): Right now hooks have no loyalties to maps, all hooks are
bind(context, '1', MDFR_CTRL, cmdid_eol_dosify); // global and once set they always apply, regardless of what map is active.
bind(context, '!', MDFR_CTRL, cmdid_eol_nixify); set_hook(context, hook_start, my_start);
set_hook(context, hook_open_file, my_file_settings);
bind(context, 'f', MDFR_CTRL, cmdid_search);
bind(context, 'r', MDFR_CTRL, cmdid_reverse_search); begin_map(context, mapid_global);
bind(context, 'g', MDFR_CTRL, cmdid_goto_line);
bind(context, 'p', MDFR_CTRL, cmdid_open_panel_vsplit);
bind(context, 'K', MDFR_CTRL, cmdid_kill_buffer); bind(context, '_', MDFR_CTRL, cmdid_open_panel_hsplit);
bind(context, 'O', MDFR_CTRL, cmdid_reopen); bind(context, 'P', MDFR_CTRL, cmdid_close_panel);
bind(context, 'w', MDFR_CTRL, cmdid_interactive_save_as); bind(context, 'n', MDFR_CTRL, cmdid_interactive_new);
bind(context, 's', MDFR_CTRL, cmdid_save); bind(context, 'o', MDFR_CTRL, cmdid_interactive_open);
bind(context, ',', MDFR_CTRL, cmdid_change_active_panel);
end_map(context); bind(context, 'k', MDFR_CTRL, cmdid_interactive_kill_buffer);
end_bind_helper(context); bind(context, 'i', MDFR_CTRL, cmdid_interactive_switch_buffer);
bind(context, 'c', MDFR_ALT, cmdid_open_color_tweaker);
return context->write_total; 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;
}

View File

@ -132,7 +132,6 @@ enum Param_ID{
par_lex_as_cpp_file, par_lex_as_cpp_file,
par_wrap_lines, par_wrap_lines,
par_key_mapid, par_key_mapid,
par_target_buffer_name,
par_cli_path, par_cli_path,
par_cli_command, par_cli_command,
par_cli_overlap_with_conflict, par_cli_overlap_with_conflict,
@ -233,8 +232,8 @@ struct Buffer_Summary{
int file_name_len; int file_name_len;
int buffer_name_len; int buffer_name_len;
const char *file_name; char *file_name;
const char *buffer_name; char *buffer_name;
int file_cursor_pos; int file_cursor_pos;
int is_lexed; int is_lexed;
@ -248,6 +247,9 @@ struct File_View_Summary{
Full_Cursor cursor; Full_Cursor cursor;
Full_Cursor mark; Full_Cursor mark;
float preferred_x;
int line_height;
int unwrapped_lines;
}; };
#ifndef FRED_STRING_STRUCT #ifndef FRED_STRING_STRUCT
@ -289,7 +291,7 @@ extern "C"{
#define GET_BUFFER_BY_NAME(name) Buffer_Summary name(void *cmd_context, String filename) #define GET_BUFFER_BY_NAME(name) Buffer_Summary name(void *cmd_context, String filename)
#define REFRESH_BUFFER_SIG(name) int name(void *cmd_context, Buffer_Summary *buffer) #define REFRESH_BUFFER_SIG(name) int name(void *cmd_context, Buffer_Summary *buffer)
#define BUFFER_SEEK_DELIMITER_SIG(name) int name(void *cmd_context, Buffer_Summary *buffer, int start, char delim, int *out) #define BUFFER_SEEK_DELIMITER_SIG(name) int name(void *cmd_context, Buffer_Summary *buffer, int start, char delim, int seek_forward, int *out)
#define BUFFER_READ_RANGE_SIG(name) int name(void *cmd_context, Buffer_Summary *buffer, int start, int end, char *out) #define BUFFER_READ_RANGE_SIG(name) int name(void *cmd_context, Buffer_Summary *buffer, int start, int end, char *out)
#define BUFFER_REPLACE_RANGE_SIG(name) int name(void *cmd_context, Buffer_Summary *buffer, int start, int end, char *str, int len) #define BUFFER_REPLACE_RANGE_SIG(name) int name(void *cmd_context, Buffer_Summary *buffer, int start, int end, char *str, int len)

View File

@ -213,7 +213,7 @@ push_directory(Application_Links *app, void *cmd_context){
return(result); return(result);
} }
#define dir_string(d) ((d).str), ((d).size) #define expand_string(d) ((d).str), ((d).size)
#if DisableMacroTranslations == 0 #if DisableMacroTranslations == 0

59
4ed.cpp
View File

@ -629,7 +629,30 @@ COMMAND_DECL(copy){
USE_WORKING_SET(working_set); USE_WORKING_SET(working_set);
USE_MEM(mem); USE_MEM(mem);
// TODO(allen): deduplicate
int r_start = 0, r_end = 0;
int start_set = 0, end_set = 0;
Command_Parameter *end = param_stack_end(&command->part);
Command_Parameter *param = param_stack_first(&command->part, end);
for (; param < end; param = param_next(param, end)){
int p = dynamic_to_int(&param->param.param);
switch (p){
case par_range_start:
start_set = 1;
r_start = dynamic_to_int(&param->param.value);
break;
case par_range_end:
end_set = 1;
r_end = dynamic_to_int(&param->param.value);
break;
}
}
Range range = get_range(view->cursor.pos, view->mark); Range range = get_range(view->cursor.pos, view->mark);
if (start_set) range.start = r_start;
if (end_set) range.end = r_end;
if (range.start < range.end){ if (range.start < range.end){
clipboard_copy(system, &mem->general, working_set, range, file); clipboard_copy(system, &mem->general, working_set, range, file);
} }
@ -643,7 +666,30 @@ COMMAND_DECL(cut){
USE_LAYOUT(layout); USE_LAYOUT(layout);
USE_MEM(mem); USE_MEM(mem);
// TODO(allen): deduplicate
int r_start = 0, r_end = 0;
int start_set = 0, end_set = 0;
Command_Parameter *end = param_stack_end(&command->part);
Command_Parameter *param = param_stack_first(&command->part, end);
for (; param < end; param = param_next(param, end)){
int p = dynamic_to_int(&param->param.param);
switch (p){
case par_range_start:
start_set = 1;
r_start = dynamic_to_int(&param->param.value);
break;
case par_range_end:
end_set = 1;
r_end = dynamic_to_int(&param->param.value);
break;
}
}
Range range = get_range(view->cursor.pos, view->mark); Range range = get_range(view->cursor.pos, view->mark);
if (start_set) range.start = r_start;
if (end_set) range.end = r_end;
if (range.start < range.end){ if (range.start < range.end){
i32 next_cursor_pos = range.start; i32 next_cursor_pos = range.start;
@ -1273,6 +1319,7 @@ COMMAND_DECL(auto_tab_range){
int start_set = 0, end_set = 0; int start_set = 0, end_set = 0;
int clear_blank_lines = 1; int clear_blank_lines = 1;
// TODO(allen): deduplicate
Command_Parameter *end = param_stack_end(&command->part); Command_Parameter *end = param_stack_end(&command->part);
Command_Parameter *param = param_stack_first(&command->part, end); Command_Parameter *param = param_stack_first(&command->part, end);
for (; param < end; param = param_next(param, end)){ for (; param < end; param = param_next(param, end)){
@ -1911,7 +1958,7 @@ COMMAND_DECL(build){
for (; param < end; param = param_next(param, end)){ for (; param < end; param = param_next(param, end)){
int p = dynamic_to_int(&param->param.param); int p = dynamic_to_int(&param->param.param);
switch (p){ switch (p){
case par_target_buffer_name: case par_name:
{ {
if (buffer_name == 0){ if (buffer_name == 0){
char *new_buffer_name = dynamic_to_string(&param->param.value, &buffer_name_len); char *new_buffer_name = dynamic_to_string(&param->param.value, &buffer_name_len);
@ -2073,6 +2120,9 @@ fill_view_summary(File_View_Summary *view, File_View *file_view, Live_Views *liv
view->file_id = (int)(file_view->file - working_set->files); view->file_id = (int)(file_view->file - working_set->files);
view->mark = view_compute_cursor_from_pos(file_view, file_view->mark); view->mark = view_compute_cursor_from_pos(file_view, file_view->mark);
view->cursor = file_view->cursor; view->cursor = file_view->cursor;
view->preferred_x = file_view->preferred_x;
view->line_height = file_view->font_height;
view->unwrapped_lines = file_view->unwrapped_lines;
} }
} }
@ -2210,7 +2260,12 @@ extern "C"{
size = buffer_size(&file->state.buffer); size = buffer_size(&file->state.buffer);
if (start < size){ if (start < size){
result = 1; result = 1;
*out = buffer_seek_delimiter(&file->state.buffer, start, delim); if (seek_forward){
*out = buffer_seek_delimiter(&file->state.buffer, start, delim);
}
else{
*out = buffer_reverse_seek_delimiter(&file->state.buffer, start, delim);
}
if (*out < 0) *out = 0; if (*out < 0) *out = 0;
if (*out > size) *out = size; if (*out > size) *out = size;
} }

View File

@ -109,6 +109,25 @@ buffer_seek_delimiter(Buffer_Type *buffer, int pos, char delim){
return(pos); return(pos);
} }
internal_4tech int
buffer_reverse_seek_delimiter(Buffer_Type *buffer, int pos, char delim){
Buffer_Backify_Type loop;
char *data;
int end;
for(loop = buffer_backify_loop(buffer, pos, 0);
buffer_backify_good(&loop);
buffer_backify_next(&loop)){
end = loop.size + loop.absolute_pos;
data = loop.data - loop.absolute_pos;
for (; pos > 0; --pos){
if (data[pos] == delim) goto double_break;
}
}
double_break:
return(pos);
}
internal_4tech int internal_4tech int
buffer_seek_whitespace_down(Buffer_Type *buffer, int pos){ buffer_seek_whitespace_down(Buffer_Type *buffer, int pos){
Buffer_Stringify_Type loop; Buffer_Stringify_Type loop;