cleaned up replace functions, made it prettier

This commit is contained in:
Allen Webster 2016-02-27 18:33:30 -05:00
parent c8b901e299
commit 336ef34e89
4 changed files with 75 additions and 229 deletions

View File

@ -303,13 +303,14 @@ CUSTOM_COMMAND_SIG(switch_to_file_in_quotes){
CUSTOM_COMMAND_SIG(goto_line){ CUSTOM_COMMAND_SIG(goto_line){
int line_number; int line_number;
String input; Query_Bar bar;
char string_space[256]; char string_space[256];
input = make_fixed_width_string(string_space); bar.prompt = make_lit_string("Goto Line: ");
bar.string = make_fixed_width_string(string_space);
if (query_user_number(app, make_lit_string("Goto Line: "), &input)){ if (query_user_number(app, &bar)){
line_number = str_to_int(input); line_number = str_to_int(bar.string);
active_view_to_line(app, line_number); active_view_to_line(app, line_number);
} }
} }
@ -437,14 +438,22 @@ CUSTOM_COMMAND_SIG(reverse_search){
} }
CUSTOM_COMMAND_SIG(replace_in_range){ CUSTOM_COMMAND_SIG(replace_in_range){
Query_Bar replace;
char replace_space[1024]; char replace_space[1024];
String replace = make_fixed_width_string(replace_space); replace.prompt = make_lit_string("Replace: ");
replace.string = make_fixed_width_string(replace_space);
Query_Bar with;
char with_space[1024]; char with_space[1024];
String with = make_fixed_width_string(with_space);; with.prompt = make_lit_string("With: ");
with.string = make_fixed_width_string(with_space);
if (!query_user_string(app, make_lit_string("Replace: "), &replace)) return; if (!query_user_string(app, &replace)) return;
if (!query_user_string(app, make_lit_string("With: "), &with)) return; if (!query_user_string(app, &with)) return;
String r, w;
r = replace.string;
w = with.string;
Buffer_Summary buffer; Buffer_Summary buffer;
File_View_Summary view; File_View_Summary view;
@ -456,24 +465,32 @@ CUSTOM_COMMAND_SIG(replace_in_range){
int pos, new_pos; int pos, new_pos;
pos = range.min; pos = range.min;
app->buffer_seek_string(app, &buffer, pos, replace.str, replace.size, 1, &new_pos); app->buffer_seek_string(app, &buffer, pos, r.str, r.size, 1, &new_pos);
while (new_pos < range.end){ while (new_pos < range.end){
app->buffer_replace_range(app, &buffer, new_pos, new_pos + replace.size, with.str, with.size); app->buffer_replace_range(app, &buffer, new_pos, new_pos + r.size, w.str, w.size);
pos = new_pos + with.size; pos = new_pos + w.size;
app->buffer_seek_string(app, &buffer, pos, replace.str, replace.size, 1, &new_pos); app->buffer_seek_string(app, &buffer, pos, r.str, r.size, 1, &new_pos);
} }
} }
CUSTOM_COMMAND_SIG(query_replace){ CUSTOM_COMMAND_SIG(query_replace){
Query_Bar replace;
char replace_space[1024]; char replace_space[1024];
String replace = make_fixed_width_string(replace_space); replace.prompt = make_lit_string("Replace: ");
replace.string = make_fixed_width_string(replace_space);
Query_Bar with;
char with_space[1024]; char with_space[1024];
String with = make_fixed_width_string(with_space);; with.prompt = make_lit_string("With: ");
with.string = make_fixed_width_string(with_space);
if (!query_user_string(app, make_lit_string("Replace: "), &replace)) return; if (!query_user_string(app, &replace)) return;
if (!query_user_string(app, make_lit_string("With: "), &with)) return; if (!query_user_string(app, &with)) return;
String r, w;
r = replace.string;
w = with.string;
Query_Bar bar; Query_Bar bar;
Buffer_Summary buffer; Buffer_Summary buffer;
@ -483,29 +500,31 @@ CUSTOM_COMMAND_SIG(query_replace){
bar.prompt = make_lit_string("Replace? (y)es, (n)ext, (esc)\n"); bar.prompt = make_lit_string("Replace? (y)es, (n)ext, (esc)\n");
bar.string = {}; bar.string = {};
app->start_query_bar(app, &bar, 0);
view = app->get_active_file_view(app); view = app->get_active_file_view(app);
buffer = app->get_buffer(app, view.buffer_id); buffer = app->get_buffer(app, view.buffer_id);
pos = view.cursor.pos; pos = view.cursor.pos;
app->buffer_seek_string(app, &buffer, pos, replace.str, replace.size, 1, &new_pos); app->buffer_seek_string(app, &buffer, pos, r.str, r.size, 1, &new_pos);
User_Input in = {}; User_Input in = {};
while (new_pos < buffer.size){ while (new_pos < buffer.size){
Range match = make_range(new_pos, new_pos + replace.size); Range match = make_range(new_pos, new_pos + r.size);
app->view_set_highlight(app, &view, match.min, match.max, 1); app->view_set_highlight(app, &view, match.min, match.max, 1);
in = app->get_user_input(app, EventOnAnyKey, EventOnButton); in = app->get_user_input(app, EventOnAnyKey, EventOnButton);
if (in.abort || in.key.keycode == key_esc) break; if (in.abort || in.key.keycode == key_esc || !key_is_unmodified(&in.key)) break;
if (in.key.character == 'y' || in.key.character == 'Y' || in.key.character == '\n' || in.key.character == '\t'){ if (in.key.character == 'y' || in.key.character == 'Y' || in.key.character == '\n' || in.key.character == '\t'){
app->buffer_replace_range(app, &buffer, match.min, match.max, with.str, with.size); app->buffer_replace_range(app, &buffer, match.min, match.max, w.str, w.size);
pos = match.start + with.size; pos = match.start + w.size;
} }
else{ else{
pos = match.max; pos = match.max;
} }
app->buffer_seek_string(app, &buffer, pos, replace.str, replace.size, 1, &new_pos); app->buffer_seek_string(app, &buffer, pos, r.str, r.size, 1, &new_pos);
} }
app->view_set_highlight(app, &view, 0, 0, 0); app->view_set_highlight(app, &view, 0, 0, 0);

View File

@ -251,9 +251,8 @@ key_is_unmodified(Key_Event_Data *key){
} }
static int static int
query_user_general(Application_Links *app, String prompt, String *string, int force_number){ query_user_general(Application_Links *app, Query_Bar *bar, int force_number){
User_Input in; User_Input in;
Query_Bar bar;
int success = 1; int success = 1;
int good_character = 0; int good_character = 0;
@ -262,13 +261,7 @@ query_user_general(Application_Links *app, String prompt, String *string, int fo
// user, if this command starts intercepting input even though no prompt is shown. // user, if this command starts intercepting input even though no prompt is shown.
// This will only happen if you have a lot of bars open already or if the current view // This will only happen if you have a lot of bars open already or if the current view
// doesn't support query bars. // doesn't support query bars.
if (app->start_query_bar(app, &bar, 0) == 0) return 0; if (app->start_query_bar(app, bar, 0) == 0) return 0;
// NOTE(allen|a3.4.4): The application side is storing a pointer straight to your Query_Bar
// any change you make to it will be reflected in what the application renders. The application
// also makes sure that it destroys all query bars whenever a command exits.
bar.prompt = prompt;
bar.string = *string;
while (1){ while (1){
// NOTE(allen|a3.4.4): This call will block until the user does one of the input // NOTE(allen|a3.4.4): This call will block until the user does one of the input
@ -306,35 +299,25 @@ query_user_general(Application_Links *app, String prompt, String *string, int fo
break; break;
} }
else if (in.key.keycode == key_back){ else if (in.key.keycode == key_back){
--bar.string.size; --bar->string.size;
} }
else if (good_character){ else if (good_character){
append(&bar.string, in.key.character); append(&bar->string, in.key.character);
} }
} }
} }
// NOTE(allen|a3.4.4): It is not always necessary to end your query bars, because
// 4coder will clean them up when the command exits anyway, but because this is
// a local scope we should clean up the bar manually because the pointer the application
// stores to our Query_Bar is about to go bad.
app->end_query_bar(app, &bar, 0);
if (success){
*string = bar.string;
}
return(success); return(success);
} }
inline int inline int
query_user_string(Application_Links *app, String prompt, String *string){ query_user_string(Application_Links *app, Query_Bar *bar){
int success = query_user_general(app, prompt, string, 0); int success = query_user_general(app, bar, 0);
return(success); return(success);
} }
inline int inline int
query_user_number(Application_Links *app, String prompt, String *string){ query_user_number(Application_Links *app, Query_Bar *bar){
int success = query_user_general(app, prompt, string, 1); int success = query_user_general(app, bar, 1);
return(success); return(success);
} }

View File

@ -21,8 +21,6 @@ struct Incremental_Search{
enum File_View_Widget_Type{ enum File_View_Widget_Type{
FWIDG_NONE, FWIDG_NONE,
//FWIDG_SEARCH,
//FWIDG_GOTO_LINE,
FWIDG_TIMELINES, FWIDG_TIMELINES,
// never below this // never below this
FWIDG_TYPE_COUNT FWIDG_TYPE_COUNT
@ -1449,8 +1447,6 @@ view_widget_height(File_View *view, i32 font_height){
} }
} }
break; break;
//case FWIDG_SEARCH: result = font_height + 2; break;
//case FWIDG_GOTO_LINE: result = font_height + 2; break;
case FWIDG_TIMELINES: result = view->widget.height; break; case FWIDG_TIMELINES: result = view->widget.height; break;
} }
return result; return result;
@ -3227,29 +3223,6 @@ draw_file_loaded(File_View *view, i32_Rect rect, b32 is_active, Render_Target *t
do_button(3, &state, &layout, "+ History", 1); do_button(3, &state, &layout, "+ History", 1);
} }
}break; }break;
#if 0
case FWIDG_SEARCH:
{
Widget_ID wid = make_id(&state, 1);
persist String search_str = make_lit_string("I-Search: ");
persist String rsearch_str = make_lit_string("Reverse-I-Search: ");
if (view->isearch.reverse){
do_text_field(wid, &state, &layout, rsearch_str, view->isearch.str);
}
else{
do_text_field(wid, &state, &layout, search_str, view->isearch.str);
}
}break;
case FWIDG_GOTO_LINE:
{
Widget_ID wid = make_id(&state, 1);
persist String gotoline_str = make_lit_string("Goto Line: ");
do_text_field(wid, &state, &layout, gotoline_str, view->isearch.str);
}break;
#endif
} }
ui_finish_frame(&view->widget.state, &state, &layout, widg_rect, 0, 0); ui_finish_frame(&view->widget.state, &state, &layout, widg_rect, 0, 0);
@ -3318,134 +3291,6 @@ command_search(System_Functions*,Command_Data*,Command_Binding);
internal void internal void
command_reverse_search(System_Functions*,Command_Data*,Command_Binding); command_reverse_search(System_Functions*,Command_Data*,Command_Binding);
#if 0
internal
HANDLE_COMMAND_SIG(handle_command_file_view){
File_View *file_view = (File_View*)(view);
Editing_File *file = file_view->file;
AllowLocal(file);
switch (file_view->widget.type){
case FWIDG_NONE:
case FWIDG_TIMELINES:
{
file_view->next_mode = {};
if (binding.function) binding.function(system, command, binding);
file_view->mode = file_view->next_mode;
if (key.keycode == key_esc)
view_set_widget(file_view, FWIDG_NONE);
}break;
case FWIDG_SEARCH:
{
String *string = &file_view->isearch.str;
Single_Line_Input_Step result =
app_single_line_input_step(system, key, string);
if (result.made_a_change ||
binding.function == command_search ||
binding.function == command_reverse_search){
b32 step_forward = 0;
b32 step_backward = 0;
if (binding.function == command_search) step_forward = 1;
if (binding.function == command_reverse_search) step_backward = 1;
i32 start_pos = file_view->isearch.pos;
if (step_forward){
if (file_view->isearch.reverse){
start_pos = file_view->temp_highlight.pos + 1;
file_view->isearch.pos = start_pos;
file_view->isearch.reverse = 0;
step_forward = 0;
}
}
if (step_backward){
if (!file_view->isearch.reverse){
start_pos = file_view->temp_highlight.pos - 1;
file_view->isearch.pos = start_pos;
file_view->isearch.reverse = 1;
step_backward = 0;
}
}
Temp_Memory temp = begin_temp_memory(&view->mem->part);
char *spare = push_array(&view->mem->part, char, string->size);
i32 size = buffer_size(&file->state.buffer);
i32 pos;
if (!result.hit_backspace){
if (file_view->isearch.reverse){
pos = buffer_rfind_string(&file->state.buffer, start_pos - 1,
string->str, string->size, spare);
if (pos >= 0){
if (step_backward){
file_view->isearch.pos = pos;
start_pos = pos;
pos = buffer_rfind_string(&file->state.buffer, start_pos - 1,
string->str, string->size, spare);
if (pos == -1) pos = start_pos;
}
view_set_temp_highlight(file_view, pos, pos+string->size);
}
}
else{
pos = buffer_find_string(&file->state.buffer, start_pos + 1, size,
string->str, string->size, spare);
if (pos < size){
if (step_forward){
file_view->isearch.pos = pos;
start_pos = pos;
pos = buffer_find_string(&file->state.buffer, start_pos + 1, size,
string->str, string->size, spare);
if (pos == size) pos = start_pos;
}
view_set_temp_highlight(file_view, pos, pos+string->size);
}
}
}
end_temp_memory(temp);
}
if (result.hit_newline || result.hit_ctrl_newline){
view_cursor_move(file_view, file_view->temp_highlight);
view_set_widget(file_view, FWIDG_NONE);
}
if (result.hit_esc){
file_view->show_temp_highlight = 0;
view_set_widget(file_view, FWIDG_NONE);
}
}break;
case FWIDG_GOTO_LINE:
{
String *string = &file_view->gotoline.str;
Single_Line_Input_Step result =
app_single_number_input_step(system, key, string);
if (result.hit_newline || result.hit_ctrl_newline){
i32 line_number = str_to_int(*string);
if (line_number < 1) line_number = 1;
file_view->cursor =
view_compute_cursor_from_unwrapped_xy(file_view, 0,
(f32)(line_number-1)*file_view->font_height);
file_view->preferred_x = view_get_cursor_x(file_view);
view_set_widget(file_view, FWIDG_NONE);
}
if (result.hit_esc){
view_set_widget(file_view, FWIDG_NONE);
}
}break;
}
}
#endif
inline void inline void
free_file_view(View *view){ free_file_view(View *view){
File_View *fview = (File_View*)view; File_View *fview = (File_View*)view;

View File

@ -787,8 +787,7 @@ ui_do_slider_input(UI_State *state, i32_Rect rect, Widget_ID wid,
} }
internal bool32 internal bool32
do_text_field(Widget_ID wid, UI_State *state, UI_Layout *layout, do_text_field(Widget_ID wid, UI_State *state, UI_Layout *layout, String prompt, String dest){
String prompt, String dest){
b32 result = 0; b32 result = 0;
i32 character_h = get_font_info(state->font_set, state->font_id)->height; i32 character_h = get_font_info(state->font_set, state->font_id)->height;
@ -810,9 +809,9 @@ do_text_field(Widget_ID wid, UI_State *state, UI_Layout *layout,
get_pop_color(state, &prompt_pop, wid, ui_style); get_pop_color(state, &prompt_pop, wid, ui_style);
draw_rectangle(target, rect, back); draw_rectangle(target, rect, back);
i32 x = rect.x0;
x = draw_string(target, state->font_id, prompt, x, rect.y0, prompt_pop); i32 x = draw_string(target, state->font_id, prompt, rect.x0, rect.y0 + 1, prompt_pop);
draw_string(target, state->font_id, dest, x, rect.y0, ui_style.base); draw_string(target, state->font_id, dest, x, rect.y0 + 1, ui_style.base);
} }
return result; return result;