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

@ -82,24 +82,24 @@ HOOK_SIG(my_file_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, // 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 // 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 // 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. // 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). // (See my_file_settings for comments on the exists field).
char text[] = "++"; Buffer_Summary buffer = app->get_active_buffer(cmd_context);
int size = sizeof(text) - 1;
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);
} }
@ -121,6 +121,7 @@ CUSTOM_COMMAND_SIG(open_long_braces){
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_start, pos);
push_parameter(app, cmd_context, par_range_end, pos + size); push_parameter(app, cmd_context, par_range_end, pos + size);
@ -173,6 +174,127 @@ CUSTOM_COMMAND_SIG(ifdef_off){
} }
} }
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){ CUSTOM_COMMAND_SIG(open_in_other){
exec_command(cmd_context, cmdid_change_active_panel); exec_command(cmd_context, cmdid_change_active_panel);
exec_command(cmd_context, cmdid_interactive_open); exec_command(cmd_context, cmdid_interactive_open);
@ -207,7 +329,7 @@ CUSTOM_COMMAND_SIG(build_at_launch_location){
// parameters directly. This only works if build.bat can be called // parameters directly. This only works if build.bat can be called
// from the starting directory // from the starting directory
push_parameter(app, cmd_context, par_cli_overlap_with_conflict, 1); push_parameter(app, cmd_context, par_cli_overlap_with_conflict, 1);
push_parameter(app, cmd_context, par_target_buffer_name, literal("*compilation*")); 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_path, literal("."));
push_parameter(app, cmd_context, par_cli_command, literal("build")); push_parameter(app, cmd_context, par_cli_command, literal("build"));
exec_command(cmd_context, cmdid_build); exec_command(cmd_context, cmdid_build);
@ -284,16 +406,6 @@ CUSTOM_COMMAND_SIG(write_and_auto_tab){
exec_command(cmd_context, cmdid_auto_tab_line_at_cursor); exec_command(cmd_context, cmdid_auto_tab_line_at_cursor);
} }
// NOTE(allen|a3.4.1): How one might go about writing things like cut_line
// same idea works for cut word and other such composite commands.
CUSTOM_COMMAND_SIG(cut_line){
exec_command(cmd_context, cmdid_seek_beginning_of_line);
exec_command(cmd_context, cmdid_set_mark);
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){ extern "C" GET_BINDING_DATA(get_bindings){
Bind_Helper context_actual = begin_bind_helper(data, size); Bind_Helper context_actual = begin_bind_helper(data, size);
Bind_Helper *context = &context_actual; Bind_Helper *context = &context_actual;
@ -306,7 +418,7 @@ extern "C" GET_BINDING_DATA(get_bindings){
begin_map(context, mapid_global); begin_map(context, mapid_global);
bind(context, 'p', MDFR_CTRL, cmdid_open_panel_vsplit); bind(context, 'p', MDFR_CTRL, cmdid_open_panel_vsplit);
bind(context, '-', MDFR_CTRL, cmdid_open_panel_hsplit); bind(context, '_', MDFR_CTRL, cmdid_open_panel_hsplit);
bind(context, 'P', MDFR_CTRL, cmdid_close_panel); bind(context, 'P', MDFR_CTRL, cmdid_close_panel);
bind(context, 'n', MDFR_CTRL, cmdid_interactive_new); bind(context, 'n', MDFR_CTRL, cmdid_interactive_new);
bind(context, 'o', MDFR_CTRL, cmdid_interactive_open); bind(context, 'o', MDFR_CTRL, cmdid_interactive_open);
@ -360,6 +472,7 @@ extern "C" GET_BINDING_DATA(get_bindings){
bind(context, '-', MDFR_CTRL, write_decrement); bind(context, '-', MDFR_CTRL, write_decrement);
bind(context, '[', MDFR_CTRL, open_long_braces); bind(context, '[', MDFR_CTRL, open_long_braces);
bind(context, 'i', MDFR_ALT, ifdef_off); bind(context, 'i', MDFR_ALT, ifdef_off);
bind(context, '1', MDFR_ALT, switch_to_file_in_quotes);
end_map(context); end_map(context);
@ -389,6 +502,11 @@ extern "C" GET_BINDING_DATA(get_bindings){
bind(context, codes->up, MDFR_CTRL, cmdid_seek_whitespace_up); bind(context, codes->up, MDFR_CTRL, cmdid_seek_whitespace_up);
bind(context, codes->down, MDFR_CTRL, cmdid_seek_whitespace_down); 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, ' ', MDFR_CTRL, cmdid_set_mark);
bind(context, 'm', MDFR_CTRL, cmdid_cursor_mark_swap); bind(context, 'm', MDFR_CTRL, cmdid_cursor_mark_swap);
bind(context, 'c', MDFR_CTRL, cmdid_copy); bind(context, 'c', MDFR_CTRL, cmdid_copy);
@ -423,6 +541,8 @@ extern "C" GET_BINDING_DATA(get_bindings){
bind(context, 'w', MDFR_CTRL, cmdid_interactive_save_as); bind(context, 'w', MDFR_CTRL, cmdid_interactive_save_as);
bind(context, 's', MDFR_CTRL, cmdid_save); bind(context, 's', MDFR_CTRL, cmdid_save);
bind(context, ',', MDFR_ALT, switch_to_compilation);
end_map(context); end_map(context);
end_bind_helper(context); end_bind_helper(context);

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

57
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;
if (seek_forward){
*out = buffer_seek_delimiter(&file->state.buffer, start, delim); *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;