diff --git a/4ed.cpp b/4ed.cpp index 5f88d264..fb00c2f0 100644 --- a/4ed.cpp +++ b/4ed.cpp @@ -213,37 +213,12 @@ COMMAND_DECL(write_character){ view->file->cursor_pos = view->cursor.pos; } -internal i32 -seek_whitespace_right(u8 *data, i32 size, i32 pos){ - while (pos < size && char_is_whitespace(data[pos])){ - ++pos; - } - while (pos < size && !char_is_whitespace(data[pos])){ - ++pos; - } - return pos; -} - -internal i32 -seek_whitespace_left(u8 *data, i32 pos){ - --pos; - while (pos > 0 && char_is_whitespace(data[pos])){ - --pos; - } - while (pos >= 0 && !char_is_whitespace(data[pos])){ - --pos; - } - ++pos; - return pos; -} - COMMAND_DECL(seek_whitespace_right){ ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); - i32 pos = seek_whitespace_right( - (u8*)file->buffer.data, file->buffer.size, view->cursor.pos); + i32 pos = buffer_seek_whitespace_right(&file->buffer, view->cursor.pos); view_cursor_move(view, pos); } @@ -253,14 +228,13 @@ COMMAND_DECL(seek_whitespace_left){ REQ_FILE_VIEW(view); REQ_FILE(file, view); - i32 pos = seek_whitespace_left( - (u8*)file->buffer.data, view->cursor.pos); + i32 pos = buffer_seek_whitespace_left(&file->buffer, view->cursor.pos); view_cursor_move(view, pos); } -// TODO(allen): see if this becomes better by using buffer procudures directly COMMAND_DECL(seek_whitespace_up){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -286,9 +260,11 @@ COMMAND_DECL(seek_whitespace_up){ if (pos != 0) ++pos; view_cursor_move(view, pos); +#endif } COMMAND_DECL(seek_whitespace_down){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -320,6 +296,7 @@ COMMAND_DECL(seek_whitespace_down){ else pos = prev_endline+1; view_cursor_move(view, pos); +#endif } internal i32 @@ -374,6 +351,7 @@ COMMAND_DECL(seek_token_right){ } COMMAND_DECL(seek_white_or_token_right){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -383,11 +361,13 @@ COMMAND_DECL(seek_white_or_token_right){ if (file->tokens_complete){ token_pos = seek_token_right(&file->token_stack, view->cursor.pos); } - white_pos = seek_whitespace_right((u8*)file->buffer.data, file->buffer.size, view->cursor.pos); + white_pos = buffer_seek_whitespace_right(&file->buffer, view->cursor.pos); view_cursor_move(view, Min(token_pos, white_pos)); +#endif } COMMAND_DECL(seek_white_or_token_left){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -397,8 +377,9 @@ COMMAND_DECL(seek_white_or_token_left){ if (file->tokens_complete){ token_pos = seek_token_left(&file->token_stack, view->cursor.pos); } - white_pos = seek_whitespace_left((u8*)file->buffer.data, view->cursor.pos); + white_pos = buffer_seek_whitespace_left(&file->buffer, view->cursor.pos); view_cursor_move(view, Max(token_pos, white_pos)); +#endif } internal i32 @@ -426,6 +407,7 @@ seek_alphanumeric_left(u8 *data, i32 pos){ } COMMAND_DECL(seek_alphanumeric_right){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -433,6 +415,7 @@ COMMAND_DECL(seek_alphanumeric_right){ i32 pos = seek_alphanumeric_right( (u8*)file->buffer.data, file->buffer.size, view->cursor.pos); view_cursor_move(view, pos); +#endif } COMMAND_DECL(seek_alphanumeric_left){ @@ -446,6 +429,7 @@ COMMAND_DECL(seek_alphanumeric_left){ } COMMAND_DECL(seek_alphanumeric_or_camel_right){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -467,6 +451,7 @@ COMMAND_DECL(seek_alphanumeric_or_camel_right){ } view_cursor_move(view, camel_pos); +#endif } COMMAND_DECL(seek_alphanumeric_or_camel_left){ @@ -552,6 +537,7 @@ COMMAND_DECL(copy){ } COMMAND_DECL(cut){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -571,6 +557,7 @@ COMMAND_DECL(cut){ view_measure_wraps(&mem->general, view); view_cursor_move(view, next_cursor_pos); } +#endif } COMMAND_DECL(paste){ @@ -647,6 +634,7 @@ COMMAND_DECL(paste_next){ } COMMAND_DECL(delete_chunk){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -661,6 +649,7 @@ COMMAND_DECL(delete_chunk){ view_cursor_move(view, next_cursor_pos); view->mark = range.start; } +#endif } COMMAND_DECL(timeline_scrub){ @@ -811,9 +800,11 @@ app_open_file(App_Vars *vars, General_Memory *general, Panel *panel, command_data->view = old_view; new_view->map = app_get_map(vars, target_file->base_map_id); - + +#if BUFFER_EXPERIMENT_SCALPEL if (created_file && target_file->tokens_exist) file_first_lex_parallel(general, target_file); +#endif } return result; @@ -886,8 +877,10 @@ COMMAND_DECL(reopen){ *file = temp_file; file->source_path.str = file->source_path_; file->live_name.str = file->live_name_; +#if BUFFER_EXPERIMENT_SCALPEL if (file->tokens_exist) file_first_lex_parallel(&mem->general, file); +#endif Partition old_part = command->part; Temp_Memory temp = begin_temp_memory(&vars->mem.part); @@ -1031,17 +1024,6 @@ COMMAND_DECL(toggle_line_wrap){ view_set_relative_scrolling(view, scrolling); } -COMMAND_DECL(toggle_endline_mode){ - ProfileMomentFunction(); - REQ_FILE_VIEW(view); - REQ_FILE(file, view); - USE_MEM(mem); - - file_measure_starts(&mem->general, view->file); - view->cursor = - view_compute_cursor_from_pos(view, view->cursor.pos); -} - COMMAND_DECL(toggle_show_whitespace){ ProfileMomentFunction(); REQ_FILE_VIEW(view); @@ -1049,6 +1031,7 @@ COMMAND_DECL(toggle_show_whitespace){ } COMMAND_DECL(toggle_tokens){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -1060,6 +1043,7 @@ COMMAND_DECL(toggle_tokens){ else{ file_first_lex_parallel(&mem->general, file); } +#endif } internal void @@ -1084,9 +1068,11 @@ case_change_range(Mem_Options *mem, File_View *view, Editing_File *file, data[i] += char_delta; } } - + +#if BUFFER_EXPERIMENT_SCALPEL if (file->token_stack.tokens) file_relex_parallel(mem, file, range.start, range.end, 0); +#endif } } @@ -1280,9 +1266,7 @@ COMMAND_DECL(move_left){ REQ_FILE(file, view); i32 pos = view->cursor.pos; - if (pos > 0){ - --pos; - } + if (pos > 0) --pos; view_cursor_move(view, pos); } @@ -1292,16 +1276,15 @@ COMMAND_DECL(move_right){ REQ_FILE_VIEW(view); REQ_FILE(file, view); - i32 size = file->buffer.size; + i32 size = buffer_size(&file->buffer); i32 pos = view->cursor.pos; - if (pos < size){ - ++pos; - } + if (pos < size) ++pos; view_cursor_move(view, pos); } COMMAND_DECL(delete){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -1322,9 +1305,11 @@ COMMAND_DECL(delete){ view_cursor_move(view, next_cursor_pos); if (view->mark >= end) view->mark -= shift; } +#endif } COMMAND_DECL(backspace){ +#if BUFFER_EXPERIMENT_SCALPEL ProfileMomentFunction(); REQ_FILE_VIEW(view); REQ_FILE(file, view); @@ -1346,6 +1331,7 @@ COMMAND_DECL(backspace){ view_cursor_move(view, next_cursor_pos); if (view->mark >= end) view->mark -= shift; } +#endif } COMMAND_DECL(move_up){ @@ -1536,6 +1522,7 @@ COMMAND_DECL(set_settings){ REQ_FILE(file, view); USE_VARS(vars); USE_MEM(mem); + AllowLocal(mem); Command_Parameter *end = param_stack_end(&command->part); Command_Parameter *param = param_stack_first(&command->part, end); @@ -1544,6 +1531,7 @@ COMMAND_DECL(set_settings){ switch (p){ case par_lex_as_cpp_file: { +#if BUFFER_EXPERIMENT_SCALPEL int v = dynamic_to_bool(¶m->param.value); if (file->tokens_exist){ if (!v) file_kill_tokens(&mem->general, file); @@ -1551,6 +1539,7 @@ COMMAND_DECL(set_settings){ else{ if (v) file_first_lex_parallel(&mem->general, file); } +#endif }break; case par_wrap_lines: @@ -1658,6 +1647,7 @@ extern "C"{ if (view){ Editing_File *file = view->file; if (file && !file->is_dummy){ +#if BUFFER_EXPERIMENT_SCALPEL Working_Set *working_set = cmd->working_set; buffer.file_id = (int)(file - working_set->files); buffer.size = file->buffer.size; @@ -1669,6 +1659,7 @@ extern "C"{ buffer.file_cursor_pos = file->cursor_pos; buffer.is_lexed = file->tokens_exist; buffer.map_id = file->base_map_id; +#endif } } @@ -1727,8 +1718,8 @@ setup_file_commands(Command_Map *commands, Partition *part, Key_Codes *codes, Co 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_white_or_token_right); - map_add(commands, codes->left, MDFR_CTRL, command_seek_white_or_token_left); + map_add(commands, codes->right, MDFR_CTRL, command_seek_whitespace_right); + map_add(commands, codes->left, MDFR_CTRL, command_seek_whitespace_left); map_add(commands, codes->up, MDFR_CTRL, command_seek_whitespace_up); map_add(commands, codes->down, MDFR_CTRL, command_seek_whitespace_down); @@ -1748,7 +1739,6 @@ setup_file_commands(Command_Map *commands, Partition *part, Key_Codes *codes, Co map_add(commands, 'H', MDFR_CTRL, command_history_forward); map_add(commands, 'd', MDFR_CTRL, command_delete_chunk); map_add(commands, 'l', MDFR_CTRL, command_toggle_line_wrap); - map_add(commands, 'L', MDFR_CTRL, command_toggle_endline_mode); 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); @@ -1850,7 +1840,6 @@ setup_command_table(){ SET(interactive_kill_buffer); SET(kill_buffer); SET(toggle_line_wrap); - SET(toggle_endline_mode); SET(to_uppercase); SET(to_lowercase); SET(toggle_show_whitespace); @@ -2603,7 +2592,9 @@ app_step(Thread_Context *thread, Key_Codes *codes, if (!view_->is_active) continue; File_View *view = view_to_file_view(view_); if (!view) continue; +#if BUFFER_EXPERIMENT_SCALPEL view_measure_wraps(&vars->mem.general, view); +#endif view->cursor = view_compute_cursor_from_pos(view, view->cursor.pos); } app_result.redraw = 1; @@ -3020,7 +3011,9 @@ app_step(Thread_Context *thread, Key_Codes *codes, view_set_file(file_view, file.file, style, vars->hooks[hook_open_file], &command_data, app_links); new_view->map = app_get_map(vars, file.file->base_map_id); +#if BUFFER_EXPERIMENT_SCALPEL if (file.file->tokens_exist) file_first_lex_parallel(general, file.file); +#endif }break; case DACT_SWITCH: @@ -3131,12 +3124,14 @@ app_step(Thread_Context *thread, Key_Codes *codes, if (vars->style.font_changed){ vars->style.font_changed = 0; +#if BUFFER_EXPERIMENT_SCALPEL Editing_File *file = vars->working_set.files; for (i32 i = vars->working_set.file_index_count; i > 0; --i, ++file){ if (file->buffer.data && !file->is_dummy){ file_measure_widths(&vars->mem.general, file, vars->style.font); } } +#endif Panel *panel = panels; for (i32 panel_i = vars->layout.panel_count; panel_i > 0; --panel_i, ++panel){ diff --git a/4ed_file_view.cpp b/4ed_file_view.cpp index fc3f72b3..adfcbba0 100644 --- a/4ed_file_view.cpp +++ b/4ed_file_view.cpp @@ -9,7 +9,9 @@ // TOP -#include "buffer/4coder_golden_array.cpp" +#include "buffer/4coder_shared.cpp" +#include "buffer/4coder_gap_buffer.cpp" +#include "buffer/4coder_buffer_abstract.cpp" struct Range{ i32 start, end; @@ -70,7 +72,7 @@ struct Undo_Data{ }; struct Editing_File{ - Buffer buffer; + Gap_Buffer buffer; Undo_Data undo; @@ -1008,7 +1010,7 @@ struct File_View_Widget{ struct File_View{ View view_base; - + Delay *delay; Editing_Layout *layout; @@ -1108,19 +1110,17 @@ internal bool32 file_save(Partition *part, Editing_File *file, u8 *filename){ bool32 result = 0; Temp_Memory temp = begin_temp_memory(part); - Buffer temp_buffer; - temp_buffer.max = partition_remaining(part); - temp_buffer.size = 0; - temp_buffer.data = push_array(part, char, temp_buffer.max); - // TODO(allen): What about using this stringify loop to convert out? - for (Buffer_Stringify_Loop loop = buffer_stringify_loop(&file->buffer, 0, file->buffer.size, file->buffer.size); + i32 max = partition_remaining(part); + i32 size = 0; + char *data = push_array(part, char, max); + i32 data_size = buffer_size(&file->buffer); + for (Gap_Buffer_Stringify_Loop loop = buffer_stringify_loop(&file->buffer, 0, data_size, data_size); buffer_stringify_good(&loop); buffer_stringify_next(&loop)){ - memcpy(temp_buffer.data, loop.data, loop.size); - temp_buffer.size += loop.size; - buffer_eol_convert_out(&temp_buffer); - result = system_save_file(filename, temp_buffer.data, temp_buffer.size); + memcpy(data + size, loop.data, loop.size); + size += loop.size; } + result = system_save_file(filename, data, size); end_temp_memory(temp); file_synchronize_times(file, filename); return result; @@ -1322,18 +1322,11 @@ view_measure_wraps(General_Memory *general, File_View *view){ general_memory_allocate(general, sizeof(real32)*max, BUBBLE_WRAPS); } } - + Font *font = view->style->font; real32 line_height = (real32)font->height; real32 max_width = view_compute_width(view); - real32 *line_widths = file->buffer.line_widths; - real32 *line_wraps = view->line_wrap_y; - i32 y_pos = 0; - for (i32 i = 0; i < line_count; ++i){ - line_wraps[i] = y_pos*line_height; - i32 line_span = view_wrapped_line_span(line_widths[i], max_width); - y_pos += line_span; - } + buffer_measure_wrap_y(&file->buffer, view->line_wrap_y, line_height, max_width); view->line_count = line_count; } @@ -1351,22 +1344,15 @@ file_create_from_string(General_Memory *general, Editing_File *file, u8 *filenam *file = {}; file->buffer.data = (char*)data; - file->buffer.size = val.size; file->buffer.max = request_size; - - if (val.size > 0){ - memcpy(data, val.str, val.size); - buffer_eol_convert_in(&file->buffer); - } - - data[val.size] = 0; + gap_buffer_initialize(&file->buffer, val.str, val.size); file_synchronize_times(file, filename); file_init_strings(file); file_set_name(file, filename); file->base_map_id = mapid_file; - + file_measure_starts(general, file); file_measure_widths(general, file, font); file->font = font; @@ -1471,7 +1457,7 @@ file_close(General_Memory *general, Editing_File *file){ internal void file_get_dummy(Editing_File *file){ *file = {}; - file->buffer.data = (char*)&file->buffer.size; + file->buffer.data = (char*)&file->buffer.size1; file->is_dummy = 1; } @@ -1479,6 +1465,7 @@ struct Shift_Information{ i32 start, end, amount; }; +#if BUFFER_EXPERIMENT_SCALPEL internal JOB_CALLBACK(job_full_lex){ Editing_File *file = (Editing_File*)data[0]; @@ -1541,6 +1528,7 @@ JOB_CALLBACK(job_full_lex){ file->tokens_complete = 1; file->still_lexing = 0; } +#endif internal void file_kill_tokens(General_Memory *general, Editing_File *file){ @@ -1555,9 +1543,9 @@ file_kill_tokens(General_Memory *general, Editing_File *file){ file->token_stack = {}; } +#if BUFFER_EXPERIMENT_SCALPEL internal void file_first_lex_parallel(General_Memory *general, Editing_File *file){ -#if 1 Assert(file->token_stack.tokens == 0); file->tokens_complete = 0; @@ -1570,9 +1558,6 @@ file_first_lex_parallel(General_Memory *general, Editing_File *file){ job.data[1] = general; job.memory_request = Kbytes(64); file->lex_job = system_post_job(BACKGROUND_THREADS, job); -#else - file_kill_tokens(general, file); -#endif } internal void @@ -1659,17 +1644,18 @@ file_relex_parallel(Mem_Options *mem, Editing_File *file, file->lex_job = system_post_job(BACKGROUND_THREADS, job); } } +#endif internal bool32 file_grow_as_needed(General_Memory *general, Editing_File *file, i32 additional_size){ bool32 result = 1; - i32 target_size = file->buffer.size + additional_size + 1; + i32 size = buffer_size(&file->buffer); + i32 target_size = size + additional_size + 1; if (target_size >= file->buffer.max){ i32 request_size = LargeRoundUp(target_size*2, Kbytes(256)); char *new_data = (char*) - general_memory_reallocate(general, file->buffer.data, file->buffer.size, request_size, BUBBLE_BUFFER); + general_memory_reallocate(general, file->buffer.data, size, request_size, BUBBLE_BUFFER); if (new_data){ - new_data[file->buffer.size] = 0; file->buffer.data = new_data; file->buffer.max = request_size; } @@ -1754,6 +1740,7 @@ struct Edit_Spec{ Edit_Step step; }; +#if BUFFER_EXPERIMENT_SCALPEL internal Edit_Step* file_post_undo(General_Memory *general, Editing_File *file, Edit_Step step, bool32 do_merge, bool32 can_merge){ @@ -1957,6 +1944,7 @@ file_post_history(General_Memory *general, Editing_File *file, return result; } +#endif inline Full_Cursor view_compute_cursor_from_pos(File_View *view, i32 pos){ @@ -2066,11 +2054,12 @@ view_set_file(File_View *view, Editing_File *file, Style *style, view->file = file; General_Memory *general = &view->view_base.mem->general; + AllowLocal(general); Font *font = style->font; view->style = style; view->font_advance = font->advance; view->font_height = font->height; - + view_measure_wraps(general, view); view->cursor = {}; @@ -2219,6 +2208,7 @@ enum History_Mode{ internal void view_update_history_before_edit(Mem_Options *mem, Editing_File *file, Edit_Step step, u8 *str, History_Mode history_mode, i32 next_cursor){ +#if BUFFER_EXPERIMENT_SCALPEL General_Memory *general = &mem->general; #if FRED_SLOW @@ -2387,6 +2377,7 @@ view_update_history_before_edit(Mem_Options *mem, Editing_File *file, Edit_Step } if (history_mode == hist_normal) file->undo.edit_history_cursor = file->undo.history.edit_count; +#endif } inline b32 @@ -2431,28 +2422,36 @@ view_do_single_edit(Mem_Options *mem, File_View *view, Editing_File *file, i32 str_len = spec.step.edit.len; i32 shift_amount; - while (buffer_replace_range(&file->buffer, start, end, str, str_len, &shift_amount)) + while (gap_buffer_replace_range(&file->buffer, start, end, str, str_len, &shift_amount)) file_grow_as_needed(general, file, shift_amount); // NOTE(allen): fixing stuff afterwards +#if BUFFER_EXPERIMENT_SCALPEL if (file->tokens_exist) file_relex_parallel(mem, file, start, end, shift_amount); +#endif i32 line_start = buffer_get_line_index(&file->buffer, start, 0, file->buffer.line_count); i32 line_end = buffer_get_line_index(&file->buffer, end, 0, file->buffer.line_count); i32 replaced_line_count = line_end - line_start; - - // TODO(allen): check this i32 new_line_count = file_count_newlines(file, start, start+str_len); - - i32 line_shift = new_line_count - replaced_line_count; file_remeasure_starts(general, file, line_start, line_end, line_shift, shift_amount); file_remeasure_widths(general, file, file->font, line_start, line_end, line_shift); - - Temp_Memory cursor_temp = begin_temp_memory(&mem->part); + i32 panel_count = layout->panel_count; + Panel *current_panel = layout->panels; + for (i32 i = 0; i < panel_count; ++i, ++current_panel){ + File_View *current_view = view_to_file_view(current_panel->view); + if (current_view && current_view->file == file){ + view_measure_wraps(general, current_view); + } + } + +#if BUFFER_EXPERIMENT_SCALPEL + i32 cursor_count = 0; + Temp_Memory cursor_temp = begin_temp_memory(&mem->part); i32 cursor_max = layout->panel_max_count * 2; Cursor_With_Index *cursors = push_array(&mem->part, Cursor_With_Index, cursor_max); @@ -2490,11 +2489,13 @@ view_do_single_edit(Mem_Options *mem, File_View *view, Editing_File *file, } end_temp_memory(cursor_temp); +#endif } internal void view_do_white_batch_edit(Mem_Options *mem, File_View *view, Editing_File *file, Editing_Layout *layout, Edit_Spec spec, History_Mode history_mode){ +#if BUFFER_EXPERIMENT_SCALPEL Assert(file); ProfileMomentFunction(); @@ -2586,6 +2587,7 @@ view_do_white_batch_edit(Mem_Options *mem, File_View *view, Editing_File *file, } end_temp_memory(cursor_temp); } +#endif } inline void @@ -2709,15 +2711,18 @@ view_history_step(Mem_Options *mem, Editing_Layout *layout, File_View *view, His // TODO(allen): should these still be view operations? internal i32 view_find_end_of_line(File_View *view, i32 pos){ +#if BUFFER_EXPERIMENT_SCALPEL Editing_File *file = view->file; char *data = file->buffer.data; while (pos < file->buffer.size && data[pos] != '\n') ++pos; if (pos > file->buffer.size) pos = file->buffer.size; +#endif return pos; } internal i32 view_find_beginning_of_line(File_View *view, i32 pos){ +#if BUFFER_EXPERIMENT_SCALPEL Editing_File *file = view->file; char *data = file->buffer.data; if (pos > 0){ @@ -2725,11 +2730,13 @@ view_find_beginning_of_line(File_View *view, i32 pos){ while (pos > 0 && data[pos] != '\n') --pos; if (pos != 0) ++pos; } +#endif return pos; } internal i32 view_find_beginning_of_next_line(File_View *view, i32 pos){ +#if BUFFER_EXPERIMENT_SCALPEL Editing_File *file = view->file; char *data = file->buffer.data; while (pos < file->buffer.size && @@ -2739,6 +2746,7 @@ view_find_beginning_of_next_line(File_View *view, i32 pos){ if (pos < file->buffer.size){ ++pos; } +#endif return pos; } @@ -2847,6 +2855,7 @@ enum Endline_Convert_Type{ internal void view_clean_whitespace(Mem_Options *mem, File_View *view, Editing_Layout *layout){ +#if BUFFER_EXPERIMENT_SCALPEL Editing_File *file = view->file; Assert(file && !file->is_dummy); Partition *part = &mem->part; @@ -2917,6 +2926,7 @@ view_clean_whitespace(Mem_Options *mem, File_View *view, Editing_Layout *layout) } end_temp_memory(temp); +#endif } internal u32* @@ -3469,14 +3479,14 @@ draw_file_view(Thread_Context *thread, View *view_, i32_Rect rect, bool32 is_act #if 1 Partition *part = &view_->mem->part; Temp_Memory temp = begin_temp_memory(part); - + partition_align(part, 4); i32 max = partition_remaining(part) / sizeof(Buffer_Render_Item); Buffer_Render_Item *items = push_array(part, Buffer_Render_Item, max); - + i32 count; buffer_get_render_data(&file->buffer, view->line_wrap_y, items, max, &count, - (real32)rect.x0, (real32)rect.y0, view->scroll_x, view->scroll_y, view->unwrapped_lines, + (real32)rect.x0, (real32)rect.y0, view->scroll_x, view->scroll_y, !view->unwrapped_lines, (real32)max_x, (real32)max_y, opad.data, opad.stride, (real32)font->height); Assert(count > 0); @@ -3494,7 +3504,7 @@ draw_file_view(Thread_Context *thread, View *view_, i32_Rect rect, bool32 is_act cursor_color = style->main.cursor_color; at_cursor_color = style->main.at_cursor_color; } - + i32 token_i = 0; u32 main_color = style->main.default_color; if (tokens_use){ @@ -3502,7 +3512,7 @@ draw_file_view(Thread_Context *thread, View *view_, i32_Rect rect, bool32 is_act main_color = *style_get_color(style, token_stack.tokens[result.token_index]); token_i = result.token_index + 1; } - + u32 mark_color = style->main.mark_color; Buffer_Render_Item *item = items; i32 prev_ind = -1; @@ -3523,7 +3533,7 @@ draw_file_view(Thread_Context *thread, View *view_, i32_Rect rect, bool32 is_act } } -#if 0 +#if BUFFER_EXPERIMENT_SCALPEL if (current_token.type == CPP_TOKEN_JUNK && i >= current_token.start && i <= current_token.start + current_token.size){ highlight_color = style->main.highlight_junk_color; @@ -3863,6 +3873,7 @@ 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: @@ -3878,6 +3889,7 @@ HANDLE_COMMAND_SIG(handle_command_file_view){ case FWIDG_SEARCH: { +#if BUFFER_EXPERIMENT_SCALPEL String *string = &file_view->isearch.str; Single_Line_Input_Step result = app_single_line_input_step(codes, key, string); @@ -3965,6 +3977,7 @@ HANDLE_COMMAND_SIG(handle_command_file_view){ file_view->show_temp_highlight = 0; view_set_widget(file_view, FWIDG_NONE); } +#endif }break; case FWIDG_GOTO_LINE: diff --git a/4ed_file_view_golden_array.cpp b/4ed_file_view_golden_array.cpp new file mode 100644 index 00000000..a1575347 --- /dev/null +++ b/4ed_file_view_golden_array.cpp @@ -0,0 +1,4031 @@ +/* + * Mr. 4th Dimention - Allen Webster + * + * 19.08.2015 + * + * File editing view for 4coder + * + */ + +// TOP + +#include "buffer/4coder_shared.cpp" +#include "buffer/4coder_golden_array.cpp" +#include "buffer/4coder_buffer_abstract.cpp" + +struct Range{ + i32 start, end; +}; + +enum Edit_Type{ + ED_NORMAL, + ED_REVERSE_NORMAL, + ED_UNDO, + ED_REDO, +}; + +struct Edit_Step{ + Edit_Type type; + union{ + struct{ + b32 can_merge; + Buffer_Edit edit; + i32 pre_pos; + i32 post_pos; + i32 next_block, prev_block; + }; + struct{ + i32 first_child; + i32 inverse_first_child; + i32 inverse_child_count; + i32 special_type; + }; + }; + i32 child_count; +}; + +struct Edit_Stack{ + u8 *strings; + i32 size, max; + + Edit_Step *edits; + i32 edit_count, edit_max; +}; + +struct Small_Edit_Stack{ + u8 *strings; + i32 size, max; + + Buffer_Edit *edits; + i32 edit_count, edit_max; +}; + +struct Undo_Data{ + Edit_Stack undo; + Edit_Stack redo; + Edit_Stack history; + Small_Edit_Stack children; + + i32 history_block_count, history_head_block; + i32 edit_history_cursor; + b32 current_block_normal; +}; + +struct Editing_File{ + Buffer buffer; + + Undo_Data undo; + + Font *font; + + i32 cursor_pos; + b32 is_dummy; + + char source_path_[256]; + char live_name_[256]; + char extension_[16]; + String source_path; + String live_name; + String extension; + + Cpp_Token_Stack token_stack; + b32 tokens_complete; + b32 tokens_exist; + b32 still_lexing; + u32 lex_job; + i32 base_map_id; + + u64 last_4ed_write_time; + u64 last_4ed_edit_time; + u64 last_sys_write_time; +}; + +struct File_Table_Entry{ + String name; + u32 hash; + i32 index; +}; + +struct File_Table{ + File_Table_Entry *table; + i32 count, max; +}; + +internal u32 +get_file_hash(String name){ + u32 x = 5381; + int i = 0; + char c; + while (i < name.size){ + c = name.str[i++]; + x = ((x << 5) + x) + c; + } + return x; +} + +internal bool32 +table_add(File_Table *table, String name, i32 index){ + Assert(table->count * 3 < table->max * 2); + + File_Table_Entry entry, e; + i32 i; + + entry.name = name; + entry.index = index; + entry.hash = get_file_hash(name); + i = entry.hash % table->max; + while ((e = table->table[i]).name.str){ + if (e.hash == entry.hash && match(e.name, entry.name)){ + return 1; + } + i = (i + 1) % table->max; + } + table->table[i] = entry; + ++table->count; + + return 0; +} + +internal bool32 +table_find_pos(File_Table *table, String name, i32 *index){ + File_Table_Entry e; + i32 i; + u32 hash; + + hash = get_file_hash(name); + i = hash % table->max; + while ((e = table->table[i]).name.size){ + if (e.name.str && e.hash == hash && match(e.name, name)){ + *index = i; + return 1; + } + i = (i + 1) % table->max; + } + + return 0; +} + +inline bool32 +table_find(File_Table *table, String name, i32 *index){ + i32 pos; + bool32 r = table_find_pos(table, name, &pos); + if (r) *index = table->table[pos].index; + return r; +} + +inline bool32 +table_remove(File_Table *table, String name){ + i32 pos; + bool32 r = table_find_pos(table, name, &pos); + if (r){ + table->table[pos].name.str = 0; + --table->count; + } + return r; +} + +struct Working_Set{ + Editing_File *files; + i32 file_index_count, file_max_count; + + File_Table table; + + String clipboards[64]; + i32 clipboard_size, clipboard_max_size; + i32 clipboard_current, clipboard_rolling; +}; + +struct Text_Effect{ + i32 start, end; + u32 color; + i32 tick_down, tick_max; +}; + +struct File_View_Mode{ + bool8 rewrite; +}; + +struct Incremental_Search{ + String str; + bool32 reverse; + i32 pos; +}; + +enum Action_Type{ + DACT_OPEN, + DACT_SAVE_AS, + DACT_SAVE, + DACT_NEW, + DACT_SWITCH, + DACT_TRY_KILL, + DACT_KILL, + DACT_CLOSE_MINOR, + DACT_CLOSE_MAJOR, + DACT_THEME_OPTIONS +}; + +struct Delayed_Action{ + Action_Type type; + String string; + Panel *panel; +}; + +struct Delay{ + Delayed_Action acts[8]; + i32 count, max; +}; + +inline void +delayed_action(Delay *delay, Action_Type type, + String string, Panel *panel){ + Assert(delay->count < delay->max); + Delayed_Action action; + action.type = type; + action.string = string; + action.panel = panel; + delay->acts[delay->count++] = action; +} + +// Hot Directory + +struct Hot_Directory{ + String string; + File_List file_list; +}; + +internal void +hot_directory_init(Hot_Directory *hot_directory, String base){ + hot_directory->string = base; + hot_directory->string.str[255] = 0; + i32 dir_size = system_get_working_directory((u8*)hot_directory->string.str, + hot_directory->string.memory_size); + if (dir_size <= 0){ + dir_size = system_get_easy_directory((u8*)hot_directory->string.str); + } + hot_directory->string.size = dir_size; + append(&hot_directory->string, "\\"); +} + +internal void +hot_directory_clean_end(Hot_Directory *hot_directory){ + String *str = &hot_directory->string; + if (str->size != 0 && str->str[str->size-1] != '\\'){ + str->size = reverse_seek_slash(*str) + 1; + str->str[str->size] = 0; + } +} + +internal i32 +hot_directory_quick_partition(File_Info *infos, i32 start, i32 pivot){ + File_Info *p = infos + pivot; + File_Info *a = infos + start; + for (i32 i = start; i < pivot; ++i, ++a){ + i32 comp = 0; + comp = p->folder - a->folder; + if (comp == 0) comp = compare(a->filename, p->filename); + if (comp < 0){ + Swap(*a, infos[start]); + ++start; + } + } + Swap(*p, infos[start]); + return start; +} + +internal void +hot_directory_quick_sort(File_Info *infos, i32 start, i32 pivot){ + i32 mid = hot_directory_quick_partition(infos, start, pivot); + if (start < mid-1) hot_directory_quick_sort(infos, start, mid-1); + if (mid+1 < pivot) hot_directory_quick_sort(infos, mid+1, pivot); +} + +inline void +hot_directory_fixup(Hot_Directory *hot_directory, Working_Set *working_set){ + File_List *files = &hot_directory->file_list; + if (files->count >= 2) + hot_directory_quick_sort(files->infos, 0, files->count - 1); +} + +inline void +hot_directory_set(Hot_Directory *hot_directory, String str, Working_Set *working_set){ + bool32 success = copy_checked(&hot_directory->string, str); + terminate_with_null(&hot_directory->string); + if (success){ + system_free_file_list(hot_directory->file_list); + hot_directory->file_list = system_get_files(str); + } + hot_directory_fixup(hot_directory, working_set); +} + +inline void +hot_directory_reload(Hot_Directory *hot_directory, Working_Set *working_set){ + if (hot_directory->file_list.block){ + system_free_file_list(hot_directory->file_list); + } + hot_directory->file_list = system_get_files(hot_directory->string); + hot_directory_fixup(hot_directory, working_set); +} + +struct Hot_Directory_Match{ + String filename; + bool32 is_folder; +}; + +internal bool32 +filename_match(String query, Absolutes *absolutes, String filename){ + bool32 result; + result = (query.size == 0); + if (!result) result = wildcard_match(absolutes, filename); + return result; +} + +internal Hot_Directory_Match +hot_directory_first_match(Hot_Directory *hot_directory, + String str, + bool32 include_files, + bool32 exact_match){ + Hot_Directory_Match result = {}; + + Absolutes absolutes; + if (!exact_match) + get_absolutes(str, &absolutes, 1, 1); + + File_List *files = &hot_directory->file_list; + File_Info *info, *end; + end = files->infos + files->count; + for (info = files->infos; info != end; ++info){ + String filename = info->filename; + bool32 is_match = 0; + if (exact_match){ + if (match(filename, str)) is_match = 1; + } + else{ + if (filename_match(str, &absolutes, filename)) is_match = 1; + } + + if (is_match){ + result.is_folder = info->folder; + result.filename = filename; + break; + } + } + + return result; +} + +struct Single_Line_Input_Step{ + bool32 hit_newline; + bool32 hit_ctrl_newline; + bool32 hit_a_character; + bool32 hit_backspace; + bool32 hit_esc; + bool32 made_a_change; + bool32 did_command; +}; + +enum Single_Line_Input_Type{ + SINGLE_LINE_STRING, + SINGLE_LINE_FILE +}; + +struct Single_Line_Mode{ + Single_Line_Input_Type type; + String *string; + Hot_Directory *hot_directory; + bool32 fast_folder_select; +}; + +internal Single_Line_Input_Step +app_single_line_input_core(Key_Codes *codes, Working_Set *working_set, + Key_Single key, Single_Line_Mode mode){ + Single_Line_Input_Step result = {}; + + if (key.key.keycode == codes->back){ + result.hit_backspace = 1; + if (mode.string->size > 0){ + result.made_a_change = 1; + --mode.string->size; + switch (mode.type){ + case SINGLE_LINE_STRING: + mode.string->str[mode.string->size] = 0; break; + + case SINGLE_LINE_FILE: + { + char end_character = mode.string->str[mode.string->size]; + if (char_is_slash(end_character)){ + mode.string->size = reverse_seek_slash(*mode.string) + 1; + mode.string->str[mode.string->size] = 0; + hot_directory_set(mode.hot_directory, *mode.string, working_set); + } + else{ + mode.string->str[mode.string->size] = 0; + } + }break; + } + } + } + + else if (key.key.character == '\n' || key.key.character == '\t'){ + result.made_a_change = 1; + if (key.modifiers[CONTROL_KEY_CONTROL] || + key.modifiers[CONTROL_KEY_ALT]){ + result.hit_ctrl_newline = 1; + } + else{ + result.hit_newline = 1; + if (mode.fast_folder_select){ + char front_name_space[256]; + String front_name = + make_string(front_name_space, 0, ArrayCount(front_name_space)); + get_front_of_directory(&front_name, *mode.string); + Hot_Directory_Match match; + match = hot_directory_first_match(mode.hot_directory, front_name, 1, 1); + if (!match.filename.str){ + match = hot_directory_first_match(mode.hot_directory, front_name, 1, 0); + } + if (match.filename.str){ + if (match.is_folder){ + set_last_folder(mode.string, match.filename); + hot_directory_set(mode.hot_directory, *mode.string, working_set); + result.hit_newline = 0; + } + else{ + mode.string->size = reverse_seek_slash(*mode.string) + 1; + append(mode.string, match.filename); + result.hit_newline = 1; + } + } + } + } + } + + else if (key.key.keycode == codes->esc){ + result.hit_esc = 1; + result.made_a_change = 1; + } + + else if (key.key.character){ + result.hit_a_character = 1; + if (!key.modifiers[CONTROL_KEY_CONTROL] && + !key.modifiers[CONTROL_KEY_ALT]){ + if (mode.string->size+1 < mode.string->memory_size){ + u8 new_character = (u8)key.key.character; + mode.string->str[mode.string->size] = new_character; + mode.string->size++; + mode.string->str[mode.string->size] = 0; + if (mode.type == SINGLE_LINE_FILE && char_is_slash(new_character)){ + hot_directory_set(mode.hot_directory, *mode.string, working_set); + } + result.made_a_change = 1; + } + } + else{ + result.did_command = 1; + result.made_a_change = 1; + } + } + + return result; +} + +inline Single_Line_Input_Step +app_single_line_input_step(Key_Codes *codes, Key_Single key, String *string){ + Single_Line_Mode mode = {}; + mode.type = SINGLE_LINE_STRING; + mode.string = string; + return app_single_line_input_core(codes, 0, key, mode); +} + +inline Single_Line_Input_Step +app_single_file_input_step(Key_Codes *codes, Working_Set *working_set, Key_Single key, + String *string, Hot_Directory *hot_directory, + bool32 fast_folder_select){ + Single_Line_Mode mode = {}; + mode.type = SINGLE_LINE_FILE; + mode.string = string; + mode.hot_directory = hot_directory; + mode.fast_folder_select = fast_folder_select; + return app_single_line_input_core(codes, working_set, key, mode); +} + +inline Single_Line_Input_Step +app_single_number_input_step(Key_Codes *codes, Key_Single key, String *string){ + Single_Line_Input_Step result = {}; + Single_Line_Mode mode = {}; + mode.type = SINGLE_LINE_STRING; + mode.string = string; + + char c = (char)key.key.character; + if (c == 0 || c == '\n' || char_is_numeric(c)) + result = app_single_line_input_core(codes, 0, key, mode); + return result; +} + +struct Widget_ID{ + i32 id; + i32 sub_id0; + i32 sub_id1; + i32 sub_id2; +}; + +inline bool32 +widget_match(Widget_ID s1, Widget_ID s2){ + return (s1.id == s2.id && s1.sub_id0 == s2.sub_id0 && + s1.sub_id1 == s2.sub_id1 && s1.sub_id2 == s2.sub_id2); +} + +struct UI_State{ + Render_Target *target; + Style *style; + Font *font; + Mouse_Summary *mouse; + Key_Summary *keys; + Key_Codes *codes; + Working_Set *working_set; + + Widget_ID selected, hover, hot; + bool32 activate_me; + bool32 redraw; + bool32 input_stage; + i32 sub_id1_change; + + real32 height, view_y; +}; + +inline Widget_ID +make_id(UI_State *state, i32 id){ + Widget_ID r = state->selected; + r.id = id; + return r; +} + +inline Widget_ID +make_sub0(UI_State *state, i32 id){ + Widget_ID r = state->selected; + r.sub_id0 = id; + return r; +} + +inline Widget_ID +make_sub1(UI_State *state, i32 id){ + Widget_ID r = state->selected; + r.sub_id1 = id; + return r; +} + +inline Widget_ID +make_sub2(UI_State *state, i32 id){ + Widget_ID r = state->selected; + r.sub_id2 = id; + return r; +} + +inline bool32 +is_selected(UI_State *state, Widget_ID id){ + return widget_match(state->selected, id); +} + +inline bool32 +is_hot(UI_State *state, Widget_ID id){ + return widget_match(state->hot, id); +} + +inline bool32 +is_hover(UI_State *state, Widget_ID id){ + return widget_match(state->hover, id); +} + +struct UI_Layout{ + i32 row_count; + i32 row_item_width; + i32 row_max_item_height; + + i32_Rect rect; + i32 x, y; +}; + +struct UI_Layout_Restore{ + UI_Layout layout; + UI_Layout *dest; +}; + +inline void +begin_layout(UI_Layout *layout, i32_Rect rect){ + layout->rect = rect; + layout->x = rect.x0; + layout->y = rect.y0; + layout->row_count = 0; + layout->row_max_item_height = 0; +} + +inline void +begin_row(UI_Layout *layout, i32 count){ + layout->row_count = count; + layout->row_item_width = (layout->rect.x1 - layout->x) / count; +} + +inline i32_Rect +layout_rect(UI_Layout *layout, i32 height){ + i32_Rect rect; + rect.x0 = layout->x; + rect.y0 = layout->y; + rect.x1 = rect.x0; + rect.y1 = rect.y0 + height; + if (layout->row_count > 0){ + --layout->row_count; + rect.x1 = rect.x0 + layout->row_item_width; + layout->x += layout->row_item_width; + layout->row_max_item_height = Max(height, layout->row_max_item_height); + } + if (layout->row_count == 0){ + rect.x1 = layout->rect.x1; + layout->row_max_item_height = Max(height, layout->row_max_item_height); + layout->y += layout->row_max_item_height; + layout->x = layout->rect.x0; + layout->row_max_item_height = 0; + } + return rect; +} + +inline UI_Layout_Restore +begin_sub_layout(UI_Layout *layout, i32_Rect area){ + UI_Layout_Restore restore; + restore.layout = *layout; + restore.dest = layout; + begin_layout(layout, area); + return restore; +} + +inline void +end_sub_layout(UI_Layout_Restore restore){ + *restore.dest = restore.layout; +} + +struct UI_Style{ + u32 dark, dim, bright; + u32 base, pop1, pop2; +}; + +internal UI_Style +get_ui_style(Style *style){ + UI_Style ui_style; + ui_style.dark = style->main.back_color; + ui_style.dim = style->main.margin_color; + ui_style.bright = style->main.margin_active_color; + ui_style.base = style->main.default_color; + ui_style.pop1 = style->main.file_info_style.pop1_color; + ui_style.pop2 = style->main.file_info_style.pop2_color; + return ui_style; +} + +internal UI_Style +get_ui_style_upper(Style *style){ + UI_Style ui_style; + ui_style.dark = style->main.margin_color; + ui_style.dim = style->main.margin_hover_color; + ui_style.bright = style->main.margin_active_color; + ui_style.base = style->main.default_color; + ui_style.pop1 = style->main.file_info_style.pop1_color; + ui_style.pop2 = style->main.file_info_style.pop2_color; + return ui_style; +} + +inline void +get_colors(UI_State *state, u32 *back, u32 *fore, Widget_ID wid, UI_Style style){ + bool32 hover = is_hover(state, wid); + bool32 hot = is_hot(state, wid); + i32 level = hot + hover; + switch (level){ + case 2: + *back = style.bright; + *fore = style.dark; + break; + case 1: + *back = style.dim; + *fore = style.bright; + break; + case 0: + *back = style.dark; + *fore = style.bright; + break; + } +} + +inline void +get_pop_color(UI_State *state, u32 *pop, Widget_ID wid, UI_Style style){ + bool32 hover = is_hover(state, wid); + bool32 hot = is_hot(state, wid); + i32 level = hot + hover; + switch (level){ + case 2: + *pop = style.pop1; + break; + case 1: + *pop = style.pop1; + break; + case 0: + *pop = style.pop1; + break; + } +} + +internal UI_State +ui_state_init(UI_State *state_in, Render_Target *target, Input_Summary *user_input, + Style *style, Working_Set *working_set, bool32 input_stage){ + UI_State state = {}; + state.target = target; + state.style = style; + state.font = style->font; + state.working_set = working_set; + if (user_input){ + state.mouse = &user_input->mouse; + state.keys = &user_input->keys; + state.codes = user_input->codes; + } + state.selected = state_in->selected; + state.hot = state_in->hot; + if (input_stage) state.hover = {}; + else state.hover = state_in->hover; + state.redraw = 0; + state.activate_me = 0; + state.input_stage = input_stage; + state.height = state_in->height; + state.view_y = state_in->view_y; + return state; +} + +inline bool32 +ui_state_match(UI_State a, UI_State b){ + return (widget_match(a.selected, b.selected) && + widget_match(a.hot, b.hot) && + widget_match(a.hover, b.hover)); +} + +internal bool32 +ui_finish_frame(UI_State *persist_state, UI_State *state, UI_Layout *layout, i32_Rect rect, + bool32 do_wheel, bool32 *did_activation){ + bool32 result = 0; + real32 h = layout->y + persist_state->view_y - rect.y0; + real32 max_y = h - (rect.y1 - rect.y0); + + persist_state->height = h; + persist_state->view_y = state->view_y; + + if (state->input_stage){ + Mouse_Summary *mouse = state->mouse; + if (mouse->wheel_used && do_wheel){ + persist_state->view_y += mouse->wheel_amount*state->font->height; + result = 1; + } + if (mouse->release_l && widget_match(state->hot, state->hover)){ + if (did_activation) *did_activation = 1; + if (state->activate_me){ + state->selected = state->hot; + } + } + if (!mouse->l && !mouse->r){ + state->hot = {}; + } + + if (!ui_state_match(*persist_state, *state) || state->redraw){ + result = 1; + } + + *persist_state = *state; + } + + if (persist_state->view_y >= max_y) persist_state->view_y = max_y; + if (persist_state->view_y < 0) persist_state->view_y = 0; + + return result; +} + +internal bool32 +ui_do_button_input(UI_State *state, i32_Rect rect, Widget_ID id, bool32 activate, bool32 *right = 0){ + bool32 result = 0; + Mouse_Summary *mouse = state->mouse; + bool32 hover = hit_check(mouse->mx, mouse->my, rect); + if (hover){ + state->hover = id; + if (activate) state->activate_me = 1; + if (mouse->press_l || (mouse->press_r && right)) state->hot = id; + if (mouse->l && mouse->r) state->hot = {}; + } + bool32 is_match = is_hot(state, id); + if (mouse->release_l && is_match){ + if (hover) result = 1; + state->redraw = 1; + } + if (right && mouse->release_r && is_match){ + if (hover) *right = 1; + state->redraw = 1; + } + return result; +} + +internal bool32 +ui_do_subdivided_button_input(UI_State *state, i32_Rect rect, i32 parts, Widget_ID id, bool32 activate, i32 *indx_out, bool32 *right = 0){ + bool32 result = 0; + real32 x0, x1; + i32_Rect sub_rect; + Widget_ID sub_widg = id; + real32 sub_width = (rect.x1 - rect.x0) / (real32)parts; + sub_rect.y0 = rect.y0; + sub_rect.y1 = rect.y1; + x1 = (real32)rect.x0; + + for (i32 i = 0; i < parts; ++i){ + x0 = x1; + x1 = x1 + sub_width; + sub_rect.x0 = TRUNC32(x0); + sub_rect.x1 = TRUNC32(x1); + sub_widg.sub_id2 = i; + if (ui_do_button_input(state, sub_rect, sub_widg, activate, right)){ + *indx_out = i; + break; + } + } + + return result; +} + +internal real32 +ui_do_vscroll_input(UI_State *state, i32_Rect top, i32_Rect bottom, i32_Rect slider, + Widget_ID id, real32 val, real32 step_amount, + real32 smin, real32 smax, real32 vmin, real32 vmax){ + Mouse_Summary *mouse = state->mouse; + i32 mx = mouse->mx; + i32 my = mouse->my; + if (hit_check(mx, my, top)){ + state->hover = id; + state->hover.sub_id2 = 1; + } + if (hit_check(mx, my, bottom)){ + state->hover = id; + state->hover.sub_id2 = 2; + } + if (hit_check(mx, my, slider)){ + state->hover = id; + state->hover.sub_id2 = 3; + } + if (mouse->press_l) state->hot = state->hover; + if (id.id == state->hot.id){ + if (mouse->release_l){ + Widget_ID wid1, wid2; + wid1 = wid2 = id; + wid1.sub_id2 = 1; + wid2.sub_id2 = 2; + if (state->hot.sub_id2 == 1 && is_hover(state, wid1)) val -= step_amount; + if (state->hot.sub_id2 == 2 && is_hover(state, wid2)) val += step_amount; + state->redraw = 1; + } + if (state->hot.sub_id2 == 3){ + real32 S, L; + S = (real32)mouse->my - (slider.y1 - slider.y0) / 2; + if (S < smin) S = smin; + if (S > smax) S = smax; + L = unlerp(smin, S, smax); + val = lerp(vmin, L, vmax); + state->redraw = 1; + } + } + return val; +} + +internal bool32 +ui_do_text_field_input(UI_State *state, String *str){ + bool32 result = 0; + Key_Summary *keys = state->keys; + for (i32 key_i = 0; key_i < keys->count; ++key_i){ + Key_Single key = get_single_key(keys, key_i); + char c = (char)key.key.character; + if (char_is_basic(c) && str->size < str->memory_size-1){ + str->str[str->size++] = c; + str->str[str->size] = 0; + } + else if (c == '\n'){ + result = 1; + } + else if (key.key.keycode == state->codes->back && str->size > 0){ + str->str[--str->size] = 0; + } + } + return result; +} + +internal bool32 +ui_do_file_field_input(UI_State *state, Hot_Directory *hot_dir){ + bool32 result = 0; + Key_Summary *keys = state->keys; + for (i32 key_i = 0; key_i < keys->count; ++key_i){ + Key_Single key = get_single_key(keys, key_i); + String *str = &hot_dir->string; + terminate_with_null(str); + Single_Line_Input_Step step = + app_single_file_input_step(state->codes, state->working_set, key, str, hot_dir, 1); + if (step.hit_newline || step.hit_ctrl_newline) result = 1; + } + return result; +} + +internal bool32 +ui_do_line_field_input(UI_State *state, String *string){ + bool32 result = 0; + Key_Summary *keys = state->keys; + for (i32 key_i = 0; key_i < keys->count; ++key_i){ + Key_Single key = get_single_key(keys, key_i); + terminate_with_null(string); + Single_Line_Input_Step step = + app_single_line_input_step(state->codes, key, string); + if (step.hit_newline || step.hit_ctrl_newline) result = 1; + } + return result; +} + +internal bool32 +ui_do_slider_input(UI_State *state, i32_Rect rect, Widget_ID wid, + real32 min, real32 max, real32 *v){ + bool32 result = 0; + ui_do_button_input(state, rect, wid, 0); + Mouse_Summary *mouse = state->mouse; + if (is_hot(state, wid)){ + result = 1; + *v = unlerp(min, (real32)mouse->mx, max); + state->redraw = 1; + } + return result; +} + +internal bool32 +do_text_field(Widget_ID wid, UI_State *state, UI_Layout *layout, + String prompt, String dest){ + bool32 result = 0; + Font *font = state->font; + i32 character_h = font->height; + + i32_Rect rect = layout_rect(layout, character_h); + + if (state->input_stage){ + ui_do_button_input(state, rect, wid, 1); + if (is_selected(state, wid)){ + if (ui_do_text_field_input(state, &dest)){ + result = 1; + } + } + } + else{ + Render_Target *target = state->target; + UI_Style ui_style = get_ui_style_upper(state->style); + u32 back, fore, prompt_pop; + get_colors(state, &back, &fore, wid, ui_style); + get_pop_color(state, &prompt_pop, wid, ui_style); + + draw_rectangle(target, rect, back); + i32 x = rect.x0; + x = draw_string(target, font, prompt, x, rect.y0, prompt_pop); + draw_string(target, font, dest, x, rect.y0, ui_style.base); + } + + return result; +} + +enum File_View_Widget_Type{ + FWIDG_NONE, + FWIDG_SEARCH, + FWIDG_GOTO_LINE, + FWIDG_TIMELINES, + // never below this + FWIDG_TYPE_COUNT +}; + +struct File_View_Widget{ + UI_State state; + File_View_Widget_Type type; + i32 height; + struct{ + bool32 undo_line; + bool32 history_line; + } timeline; +}; + +struct File_View{ + View view_base; + + Delay *delay; + Editing_Layout *layout; + + Editing_File *file; + Style *style; + + i32 font_advance; + i32 font_height; + + Full_Cursor cursor; + i32 mark; + real32 scroll_y, target_y, vel_y; + real32 scroll_x, target_x, vel_x; + real32 preferred_x; + Full_Cursor scroll_y_cursor; + union{ + Incremental_Search isearch; + struct{ + String str; + } gotoline; + }; + + Full_Cursor temp_highlight; + i32 temp_highlight_end_pos; + bool32 show_temp_highlight; + + File_View_Mode mode, next_mode; + File_View_Widget widget; + real32 rewind_amount, rewind_speed; + i32 rewind_max, scrub_max; + bool32 unwrapped_lines; + bool32 show_whitespace; + + i32 line_count, line_max; + real32 *line_wrap_y; + + Text_Effect paste_effect; +}; + +inline File_View* +view_to_file_view(View *view){ + File_View* result = 0; + if (view && view->type == VIEW_TYPE_FILE){ + result = (File_View*)view; + } + return result; +} + +internal Range +get_range(i32 a, i32 b){ + Range result = {}; + if (a < b){ + result.start = a; + result.end = b; + } + else{ + result.start = b; + result.end = a; + } + return result; +} + +inline bool32 +starts_new_line(u8 character){ + return (character == '\n'); +} + +inline void +file_init_strings(Editing_File *file){ + file->source_path = make_fixed_width_string(file->source_path_); + file->live_name = make_fixed_width_string(file->live_name_); + file->extension = make_fixed_width_string(file->extension_); +} + +inline void +file_set_name(Editing_File *file, u8 *filename){ + String f, ext; + f = make_string_slowly((char*)filename); + copy_checked(&file->source_path, f); + file->live_name = make_fixed_width_string(file->live_name_); + get_front_of_directory(&file->live_name, f); + ext = file_extension(f); + copy(&file->extension, ext); +} + +inline void +file_synchronize_times(Editing_File *file, u8 *filename){ + Time_Stamp stamp = system_file_time_stamp(filename); + if (stamp.success){ + file->last_4ed_write_time = stamp.time; + file->last_4ed_edit_time = stamp.time; + file->last_sys_write_time = stamp.time; + } +} + +internal bool32 +file_save(Partition *part, Editing_File *file, u8 *filename){ + bool32 result = 0; + Temp_Memory temp = begin_temp_memory(part); + Buffer temp_buffer; + temp_buffer.max = partition_remaining(part); + temp_buffer.size = 0; + temp_buffer.data = push_array(part, char, temp_buffer.max); + // TODO(allen): What about using this stringify loop to convert out? + for (Buffer_Stringify_Loop loop = buffer_stringify_loop(&file->buffer, 0, file->buffer.size, file->buffer.size); + buffer_stringify_good(&loop); + buffer_stringify_next(&loop)){ + memcpy(temp_buffer.data, loop.data, loop.size); + temp_buffer.size += loop.size; + buffer_eol_convert_out(&temp_buffer); + result = system_save_file(filename, temp_buffer.data, temp_buffer.size); + } + end_temp_memory(temp); + file_synchronize_times(file, filename); + return result; +} + +inline bool32 +file_save_and_set_names(Partition *part, Editing_File *file, u8 *filename){ + bool32 result = 0; + if (file_save(part, file, filename)){ + result = 1; + file_set_name(file, filename); + } + return result; +} + +inline i32 +file_count_newlines(Editing_File *file, i32 start, i32 end){ + i32 count = buffer_count_newlines(&file->buffer, start, end); + return count; +} + +enum File_Bubble_Type{ + BUBBLE_BUFFER = 1, + BUBBLE_STARTS, + BUBBLE_WIDTHS, + BUBBLE_WRAPS, + BUBBLE_TOKENS, + BUBBLE_UNDO_STRING, + BUBBLE_UNDO, + BUBBLE_UNDO_CHILDREN, + // + FILE_BUBBLE_TYPE_END, +}; + +#define GROW_FAILED 0 +#define GROW_NOT_NEEDED 1 +#define GROW_SUCCESS 2 + +internal i32 +file_grow_starts_as_needed(General_Memory *general, Editing_File *file, i32 additional_lines){ + bool32 result = GROW_NOT_NEEDED; + i32 max = file->buffer.line_max; + i32 count = file->buffer.line_count; + i32 target_lines = count + additional_lines; + if (target_lines > max){ + max <<= 1; + i32 *new_lines = (i32*) + general_memory_reallocate(general, file->buffer.line_starts, + sizeof(i32)*count, sizeof(i32)*max, BUBBLE_STARTS); + if (new_lines){ + file->buffer.line_starts = new_lines; + file->buffer.line_max = max; + result = GROW_SUCCESS; + } + else{ + result = GROW_FAILED; + } + } + return result; +} + +internal void +file_measure_starts(General_Memory *general, Editing_File *file){ + ProfileMomentFunction(); + if (!file->buffer.line_starts){ + i32 max = file->buffer.line_max = Kbytes(1); + file->buffer.line_starts = (i32*)general_memory_allocate(general, max*sizeof(i32), BUBBLE_STARTS); + TentativeAssert(file->buffer.line_starts); + // TODO(allen): when unable to allocate? + } + + Buffer_Measure_Starts state = {}; + while (buffer_measure_starts(&state, &file->buffer)){ + i32 max = file->buffer.line_max; + i32 count = state.count; + i32 target_lines = count + 1; + + max = (target_lines << 1); + i32 *new_lines = (i32*) + general_memory_reallocate(general, file->buffer.line_starts, + sizeof(i32)*count, sizeof(i32)*max, BUBBLE_STARTS); + + // TODO(allen): when unable to grow? + TentativeAssert(new_lines); + file->buffer.line_starts = new_lines; + file->buffer.line_max = max; + } + file->buffer.line_count = state.count; +} + +internal void +file_remeasure_starts(General_Memory *general, Editing_File *file, + i32 line_start, i32 line_end, i32 line_shift, + i32 character_shift){ + ProfileMomentFunction(); + + Assert(file->buffer.line_starts); + file_grow_starts_as_needed(general, file, line_shift); + + buffer_remeasure_starts(&file->buffer, line_start, line_end, line_shift, character_shift); +} + +struct Opaque_Font_Advance{ + void *data; + int stride; +}; + +inline Opaque_Font_Advance +get_opaque_font_advance(Font *font){ + Opaque_Font_Advance result; + result.data = (char*)font->chardata + OffsetOfPtr(font->chardata, xadvance); + result.stride = sizeof(*font->chardata); + return result; +} + +internal void +file_grow_widths_as_needed(General_Memory *general, Editing_File *file){ + i32 line_count = file->buffer.line_count; + if (line_count > file->buffer.widths_max){ + i32 new_max = LargeRoundUp(line_count, Kbytes(1)); + if (file->buffer.line_widths){ + file->buffer.line_widths = (real32*) + general_memory_reallocate_nocopy(general, file->buffer.line_widths, sizeof(real32)*new_max, BUBBLE_WIDTHS); + } + else{ + file->buffer.line_widths = (real32*) + general_memory_allocate(general, sizeof(real32)*new_max, BUBBLE_WIDTHS); + } + file->buffer.widths_max = new_max; + } +} + +internal void +file_measure_widths(General_Memory *general, Editing_File *file, Font *font){ + ProfileMomentFunction(); + + file_grow_widths_as_needed(general, file); + Opaque_Font_Advance opad = get_opaque_font_advance(font); + buffer_measure_widths(&file->buffer, opad.data, opad.stride); +} + +internal void +file_remeasure_widths(General_Memory *general, Editing_File *file, Font *font, + i32 line_start, i32 line_end, i32 line_shift){ + ProfileMomentFunction(); + + file_grow_widths_as_needed(general, file); + Opaque_Font_Advance opad = get_opaque_font_advance(font); + buffer_remeasure_widths(&file->buffer, opad.data, opad.stride, + line_start, line_end, line_shift); +} + +inline i32 +view_wrapped_line_span(real32 line_width, real32 max_width){ + i32 line_count = CEIL32(line_width / max_width); + if (line_count == 0) line_count = 1; + return line_count; +} + +internal i32 +view_compute_lowest_line(File_View *view){ + i32 last_line = view->line_count - 1; + i32 lowest_line = 0; + if (last_line > 0){ + if (view->unwrapped_lines){ + lowest_line = last_line; + } + else{ + real32 wrap_y = view->line_wrap_y[last_line]; + Editing_File *file = view->file; + Assert(!file->is_dummy); + Style *style = view->style; + Font *font = style->font; + lowest_line = FLOOR32(wrap_y / font->height); + + real32 width = file->buffer.line_widths[last_line]; + real32 max_width = view_compute_width(view); + i32 line_span = view_wrapped_line_span(width, max_width); + lowest_line += line_span - 1; + } + } + return lowest_line; +} + +internal void +view_measure_wraps(General_Memory *general, File_View *view){ + ProfileMomentFunction(); + Editing_File *file = view->file; + i32 line_count = file->buffer.line_count; + + if (view->line_max < line_count){ + i32 max = view->line_max = LargeRoundUp(line_count, Kbytes(1)); + if (view->line_wrap_y){ + view->line_wrap_y = (real32*) + general_memory_reallocate_nocopy(general, view->line_wrap_y, sizeof(real32)*max, BUBBLE_WRAPS); + } + else{ + view->line_wrap_y = (real32*) + general_memory_allocate(general, sizeof(real32)*max, BUBBLE_WRAPS); + } + } + + Font *font = view->style->font; + real32 line_height = (real32)font->height; + real32 max_width = view_compute_width(view); + buffer_measure_wrap_y(&file->buffer, view->line_wrap_y, line_height, max_width); + + view->line_count = line_count; +} + +internal void +file_create_from_string(General_Memory *general, Editing_File *file, u8 *filename, Font *font, String val){ + i32 request_size = LargeRoundUp(1+val.size*2, Kbytes(64)); + u8 *data = (u8*)general_memory_allocate(general, request_size, BUBBLE_BUFFER); + + // TODO(allen): if we didn't get the memory what is going on? + // request_size too large? + // need to expand general memory allocator's base pool? + // need to release least recently used target? + Assert(data); + + *file = {}; + file->buffer.data = (char*)data; + file->buffer.size = val.size; + file->buffer.max = request_size; + + if (val.size > 0){ + memcpy(data, val.str, val.size); + buffer_eol_convert_in(&file->buffer); + } + + data[val.size] = 0; + + file_synchronize_times(file, filename); + file_init_strings(file); + file_set_name(file, filename); + + file->base_map_id = mapid_file; + + file_measure_starts(general, file); + file_measure_widths(general, file, font); + file->font = font; + + file->undo.undo.max = request_size; + file->undo.undo.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); + file->undo.undo.edit_max = request_size / sizeof(Edit_Step); + file->undo.undo.edits = (Edit_Step*)general_memory_allocate(general, request_size, BUBBLE_UNDO); + + file->undo.redo.max = request_size; + file->undo.redo.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); + file->undo.redo.edit_max = request_size / sizeof(Edit_Step); + file->undo.redo.edits = (Edit_Step*)general_memory_allocate(general, request_size, BUBBLE_UNDO); + + file->undo.history.max = request_size; + file->undo.history.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); + file->undo.history.edit_max = request_size / sizeof(Edit_Step); + file->undo.history.edits = (Edit_Step*)general_memory_allocate(general, request_size, BUBBLE_UNDO); + + file->undo.children.max = request_size; + file->undo.children.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); + file->undo.children.edit_max = request_size / sizeof(Buffer_Edit); + file->undo.children.edits = (Buffer_Edit*)general_memory_allocate(general, request_size, BUBBLE_UNDO); + + file->undo.history_block_count = 1; + file->undo.history_head_block = 0; + file->undo.current_block_normal = 1; +} + +internal bool32 +file_create(General_Memory *general, Editing_File *file, u8 *filename, Font *font){ + bool32 result = 0; + + File_Data raw_file = system_load_file(filename); + if (raw_file.data){ + result = 1; + String val = make_string((char*)raw_file.data, raw_file.size); + file_create_from_string(general, file, filename, font, val); + system_free_file(raw_file); + } + + return result; +} + +internal bool32 +file_create_empty(General_Memory *general, Editing_File *file, u8 *filename, Font *font){ + bool32 result = 1; + + String empty_str = {}; + file_create_from_string(general, file, filename, font, empty_str); + + return result; +} + +struct Get_File_Result{ + Editing_File *file; + i32 index; +}; + +internal Get_File_Result +working_set_get_available_file(Working_Set *working_set){ + Get_File_Result result = {}; + + for (i32 buffer_index = 1; buffer_index < working_set->file_max_count; ++buffer_index){ + if (buffer_index == working_set->file_index_count || + working_set->files[buffer_index].is_dummy){ + result.index = buffer_index; + result.file = working_set->files + buffer_index; + if (buffer_index == working_set->file_index_count){ + ++working_set->file_index_count; + } + break; + } + } + + Assert(result.file); + return result; +} + +internal void +file_close(General_Memory *general, Editing_File *file){ + if (file->still_lexing){ + system_cancel_job(BACKGROUND_THREADS, file->lex_job); + } + if (file->token_stack.tokens){ + general_memory_free(general, file->token_stack.tokens); + } + general_memory_free(general, file->buffer.data); + general_memory_free(general, file->buffer.line_starts); + general_memory_free(general, file->buffer.line_widths); + + general_memory_free(general, file->undo.undo.strings); + general_memory_free(general, file->undo.undo.edits); + + general_memory_free(general, file->undo.redo.strings); + general_memory_free(general, file->undo.redo.edits); + + general_memory_free(general, file->undo.history.strings); + general_memory_free(general, file->undo.history.edits); +} + +internal void +file_get_dummy(Editing_File *file){ + *file = {}; + file->buffer.data = (char*)&file->buffer.size; + file->is_dummy = 1; +} + +struct Shift_Information{ + i32 start, end, amount; +}; + +internal +JOB_CALLBACK(job_full_lex){ + Editing_File *file = (Editing_File*)data[0]; + General_Memory *general = (General_Memory*)data[1]; + + Cpp_File cpp_file; + cpp_file.data = file->buffer.data; + cpp_file.size = file->buffer.size; + + Cpp_Token_Stack tokens; + tokens.tokens = (Cpp_Token*)memory->data; + tokens.max_count = memory->size / sizeof(Cpp_Token); + tokens.count = 0; + + Cpp_Lex_Data status; + status = cpp_lex_file_nonalloc(cpp_file, &tokens); + while (!status.complete){ + system_grow_thread_memory(memory); + tokens.tokens = (Cpp_Token*)memory->data; + tokens.max_count = memory->size / sizeof(Cpp_Token); + status = cpp_lex_file_nonalloc(cpp_file, &tokens, status); + } + + i32 new_max = LargeRoundUp(tokens.count, Kbytes(1)); + system_aquire_lock(FRAME_LOCK); + if (file->token_stack.tokens){ + file->token_stack.tokens = (Cpp_Token*) + general_memory_reallocate_nocopy(general, file->token_stack.tokens, new_max*sizeof(Cpp_Token), BUBBLE_TOKENS); + } + else{ + file->token_stack.tokens = (Cpp_Token*) + general_memory_allocate(general, new_max*sizeof(Cpp_Token), BUBBLE_TOKENS); + } + system_release_lock(FRAME_LOCK); + + i32 copy_amount = Kbytes(8); + i32 uncoppied = tokens.count*sizeof(Cpp_Token); + if (copy_amount > uncoppied) copy_amount = uncoppied; + + u8 *dest = (u8*)file->token_stack.tokens; + u8 *src = (u8*)tokens.tokens; + + while (uncoppied > 0){ + system_aquire_lock(FRAME_LOCK); + memcpy(dest, src, copy_amount); + system_release_lock(FRAME_LOCK); + dest += copy_amount; + src += copy_amount; + uncoppied -= copy_amount; + if (copy_amount > uncoppied) copy_amount = uncoppied; + } + + file->token_stack.count = tokens.count; + file->token_stack.max_count = new_max; + system_force_redraw(); + + // NOTE(allen): These are outside the locked section because I don't + // think getting these out of order will cause critical bugs, and I + // want to minimize what's done in locked sections. + file->tokens_complete = 1; + file->still_lexing = 0; +} + +internal void +file_kill_tokens(General_Memory *general, Editing_File *file){ + if (file->still_lexing){ + system_cancel_job(BACKGROUND_THREADS, file->lex_job); + } + if (file->token_stack.tokens){ + general_memory_free(general, file->token_stack.tokens); + } + file->tokens_exist = 0; + file->tokens_complete = 0; + file->token_stack = {}; +} + +internal void +file_first_lex_parallel(General_Memory *general, Editing_File *file){ + Assert(file->token_stack.tokens == 0); + + file->tokens_complete = 0; + file->tokens_exist = 1; + file->still_lexing = 1; + + Job_Data job; + job.callback = job_full_lex; + job.data[0] = file; + job.data[1] = general; + job.memory_request = Kbytes(64); + file->lex_job = system_post_job(BACKGROUND_THREADS, job); +} + +internal void +file_relex_parallel(Mem_Options *mem, Editing_File *file, + i32 start_i, i32 end_i, i32 amount){ + General_Memory *general = &mem->general; + Partition *part = &mem->part; + if (file->token_stack.tokens == 0){ + file_first_lex_parallel(general, file); + return; + } + + bool32 inline_lex = !file->still_lexing; + if (inline_lex){ + Cpp_File cpp_file; + cpp_file.data = file->buffer.data; + cpp_file.size = file->buffer.size; + + Cpp_Token_Stack *stack = &file->token_stack; + + Cpp_Relex_State state = + cpp_relex_nonalloc_start(cpp_file, stack, + start_i, end_i, amount, 100); + + Temp_Memory temp = begin_temp_memory(part); + i32 relex_end; + Cpp_Token_Stack relex_space; + relex_space.count = 0; + relex_space.max_count = state.space_request; + relex_space.tokens = push_array(part, Cpp_Token, relex_space.max_count); + if (cpp_relex_nonalloc_main(&state, &relex_space, &relex_end)){ + inline_lex = 0; + } + else{ + i32 delete_amount = relex_end - state.start_token_i; + i32 shift_amount = relex_space.count - delete_amount; + + if (shift_amount != 0){ + int new_count = stack->count + shift_amount; + if (new_count > stack->max_count){ + int new_max = LargeRoundUp(new_count, Kbytes(1)); + stack->tokens = (Cpp_Token*) + general_memory_reallocate(general, stack->tokens, + stack->count*sizeof(Cpp_Token), + new_max*sizeof(Cpp_Token), BUBBLE_TOKENS); + stack->max_count = new_max; + } + + int shift_size = stack->count - relex_end; + if (shift_size > 0){ + Cpp_Token *old_base = stack->tokens + relex_end; + memmove(old_base + shift_amount, old_base, + sizeof(Cpp_Token)*shift_size); + } + + stack->count += shift_amount; + } + + memcpy(state.stack->tokens + state.start_token_i, relex_space.tokens, + sizeof(Cpp_Token)*relex_space.count); + } + + end_temp_memory(temp); + } + + if (!inline_lex){ + i32 end_token_i = cpp_get_end_token(&file->token_stack, end_i); + cpp_shift_token_starts(&file->token_stack, end_token_i, amount); + --end_token_i; + if (end_token_i >= 0){ + Cpp_Token *token = file->token_stack.tokens + end_token_i; + if (token->start < end_i && token->start + token->size > end_i){ + token->size += amount; + } + } + + file->still_lexing = 1; + + Job_Data job; + job.callback = job_full_lex; + job.data[0] = file; + job.data[1] = general; + job.memory_request = Kbytes(64); + file->lex_job = system_post_job(BACKGROUND_THREADS, job); + } +} + +internal bool32 +file_grow_as_needed(General_Memory *general, Editing_File *file, i32 additional_size){ + bool32 result = 1; + i32 target_size = file->buffer.size + additional_size + 1; + if (target_size >= file->buffer.max){ + i32 request_size = LargeRoundUp(target_size*2, Kbytes(256)); + char *new_data = (char*) + general_memory_reallocate(general, file->buffer.data, file->buffer.size, request_size, BUBBLE_BUFFER); + if (new_data){ + new_data[file->buffer.size] = 0; + file->buffer.data = new_data; + file->buffer.max = request_size; + } + else{ + result = 0; + } + } + return result; +} + +internal void +undo_stack_grow_string(General_Memory *general, Edit_Stack *stack, i32 extra_size){ + i32 old_max = stack->max; + u8 *old_str = stack->strings; + i32 new_max = old_max*2 + extra_size; + u8 *new_str = (u8*) + general_memory_reallocate(general, old_str, old_max, new_max); + stack->strings = new_str; + stack->max = new_max; +} + +internal void +undo_stack_grow_edits(General_Memory *general, Edit_Stack *stack){ + i32 old_max = stack->edit_max; + Edit_Step *old_eds = stack->edits; + i32 new_max = old_max*2 + 2; + Edit_Step *new_eds = (Edit_Step*) + general_memory_reallocate(general, old_eds, old_max*sizeof(Edit_Step), new_max*sizeof(Edit_Step)); + stack->edits = new_eds; + stack->edit_max = new_max; +} + +internal void +child_stack_grow_string(General_Memory *general, Small_Edit_Stack *stack, i32 extra_size){ + i32 old_max = stack->max; + u8 *old_str = stack->strings; + i32 new_max = old_max*2 + extra_size; + u8 *new_str = (u8*) + general_memory_reallocate(general, old_str, old_max, new_max); + stack->strings = new_str; + stack->max = new_max; +} + +internal void +child_stack_grow_edits(General_Memory *general, Small_Edit_Stack *stack, i32 amount){ + i32 old_max = stack->edit_max; + Buffer_Edit *old_eds = stack->edits; + i32 new_max = old_max*2 + amount; + Buffer_Edit *new_eds = (Buffer_Edit*) + general_memory_reallocate(general, old_eds, old_max*sizeof(Buffer_Edit), new_max*sizeof(Buffer_Edit)); + stack->edits = new_eds; + stack->edit_max = new_max; +} + +internal i32 +undo_children_push(General_Memory *general, Small_Edit_Stack *children, + Buffer_Edit *edits, i32 edit_count, u8 *strings, i32 string_size){ + i32 result = children->edit_count; + if (children->edit_count + edit_count > children->edit_max) + child_stack_grow_edits(general, children, edit_count); + + if (children->size + string_size > children->max) + child_stack_grow_string(general, children, string_size); + + memcpy(children->edits + children->edit_count, edits, edit_count*sizeof(Buffer_Edit)); + memcpy(children->strings + children->size, strings, string_size); + + Buffer_Edit *edit = children->edits + children->edit_count; + i32 start_pos = children->size; + for (i32 i = 0; i < edit_count; ++i, ++edit){ + edit->str_start += start_pos; + } + + children->edit_count += edit_count; + children->size += string_size; + + return result; +} + +struct Edit_Spec{ + u8 *str; + Edit_Step step; +}; + +internal Edit_Step* +file_post_undo(General_Memory *general, Editing_File *file, + Edit_Step step, bool32 do_merge, bool32 can_merge){ + if (step.type == ED_NORMAL){ + file->undo.redo.size = 0; + file->undo.redo.edit_count = 0; + } + + Edit_Stack *undo = &file->undo.undo; + Edit_Step *result = 0; + + if (step.child_count == 0){ + if (step.edit.end - step.edit.start + undo->size > undo->max) + undo_stack_grow_string(general, undo, step.edit.end - step.edit.start); + + Buffer_Edit inv; + buffer_invert_edit(&file->buffer, step.edit, &inv, + (char*)undo->strings, &undo->size, undo->max); + + Edit_Step inv_step = {}; + inv_step.edit = inv; + inv_step.pre_pos = step.pre_pos; + inv_step.post_pos = step.post_pos; + inv_step.can_merge = (b8)can_merge; + inv_step.type = ED_UNDO; + + bool32 did_merge = 0; + if (do_merge && undo->edit_count > 0){ + Edit_Step prev = undo->edits[undo->edit_count-1]; + if (prev.can_merge && inv_step.edit.len == 0 && prev.edit.len == 0){ + if (prev.edit.end == inv_step.edit.start){ + did_merge = 1; + inv_step.edit.start = prev.edit.start; + inv_step.pre_pos = prev.pre_pos; + } + } + } + + if (did_merge){ + result = undo->edits + (undo->edit_count-1); + *result = inv_step; + } + else{ + if (undo->edit_count == undo->edit_max) + undo_stack_grow_edits(general, undo); + result = undo->edits + (undo->edit_count++); + *result = inv_step; + } + } + else{ + Edit_Step inv_step = {}; + inv_step.type = ED_UNDO; + inv_step.first_child = step.inverse_first_child; + inv_step.inverse_first_child = step.first_child; + inv_step.special_type = step.special_type; + inv_step.child_count = step.inverse_child_count; + inv_step.inverse_child_count = step.child_count; + + if (undo->edit_count == undo->edit_max) + undo_stack_grow_edits(general, undo); + result = undo->edits + (undo->edit_count++); + *result = inv_step; + } + + return result; +} + +inline void +undo_stack_pop(Edit_Stack *stack){ + if (stack->edit_count > 0){ + Edit_Step *edit = stack->edits + (--stack->edit_count); + stack->size -= edit->edit.len; + } +} + +internal void +file_post_redo(General_Memory *general, Editing_File *file, Edit_Step step){ + Edit_Stack *redo = &file->undo.redo; + + if (step.child_count == 0){ + if (step.edit.end - step.edit.start + redo->size > redo->max) + undo_stack_grow_string(general, redo, step.edit.end - step.edit.start); + + Buffer_Edit inv; + buffer_invert_edit(&file->buffer, step.edit, &inv, + (char*)redo->strings, &redo->size, redo->max); + + Edit_Step inv_step = {}; + inv_step.edit = inv; + inv_step.pre_pos = step.pre_pos; + inv_step.post_pos = step.post_pos; + inv_step.type = ED_REDO; + + if (redo->edit_count == redo->edit_max) + undo_stack_grow_edits(general, redo); + redo->edits[redo->edit_count++] = inv_step; + } + else{ + Edit_Step inv_step = {}; + inv_step.type = ED_REDO; + inv_step.first_child = step.inverse_first_child; + inv_step.inverse_first_child = step.first_child; + inv_step.special_type = step.special_type; + inv_step.child_count = step.inverse_child_count; + inv_step.inverse_child_count = step.child_count; + + if (redo->edit_count == redo->edit_max) + undo_stack_grow_edits(general, redo); + redo->edits[redo->edit_count++] = inv_step; + } +} + +inline void +file_post_history_block(Editing_File *file, i32 pos){ + Assert(file->undo.history_head_block < pos); + Assert(pos < file->undo.history.edit_count); + + Edit_Step *history = file->undo.history.edits; + Edit_Step *step = history + file->undo.history_head_block; + step->next_block = pos; + step = history + pos; + step->prev_block = file->undo.history_head_block; + file->undo.history_head_block = pos; + ++file->undo.history_block_count; +} + +inline void +file_unpost_history_block(Editing_File *file){ + Assert(file->undo.history_block_count > 1); + --file->undo.history_block_count; + Edit_Step *old_head = file->undo.history.edits + file->undo.history_head_block; + file->undo.history_head_block = old_head->prev_block; +} + +internal Edit_Step* +file_post_history(General_Memory *general, Editing_File *file, + Edit_Step step, bool32 do_merge, bool32 can_merge){ + Edit_Stack *history = &file->undo.history; + Edit_Step *result = 0; + + persist Edit_Type reverse_types[4]; + if (reverse_types[ED_UNDO] == 0){ + reverse_types[ED_NORMAL] = ED_REVERSE_NORMAL; + reverse_types[ED_REVERSE_NORMAL] = ED_NORMAL; + reverse_types[ED_UNDO] = ED_REDO; + reverse_types[ED_REDO] = ED_UNDO; + } + + if (step.child_count == 0){ + if (step.edit.end - step.edit.start + history->size > history->max) + undo_stack_grow_string(general, history, step.edit.end - step.edit.start); + + Buffer_Edit inv; + buffer_invert_edit(&file->buffer, step.edit, &inv, + (char*)history->strings, &history->size, history->max); + + Edit_Step inv_step = {}; + inv_step.edit = inv; + inv_step.pre_pos = step.pre_pos; + inv_step.post_pos = step.post_pos; + inv_step.can_merge = (b8)can_merge; + inv_step.type = reverse_types[step.type]; + + bool32 did_merge = 0; + if (do_merge && history->edit_count > 0){ + Edit_Step prev = history->edits[history->edit_count-1]; + if (prev.can_merge && inv_step.edit.len == 0 && prev.edit.len == 0){ + if (prev.edit.end == inv_step.edit.start){ + did_merge = 1; + inv_step.edit.start = prev.edit.start; + inv_step.pre_pos = prev.pre_pos; + } + } + } + + if (did_merge){ + result = history->edits + (history->edit_count-1); + } + else{ + if (history->edit_count == history->edit_max) + undo_stack_grow_edits(general, history); + result = history->edits + (history->edit_count++); + } + + *result = inv_step; + } + else{ + Edit_Step inv_step = {}; + inv_step.type = reverse_types[step.type]; + inv_step.first_child = step.inverse_first_child; + inv_step.inverse_first_child = step.first_child; + inv_step.special_type = step.special_type; + inv_step.inverse_child_count = step.child_count; + inv_step.child_count = step.inverse_child_count; + + if (history->edit_count == history->edit_max) + undo_stack_grow_edits(general, history); + result = history->edits + (history->edit_count++); + *result = inv_step; + } + + return result; +} + +inline Full_Cursor +view_compute_cursor_from_pos(File_View *view, i32 pos){ + Editing_File *file = view->file; + Style *style = view->style; + Font *font = style->font; + + real32 max_width = view_compute_width(view); + Opaque_Font_Advance opad = get_opaque_font_advance(font); + + return buffer_cursor_from_pos(&file->buffer, pos, view->line_wrap_y, + max_width, (real32)font->height, opad.data, opad.stride); +} + +inline Full_Cursor +view_compute_cursor_from_unwrapped_xy(File_View *view, real32 seek_x, real32 seek_y, + bool32 round_down = 0){ + Editing_File *file = view->file; + Style *style = view->style; + Font *font = style->font; + + real32 max_width = view_compute_width(view); + Opaque_Font_Advance opad = get_opaque_font_advance(font); + + return buffer_cursor_from_unwrapped_xy(&file->buffer, seek_x, seek_y, round_down, view->line_wrap_y, + max_width, (real32)font->height, opad.data, opad.stride); +} + +inline Full_Cursor +view_compute_cursor_from_wrapped_xy(File_View *view, real32 seek_x, real32 seek_y, + bool32 round_down = 0){ + Editing_File *file = view->file; + Style *style = view->style; + Font *font = style->font; + + real32 max_width = view_compute_width(view); + Opaque_Font_Advance opad = get_opaque_font_advance(font); + + return buffer_cursor_from_wrapped_xy(&file->buffer, seek_x, seek_y, round_down, view->line_wrap_y, + max_width, (real32)font->height, opad.data, opad.stride); +} + +inline Full_Cursor +view_compute_cursor_from_xy(File_View *view, real32 seek_x, real32 seek_y){ + Full_Cursor result; + if (view->unwrapped_lines) result = view_compute_cursor_from_unwrapped_xy(view, seek_x, seek_y); + else result = view_compute_cursor_from_wrapped_xy(view, seek_x, seek_y); + return result; +} + +inline void +view_set_temp_highlight(File_View *view, i32 pos, i32 end_pos){ + view->temp_highlight = view_compute_cursor_from_pos(view, pos); + view->temp_highlight_end_pos = end_pos; + view->show_temp_highlight = 1; +} + +inline i32 +view_get_cursor_pos(File_View *view){ + i32 result; + if (view->show_temp_highlight){ + result = view->temp_highlight.pos; + } + else{ + result = view->cursor.pos; + } + return result; +} + +inline real32 +view_get_cursor_x(File_View *view){ + real32 result; + Full_Cursor *cursor; + if (view->show_temp_highlight){ + cursor = &view->temp_highlight; + } + else{ + cursor = &view->cursor; + } + if (view->unwrapped_lines){ + result = cursor->unwrapped_x; + } + else{ + result = cursor->wrapped_x; + } + return result; +} + +inline real32 +view_get_cursor_y(File_View *view){ + Full_Cursor *cursor; + real32 result; + + if (view->show_temp_highlight) cursor = &view->temp_highlight; + else cursor = &view->cursor; + + if (view->unwrapped_lines) result = cursor->unwrapped_y; + else result = cursor->wrapped_y; + + return result; +} + +internal void +view_set_file(File_View *view, Editing_File *file, Style *style, + Custom_Command_Function *open_hook, void *cmd_context, Application_Links app){ + Panel *panel = view->view_base.panel; + view->file = file; + + General_Memory *general = &view->view_base.mem->general; + Font *font = style->font; + view->style = style; + view->font_advance = font->advance; + view->font_height = font->height; + + view_measure_wraps(general, view); + + view->cursor = {}; + view->cursor = view_compute_cursor_from_pos(view, file->cursor_pos); + + real32 cursor_x, cursor_y; + real32 w, h; + real32 target_x, target_y; + + cursor_x = view_get_cursor_x(view); + cursor_y = view_get_cursor_y(view); + + w = (real32)(panel->inner.x1 - panel->inner.x0); + h = (real32)(panel->inner.y1 - panel->inner.y0); + + target_x = 0; + if (cursor_x < target_x){ + target_x = (real32)Max(0, cursor_x - w*.5f); + } + else if (cursor_x >= target_x + w){ + target_x = (real32)(cursor_x - w*.5f); + } + + target_y = (real32)FLOOR32(cursor_y - h*.5f); + if (target_y < 0) target_y = 0; + + view->target_x = target_x; + view->target_y = target_y; + view->scroll_x = target_x; + view->scroll_y = target_y; + + view->vel_y = 1.f; + view->vel_x = 1.f; + + if (open_hook) open_hook(cmd_context, app); +} + +struct Relative_Scrolling{ + real32 scroll_x, scroll_y; + real32 target_x, target_y; +}; + +internal Relative_Scrolling +view_get_relative_scrolling(File_View *view){ + Relative_Scrolling result; + real32 cursor_x, cursor_y; + cursor_x = view_get_cursor_x(view); + cursor_y = view_get_cursor_y(view); + result.scroll_x = cursor_x - view->scroll_x; + result.scroll_y = cursor_y - view->scroll_y; + result.target_x = cursor_x - view->target_x; + result.target_y = cursor_y - view->target_y; + return result; +} + +internal void +view_set_relative_scrolling(File_View *view, Relative_Scrolling scrolling){ + real32 cursor_x, cursor_y; + cursor_x = view_get_cursor_x(view); + cursor_y = view_get_cursor_y(view); + view->scroll_y = cursor_y - scrolling.scroll_y; + view->target_y = cursor_y - scrolling.target_y; + if (view->scroll_y < 0) view->scroll_y = 0; + if (view->target_y < 0) view->target_y = 0; +} + +inline void +view_cursor_move(File_View *view, Full_Cursor cursor){ + view->cursor = cursor; + view->preferred_x = view_get_cursor_x(view); + view->file->cursor_pos = view->cursor.pos; + view->show_temp_highlight = 0; +} + +inline void +view_cursor_move(File_View *view, i32 pos){ + view->cursor = view_compute_cursor_from_pos(view, pos); + view->preferred_x = view_get_cursor_x(view); + view->file->cursor_pos = view->cursor.pos; + view->show_temp_highlight = 0; +} + +inline void +view_cursor_move(File_View *view, real32 x, real32 y, bool32 round_down = 0){ + if (view->unwrapped_lines){ + view->cursor = view_compute_cursor_from_unwrapped_xy(view, x, y, round_down); + } + else{ + view->cursor = view_compute_cursor_from_wrapped_xy(view, x, y, round_down); + } + view->preferred_x = view_get_cursor_x(view); + view->file->cursor_pos = view->cursor.pos; + view->show_temp_highlight = 0; +} + +inline void +view_set_widget(File_View *view, File_View_Widget_Type type){ + view->widget.type = type; +} + +inline i32 +view_widget_height(File_View *view, Font *font){ + i32 result = 0; + switch (view->widget.type){ + case FWIDG_NONE: 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; + } + return result; +} + +inline i32_Rect +view_widget_rect(File_View *view, Font *font){ + Panel *panel = view->view_base.panel; + i32_Rect whole = panel->inner; + i32_Rect result; + result.x0 = whole.x0; + result.x1 = whole.x1; + result.y0 = whole.y0 + font->height + 2; + result.y1 = result.y0 + view_widget_height(view, font); + + return result; +} + +#if FRED_SLOW +inline b32 +debug_edit_step_check(Edit_Step a, Edit_Step b){ + Assert(a.type == b.type); + Assert(a.can_merge == b.can_merge); + Assert(a.pre_pos == b.pre_pos); + Assert(a.post_pos == b.post_pos); + Assert(a.edit.start == b.edit.start); + Assert(a.edit.end == b.edit.end); + Assert(a.edit.len == b.edit.len); + return 1; +} +#endif + +enum History_Mode{ + hist_normal, + hist_backward, + hist_forward +}; + +internal void +view_update_history_before_edit(Mem_Options *mem, Editing_File *file, Edit_Step step, u8 *str, + History_Mode history_mode, i32 next_cursor){ + General_Memory *general = &mem->general; + +#if FRED_SLOW + if (history_mode == hist_backward) + debug_edit_step_check(step, file->undo.history.edits[file->undo.edit_history_cursor]); + else if (history_mode == hist_forward) + debug_edit_step_check(step, file->undo.history.edits[file->undo.history.edit_count]); + switch (step.type){ + case ED_UNDO: + { + Assert(file->undo.undo.edit_count > 0); + debug_edit_step_check(step, file->undo.undo.edits[file->undo.undo.edit_count-1]); + }break; + case ED_REDO: + { + Assert(file->undo.redo.edit_count > 0); + debug_edit_step_check(step, file->undo.redo.edits[file->undo.redo.edit_count-1]); + }break; + } +#endif + + bool32 can_merge = 0, do_merge = 0; + switch (step.type){ + case ED_NORMAL: + { + if (step.edit.len == 1 && str && char_is_alpha_numeric(*str)) can_merge = 1; + if (step.edit.len == 1 && str && (can_merge || char_is_whitespace(*str))) do_merge = 1; + + Edit_Step *new_step = 0; + if (history_mode != hist_forward){ + new_step = file_post_history(general, file, step, do_merge, can_merge); + new_step->post_pos = next_cursor; + } + + new_step = file_post_undo(general, file, step, do_merge, can_merge); + new_step->post_pos = next_cursor; + }break; + + case ED_REVERSE_NORMAL: + { + if (history_mode != hist_forward) + file_post_history(general, file, step, do_merge, can_merge); + + undo_stack_pop(&file->undo.undo); + + bool32 restore_redos = 0; + Edit_Step *redo_end = 0; + + if (history_mode == hist_backward && file->undo.edit_history_cursor > 0){ + restore_redos = 1; + redo_end = file->undo.history.edits + (file->undo.edit_history_cursor - 1); + } + else if (history_mode == hist_forward && file->undo.history.edit_count > 0){ + restore_redos = 1; + redo_end = file->undo.history.edits + (file->undo.history.edit_count - 1); + } + + if (restore_redos){ + Edit_Step *redo_start = redo_end; + i32 steps_of_redo = 0; + i32 strings_of_redo = 0; + i32 undo_count = 0; + while (redo_start->type == ED_REDO || redo_start->type == ED_UNDO){ + if (redo_start->type == ED_REDO){ + if (undo_count > 0) --undo_count; + else{ + ++steps_of_redo; + strings_of_redo += redo_start->edit.len; + } + } + else{ + ++undo_count; + } + --redo_start; + } + + if (redo_start < redo_end){ + ++redo_start; + ++redo_end; + + if (file->undo.redo.edit_count + steps_of_redo > file->undo.redo.edit_max) + undo_stack_grow_edits(general, &file->undo.redo); + + if (file->undo.redo.size + strings_of_redo > file->undo.redo.max) + undo_stack_grow_string(general, &file->undo.redo, strings_of_redo); + + u8 *str_src = file->undo.history.strings + redo_end->edit.str_start; + u8 *str_dest_base = file->undo.redo.strings; + i32 str_redo_pos = file->undo.redo.size + strings_of_redo; + + Edit_Step *edit_src = redo_end; + Edit_Step *edit_dest = file->undo.redo.edits + file->undo.redo.edit_count + steps_of_redo; + + i32 undo_count = 0; + for (i32 i = 0; i < steps_of_redo;){ + --edit_src; + str_src -= edit_src->edit.len; + if (edit_src->type == ED_REDO){ + if (undo_count > 0){ + --undo_count; + } + else{ + ++i; + + --edit_dest; + *edit_dest = *edit_src; + + str_redo_pos -= edit_dest->edit.len; + edit_dest->edit.str_start = str_redo_pos; + + memcpy(str_dest_base + str_redo_pos, str_src, edit_dest->edit.len); + } + } + else{ + ++undo_count; + } + } + Assert(undo_count == 0); + + file->undo.redo.size += strings_of_redo; + file->undo.redo.edit_count += steps_of_redo; + } + } + }break; + + case ED_UNDO: + { + if (history_mode != hist_forward) + file_post_history(general, file, step, do_merge, can_merge); + file_post_redo(general, file, step); + undo_stack_pop(&file->undo.undo); + }break; + + case ED_REDO: + { + if (step.edit.len == 1 && str && char_is_alpha_numeric(*str)) can_merge = 1; + if (step.edit.len == 1 && str && (can_merge || char_is_whitespace(*str))) do_merge = 1; + + if (history_mode != hist_forward) + file_post_history(general, file, step, do_merge, can_merge); + + file_post_undo(general, file, step, do_merge, can_merge); + undo_stack_pop(&file->undo.redo); + }break; + } + + if (history_mode != hist_forward){ + if (step.type == ED_UNDO || step.type == ED_REDO){ + if (file->undo.current_block_normal){ + file_post_history_block(file, file->undo.history.edit_count - 1); + file->undo.current_block_normal = 0; + } + } + else{ + if (!file->undo.current_block_normal){ + file_post_history_block(file, file->undo.history.edit_count - 1); + file->undo.current_block_normal = 1; + } + } + } + else{ + if (file->undo.history_head_block == file->undo.history.edit_count){ + file_unpost_history_block(file); + file->undo.current_block_normal = !file->undo.current_block_normal; + } + } + + if (history_mode == hist_normal) file->undo.edit_history_cursor = file->undo.history.edit_count; +} + +inline b32 +debug_step_match(Edit_Step a, Edit_Step b){ + Assert(a.type == b.type); + Assert(a.can_merge == b.can_merge); + Assert(a.pre_pos == b.pre_pos); + Assert(a.post_pos == b.post_pos); + Assert(a.next_block == b.next_block); + Assert(a.prev_block == b.prev_block); + Assert(a.edit.start == b.edit.start); + Assert(a.edit.end == b.edit.end); + Assert(a.edit.len == b.edit.len); + return 1; +} + +#if 1 +inline void +file_pre_edit_maintenance(Editing_File *file){ + if (file->still_lexing) + system_cancel_job(BACKGROUND_THREADS, file->lex_job); + + file->last_4ed_edit_time = system_get_now(); +} + +internal void +view_do_single_edit(Mem_Options *mem, File_View *view, Editing_File *file, + Editing_Layout *layout, Edit_Spec spec, History_Mode history_mode, i32 next_cursor){ + Assert(file); + ProfileMomentFunction(); + + // NOTE(allen): fixing stuff beforewards???? + view_update_history_before_edit(mem, file, spec.step, spec.str, history_mode, next_cursor); + file_pre_edit_maintenance(file); + + // NOTE(allen): actual text replacement + General_Memory *general = &mem->general; + + i32 start = spec.step.edit.start; + i32 end = spec.step.edit.end; + char *str = (char*)spec.str; + i32 str_len = spec.step.edit.len; + + i32 shift_amount; + while (buffer_replace_range(&file->buffer, start, end, str, str_len, &shift_amount)) + file_grow_as_needed(general, file, shift_amount); + + // NOTE(allen): fixing stuff afterwards + if (file->tokens_exist) + file_relex_parallel(mem, file, start, end, shift_amount); + + i32 line_start = buffer_get_line_index(&file->buffer, start, 0, file->buffer.line_count); + i32 line_end = buffer_get_line_index(&file->buffer, end, 0, file->buffer.line_count); + i32 replaced_line_count = line_end - line_start; + + // TODO(allen): check this + i32 new_line_count = file_count_newlines(file, start, start+str_len); + + + i32 line_shift = new_line_count - replaced_line_count; + + file_remeasure_starts(general, file, line_start, line_end, line_shift, shift_amount); + file_remeasure_widths(general, file, file->font, line_start, line_end, line_shift); + + Temp_Memory cursor_temp = begin_temp_memory(&mem->part); + i32 panel_count = layout->panel_count; + i32 cursor_max = layout->panel_max_count * 2; + Cursor_With_Index *cursors = push_array(&mem->part, Cursor_With_Index, cursor_max); + + i32 cursor_count = 0; + Panel *current_panel = layout->panels; + for (i32 i = 0; i < panel_count; ++i, ++current_panel){ + File_View *current_view = view_to_file_view(current_panel->view); + if (current_view && current_view->file == file){ + view_measure_wraps(general, current_view); + if (current_view != view){ + write_cursor_with_index(cursors, &cursor_count, current_view->cursor.pos); + write_cursor_with_index(cursors, &cursor_count, current_view->mark); + } + } + } + + if (cursor_count > 0){ + buffer_sort_cursors(cursors, cursor_count); + buffer_update_cursors(cursors, cursor_count, + start, end, shift_amount + (end - start)); + buffer_unsort_cursors(cursors, cursor_count); + } + + cursor_count = 0; + current_panel = layout->panels; + for (i32 i = 0; i < panel_count; ++i, ++current_panel){ + File_View *current_view = view_to_file_view(current_panel->view); + if (current_view && current_view->file == file){ + if (current_view != view){ + view_cursor_move(current_view, cursors[cursor_count++].pos); + current_view->mark = cursors[cursor_count++].pos; + current_view->preferred_x = view_get_cursor_x(current_view); + } + } + } + + end_temp_memory(cursor_temp); +} + +internal void +view_do_white_batch_edit(Mem_Options *mem, File_View *view, Editing_File *file, + Editing_Layout *layout, Edit_Spec spec, History_Mode history_mode){ + Assert(file); + ProfileMomentFunction(); + + // NOTE(allen): fixing stuff beforewards???? + Assert(spec.str == 0); + view_update_history_before_edit(mem, file, spec.step, 0, history_mode, 0); + file_pre_edit_maintenance(file); + + // NOTE(allen): actual text replacement + General_Memory *general = &mem->general; + Partition *part = &mem->part; + + u8 *str_base = file->undo.children.strings; + i32 batch_size = spec.step.child_count; + Buffer_Edit *batch = file->undo.children.edits + spec.step.first_child; + + Assert(spec.step.first_child < file->undo.children.edit_count); + Assert(batch_size >= 0); + + i32 shift_amount = buffer_batch_edit_max_shift(batch, batch_size); + file_grow_as_needed(general, file, shift_amount); + + buffer_batch_edit(&file->buffer, batch, (char*)str_base, batch_size); + + // NOTE(allen): token fixing + if (file->tokens_complete){ + Cpp_Token_Stack tokens = file->token_stack; + Cpp_Token *token = tokens.tokens; + Cpp_Token *end_token = tokens.tokens + tokens.count; + + Buffer_Edit *edit = batch; + Buffer_Edit *end_edit = batch + batch_size; + + i32 shift_amount = 0; + + for (; token < end_token && edit < end_edit; ++edit){ + for (; token->start < edit->start && token < end_token; ++token){ + token->start += shift_amount; + } + shift_amount += (edit->len - (edit->end - edit->start)); + } + for (; token < end_token; ++token){ + token->start += shift_amount; + } + } + + // NOTE(allen): meta data + { + Buffer_Measure_Starts state = {}; + if (buffer_measure_starts(&state, &file->buffer)) Assert(0); + + Opaque_Font_Advance opad = get_opaque_font_advance(file->font); + buffer_measure_widths(&file->buffer, opad.data, opad.stride); + } + + // NOTE(allen): cursor fixing + { + Temp_Memory cursor_temp = begin_temp_memory(part); + i32 cursor_max = layout->panel_max_count * 2; + Cursor_With_Index *cursors = push_array(part, Cursor_With_Index, cursor_max); + + i32 panel_count = layout->panel_count; + i32 cursor_count = 0; + Panel *current_panel = layout->panels; + for (i32 i = 0; i < panel_count; ++i, ++current_panel){ + File_View *current_view = view_to_file_view(current_panel->view); + if (current_view && current_view->file == file){ + view_measure_wraps(general, current_view); + write_cursor_with_index(cursors, &cursor_count, current_view->cursor.pos); + write_cursor_with_index(cursors, &cursor_count, current_view->mark); + } + } + + if (cursor_count > 0){ + buffer_sort_cursors(cursors, cursor_count); + buffer_batch_edit_update_cursors(cursors, cursor_count, batch, batch_size); + buffer_unsort_cursors(cursors, cursor_count); + } + + cursor_count = 0; + current_panel = layout->panels; + for (i32 i = 0; i < panel_count; ++i, ++current_panel){ + File_View *current_view = view_to_file_view(current_panel->view); + if (current_view && current_view->file == file){ + view_cursor_move(current_view, cursors[cursor_count++].pos); + current_view->mark = cursors[cursor_count++].pos; + current_view->preferred_x = view_get_cursor_x(current_view); + } + } + end_temp_memory(cursor_temp); + } +} + +inline void +view_replace_range(Mem_Options *mem, File_View *view, Editing_Layout *layout, + i32 start, i32 end, u8 *str, i32 len, i32 next_cursor){ + Edit_Spec spec = {}; + spec.step.type = ED_NORMAL; + spec.step.edit.start = start; + spec.step.edit.end = end; + spec.step.edit.len = len; + spec.str = str; + view_do_single_edit(mem, view, view->file, layout, spec, hist_normal, next_cursor); +} + +internal void +view_post_paste_effect(File_View *view, i32 ticks, i32 start, i32 size, u32 color){ + view->paste_effect.start = start; + view->paste_effect.end = start + size; + view->paste_effect.color = color; + view->paste_effect.tick_down = ticks; + view->paste_effect.tick_max = ticks; +} + +internal void +view_undo_redo(Mem_Options *mem, Editing_Layout *layout, File_View *view, Editing_File *file, + Edit_Stack *stack, Edit_Type expected_type){ + if (file && stack->edit_count > 0){ + Edit_Step step = stack->edits[stack->edit_count-1]; + + Assert(step.type == expected_type); + + Edit_Spec spec; + spec.step = step; + + if (step.child_count == 0){ + spec.step.edit.str_start = 0; + spec.str = stack->strings + step.edit.str_start; + + view_do_single_edit(mem, view, file, layout, spec, hist_normal, 0); + + view_cursor_move(view, step.pre_pos); + view->mark = view->cursor.pos; + + view_post_paste_effect(view, 10, step.edit.start, step.edit.len, + view->style->main.undo_color); + } + else{ + TentativeAssert(spec.step.special_type == 1); + view_do_white_batch_edit(mem, view, file, layout, spec, hist_normal); + } + } +} + +inline void +view_undo(Mem_Options *mem, Editing_Layout *layout, File_View *view){ + Editing_File *file = view->file; + view_undo_redo(mem, layout, view, file, &file->undo.undo, ED_UNDO); +} + +inline void +view_redo(Mem_Options *mem, Editing_Layout *layout, File_View *view){ + Editing_File *file = view->file; + view_undo_redo(mem, layout, view, file, &file->undo.redo, ED_REDO); +} + +internal void +view_history_step(Mem_Options *mem, Editing_Layout *layout, File_View *view, History_Mode history_mode){ + Assert(history_mode != hist_normal); + + Editing_File *file = view->file; + bool32 do_history_step = 0; + Edit_Step step = {}; + if (history_mode == hist_backward){ + if (file->undo.edit_history_cursor > 0){ + do_history_step = 1; + step = file->undo.history.edits[--file->undo.edit_history_cursor]; + } + } + else{ + if (file->undo.edit_history_cursor < file->undo.history.edit_count){ + Assert(((file->undo.history.edit_count - file->undo.edit_history_cursor) & 1) == 0); + step = file->undo.history.edits[--file->undo.history.edit_count]; + file->undo.history.size -= step.edit.len; + ++file->undo.edit_history_cursor; + do_history_step = 1; + } + } + + if (do_history_step){ + Edit_Spec spec; + spec.step = step; + + if (spec.step.child_count == 0){ + spec.step.edit.str_start = 0; + spec.str = file->undo.history.strings + step.edit.str_start; + + view_do_single_edit(mem, view, file, layout, spec, history_mode, step.post_pos); + + switch (spec.step.type){ + case ED_NORMAL: + case ED_REDO: + view_cursor_move(view, step.post_pos); + break; + + case ED_REVERSE_NORMAL: + case ED_UNDO: + view_cursor_move(view, step.pre_pos); + break; + } + view->mark = view->cursor.pos; + } + else{ + TentativeAssert(spec.step.special_type == 1); + view_do_white_batch_edit(mem, view, file, layout, spec, history_mode); + } + } +} + +#endif + +// TODO(allen): should these still be view operations? +internal i32 +view_find_end_of_line(File_View *view, i32 pos){ + Editing_File *file = view->file; + char *data = file->buffer.data; + while (pos < file->buffer.size && data[pos] != '\n') ++pos; + if (pos > file->buffer.size) pos = file->buffer.size; + return pos; +} + +internal i32 +view_find_beginning_of_line(File_View *view, i32 pos){ + Editing_File *file = view->file; + char *data = file->buffer.data; + if (pos > 0){ + --pos; + while (pos > 0 && data[pos] != '\n') --pos; + if (pos != 0) ++pos; + } + return pos; +} + +internal i32 +view_find_beginning_of_next_line(File_View *view, i32 pos){ + Editing_File *file = view->file; + char *data = file->buffer.data; + while (pos < file->buffer.size && + !starts_new_line(data[pos])){ + ++pos; + } + if (pos < file->buffer.size){ + ++pos; + } + return pos; +} + +internal String* +working_set_next_clipboard_string(General_Memory *general, Working_Set *working, i32 str_size){ + String *result = 0; + i32 clipboard_current = working->clipboard_current; + if (working->clipboard_size == 0){ + clipboard_current = 0; + working->clipboard_size = 1; + } + else{ + ++clipboard_current; + if (clipboard_current >= working->clipboard_max_size){ + clipboard_current = 0; + } + else if (working->clipboard_size <= clipboard_current){ + working->clipboard_size = clipboard_current+1; + } + } + result = &working->clipboards[clipboard_current]; + working->clipboard_current = clipboard_current; + working->clipboard_rolling = clipboard_current; + char *new_str; + if (result->str){ + new_str = (char*)general_memory_reallocate(general, result->str, result->size, str_size); + } + else{ + new_str = (char*)general_memory_allocate(general, str_size+1); + } + // TODO(allen): What if new_str == 0? + *result = make_string(new_str, 0, str_size); + return result; +} + +internal String* +working_set_clipboard_head(Working_Set *working){ + String *result = 0; + if (working->clipboard_size > 0){ + i32 clipboard_index = working->clipboard_current; + working->clipboard_rolling = clipboard_index; + result = &working->clipboards[clipboard_index]; + } + return result; +} + +internal String* +working_set_clipboard_roll_down(Working_Set *working){ + String *result = 0; + if (working->clipboard_size > 0){ + i32 clipboard_index = working->clipboard_rolling; + --clipboard_index; + if (clipboard_index < 0){ + clipboard_index = working->clipboard_size-1; + } + working->clipboard_rolling = clipboard_index; + result = &working->clipboards[clipboard_index]; + } + return result; +} + +inline Editing_File* +working_set_contains(Working_Set *working, String filename){ + Editing_File *result = 0; + i32 index; + if (table_find(&working->table, filename, &index)){ + result = working->files + index; + } + return result; +} + +// TODO(allen): Find a way to choose an ordering for these so it picks better first options. +internal Editing_File* +working_set_lookup_file(Working_Set *working_set, String string){ + Editing_File *file = working_set_contains(working_set, string); + + if (!file){ + i32 file_i; + i32 end = working_set->file_index_count; + file = working_set->files; + for (file_i = 0; file_i < end; ++file_i, ++file){ + if (file->live_name.str && (string.size == 0 || has_substr(file->live_name, string))){ + break; + } + } + if (file_i == end) file = 0; + } + + return file; +} + +internal void +clipboard_copy(General_Memory *general, Working_Set *working, u8 *data, Range range){ + i32 size = range.end - range.start; + String *dest = working_set_next_clipboard_string(general, working, size); + copy(dest, make_string((char*)data + range.start, size)); + system_post_clipboard(*dest); +} + +enum Endline_Convert_Type{ + ENDLINE_RN, + ENDLINE_N, + ENDLINE_R, + ENDLINE_ERASE, +}; + +internal void +view_clean_whitespace(Mem_Options *mem, File_View *view, Editing_Layout *layout){ + Editing_File *file = view->file; + Assert(file && !file->is_dummy); + Partition *part = &mem->part; + i32 line_count = file->buffer.line_count; + i32 edit_max = line_count * 2; + i32 edit_count = 0; + + Temp_Memory temp = begin_temp_memory(part); + Buffer_Edit *edits = push_array(part, Buffer_Edit, edit_max); + + char *str_base = (char*)part->base + part->pos; + i32 str_size = 0; + for (i32 line_i = 0; line_i < line_count; ++line_i){ + i32 start = file->buffer.line_starts[line_i]; + i32 preferred_indentation; + bool32 all_whitespace = 0; + bool32 all_space = 0; + i32 hard_start = + buffer_find_hard_start(&file->buffer, start, &all_whitespace, &all_space, &preferred_indentation, 4); + + if (all_whitespace) preferred_indentation = 0; + + if ((all_whitespace && hard_start > start) || !all_space){ + Buffer_Edit new_edit; + new_edit.str_start = str_size; + str_size += preferred_indentation; + char *str = push_array(part, char, preferred_indentation); + for (i32 j = 0; j < preferred_indentation; ++j) str[j] = ' '; + new_edit.len = preferred_indentation; + new_edit.start = start; + new_edit.end = hard_start; + edits[edit_count++] = new_edit; + } + Assert(edit_count <= edit_max); + } + + if (edit_count > 0){ + Assert(buffer_batch_debug_sort_check(edits, edit_count)); + + // NOTE(allen): computing edit spec, doing batch edit + Buffer_Edit *inverse_array = push_array(part, Buffer_Edit, edit_count); + Assert(inverse_array); + + char *inv_str = (char*)part->base + part->pos; + i32 inv_str_pos = 0; + Buffer_Invert_Batch state = {}; + if (buffer_invert_batch(&state, &file->buffer, edits, edit_count, + inverse_array, inv_str, &inv_str_pos, part->max - part->pos)) + Assert(0); + + General_Memory *general = &mem->general; + i32 first_child = + undo_children_push(general, &file->undo.children, + edits, edit_count, (u8*)(str_base), str_size); + i32 inverse_first_child = + undo_children_push(general, &file->undo.children, + inverse_array, edit_count, (u8*)(inv_str), inv_str_pos); + + Edit_Spec spec = {}; + spec.step.type = ED_NORMAL; + spec.step.first_child = first_child; + spec.step.inverse_first_child = inverse_first_child; + spec.step.special_type = 1; + spec.step.child_count = edit_count; + spec.step.inverse_child_count = edit_count; + + view_do_white_batch_edit(mem, view, file, layout, spec, hist_normal); + } + + end_temp_memory(temp); +} + +internal u32* +style_get_color(Style *style, Cpp_Token token){ + u32 *result; + if (token.flags & CPP_TFLAG_IS_KEYWORD){ + if (token.type == CPP_TOKEN_BOOLEAN_CONSTANT){ + result = &style->main.bool_constant_color; + } + else{ + result = &style->main.keyword_color; + } + } + else if(token.flags & CPP_TFLAG_PP_DIRECTIVE){ + result = &style->main.preproc_color; + } + else{ + switch (token.type){ + case CPP_TOKEN_COMMENT: + result = &style->main.comment_color; + break; + + case CPP_TOKEN_STRING_CONSTANT: + result = &style->main.str_constant_color; + break; + + case CPP_TOKEN_CHARACTER_CONSTANT: + result = &style->main.char_constant_color; + break; + + case CPP_TOKEN_INTEGER_CONSTANT: + result = &style->main.int_constant_color; + break; + + case CPP_TOKEN_FLOATING_CONSTANT: + result = &style->main.float_constant_color; + break; + + case CPP_TOKEN_INCLUDE_FILE: + result = &style->main.include_color; + break; + + default: + result = &style->main.default_color; + } + } + return result; +} + +internal bool32 +smooth_camera_step(real32 *target, real32 *current, real32 *vel, real32 S, real32 T){ + bool32 result = 0; + real32 targ = *target; + real32 curr = *current; + real32 v = *vel; + if (curr != targ){ + if (curr > targ - .1f && curr < targ + .1f){ + curr = targ; + v = 1.f; + } + else{ + real32 L = lerp(curr, T, targ); + + i32 sign = (targ > curr) - (targ < curr); + real32 V = curr + sign*v; + + if (sign > 0) curr = Min(L, V); + else curr = Max(L, V); + + if (curr == V){ + v *= S; + } + } + + *target = targ; + *current = curr; + *vel = v; + result = 1; + } + return result; +} + +inline real32 +view_compute_max_target_y(i32 lowest_line, i32 line_height, real32 view_height){ + real32 max_target_y = ((lowest_line+.5f)*line_height) - view_height*.5f; + return max_target_y; +} + +internal real32 +view_compute_max_target_y(File_View *view){ + i32 lowest_line = view_compute_lowest_line(view); + i32 line_height = view->style->font->height; + real32 view_height = view_compute_height(view); + real32 max_target_y = view_compute_max_target_y( + lowest_line, line_height, view_height); + return max_target_y; +} + +internal void +remeasure_file_view(View *view_, i32_Rect rect){ + File_View *view = (File_View*)view_; + Relative_Scrolling relative = view_get_relative_scrolling(view); + view_measure_wraps(&view->view_base.mem->general, view); + view_cursor_move(view, view->cursor.pos); + view->preferred_x = view_get_cursor_x(view); + view_set_relative_scrolling(view, relative); +} + +internal bool32 +do_button(i32 id, UI_State *state, UI_Layout *layout, char *text, i32 height_mult, + bool32 is_toggle = 0, bool32 on = 0){ + bool32 result = 0; + Font *font = state->font; + i32 character_h = font->height; + + i32_Rect btn_rect = layout_rect(layout, character_h * height_mult); + if (height_mult > 1) btn_rect = get_inner_rect(btn_rect, 2); + else{ + btn_rect.x0 += 2; + btn_rect.x1 -= 2; + } + + Widget_ID wid = make_id(state, id); + + if (state->input_stage){ + if (ui_do_button_input(state, btn_rect, wid, 0)){ + result = 1; + } + } + else{ + Render_Target *target = state->target; + UI_Style ui_style = get_ui_style(state->style); + u32 back, fore, outline; + outline = ui_style.bright; + get_colors(state, &back, &fore, wid, ui_style); + + draw_rectangle(target, btn_rect, back); + draw_rectangle_outline(target, btn_rect, outline); + real32 text_width = font_string_width(font, text); + i32 box_width = btn_rect.x1 - btn_rect.x0; + i32 box_height = btn_rect.y1 - btn_rect.y0; + i32 x_pos = TRUNC32(btn_rect.x0 + (box_width - text_width)*.5f); + draw_string(target, font, text, x_pos, btn_rect.y0 + (box_height - character_h) / 2, fore); + + if (is_toggle){ + i32_Rect on_box = get_inner_rect(btn_rect, character_h/2); + on_box.x1 = on_box.x0 + (on_box.y1 - on_box.y0); + + if (on) draw_rectangle(target, on_box, fore); + else draw_rectangle(target, on_box, back); + draw_rectangle_outline(target, on_box, fore); + } + } + + return result; +} + +internal bool32 +do_undo_slider(Widget_ID wid, UI_State *state, UI_Layout *layout, i32 max, i32 v, Undo_Data *undo, i32 *out){ + bool32 result = 0; + Font *font = state->font; + i32 character_h = font->height; + + i32_Rect containing_rect = layout_rect(layout, character_h); + + i32_Rect click_rect; + click_rect.x0 = containing_rect.x0 + character_h - 1; + click_rect.x1 = containing_rect.x1 - character_h + 1; + click_rect.y0 = containing_rect.y0 + 2; + click_rect.y1 = containing_rect.y1 - 2; + + if (state->input_stage){ + real32 l; + if (ui_do_slider_input(state, click_rect, wid, (real32)click_rect.x0, (real32)click_rect.x1, &l)){ + real32 v_new = lerp(0.f, l, (real32)max); + v = ROUND32(v_new); + result = 1; + if (out) *out = v; + } + } + else{ + if (max > 0){ + Render_Target *target = state->target; + UI_Style ui_style = get_ui_style_upper(state->style); + + real32 L = unlerp(0.f, (real32)v, (real32)max); + i32 x = FLOOR32(lerp((real32)click_rect.x0, L, (real32)click_rect.x1)); + + i32 bar_top = ((click_rect.y0 + click_rect.y1) >> 1) - 1; + i32 bar_bottom = bar_top + 2; + + bool32 show_bar = 1; + real32 tick_step = (click_rect.x1 - click_rect.x0) / (real32)max; + bool32 show_ticks = 1; + if (tick_step <= 5.f) show_ticks = 0; + + if (undo == 0){ + if (show_bar){ + i32_Rect slider_rect; + slider_rect.x0 = click_rect.x0; + slider_rect.x1 = x; + slider_rect.y0 = bar_top; + slider_rect.y1 = bar_bottom; + + draw_rectangle(target, slider_rect, ui_style.dim); + + slider_rect.x0 = x; + slider_rect.x1 = click_rect.x1; + draw_rectangle(target, slider_rect, ui_style.pop1); + } + + if (show_ticks){ + real32_Rect tick; + tick.x0 = (real32)click_rect.x0 - 1; + tick.x1 = (real32)click_rect.x0 + 1; + tick.y0 = (real32)bar_top - 3; + tick.y1 = (real32)bar_bottom + 3; + + for (i32 i = 0; i < v; ++i){ + draw_rectangle(target, tick, ui_style.dim); + tick.x0 += tick_step; + tick.x1 += tick_step; + } + + for (i32 i = v; i <= max; ++i){ + draw_rectangle(target, tick, ui_style.pop1); + tick.x0 += tick_step; + tick.x1 += tick_step; + } + } + } + else{ + if (show_bar){ + i32_Rect slider_rect; + slider_rect.x0 = click_rect.x0; + slider_rect.y0 = bar_top; + slider_rect.y1 = bar_bottom; + + Edit_Step *history = undo->history.edits; + i32 block_count = undo->history_block_count; + Edit_Step *step = history; + for (i32 i = 0; i < block_count; ++i){ + u32 color; + if (step->type == ED_REDO || + step->type == ED_UNDO) color = ui_style.pop1; + else color = ui_style.dim; + + real32 L; + if (i + 1 == block_count){ + L = 1.f; + }else{ + step = history + step->next_block; + L = unlerp(0.f, (real32)(step - history), (real32)max); + } + if (L > 1.f) L = 1.f; + i32 x = FLOOR32(lerp((real32)click_rect.x0, L, (real32)click_rect.x1)); + + slider_rect.x1 = x; + draw_rectangle(target, slider_rect, color); + slider_rect.x0 = slider_rect.x1; + + if (L == 1.f) break; + } + } + + if (show_ticks){ + real32_Rect tick; + tick.x0 = (real32)click_rect.x0 - 1; + tick.x1 = (real32)click_rect.x0 + 1; + tick.y0 = (real32)bar_top - 3; + tick.y1 = (real32)bar_bottom + 3; + + Edit_Step *history = undo->history.edits; + u32 color = ui_style.dim; + for (i32 i = 0; i <= max; ++i){ + if (i != max){ + if (history[i].type == ED_REDO) color = ui_style.pop1; + else if (history[i].type == ED_UNDO || + history[i].type == ED_NORMAL) color = ui_style.pop2; + else color = ui_style.dim; + } + draw_rectangle(target, tick, color); + tick.x0 += tick_step; + tick.x1 += tick_step; + } + } + } + + i32_Rect slider_handle; + slider_handle.x0 = x - 2; + slider_handle.x1 = x + 2; + slider_handle.y0 = click_rect.y0; + slider_handle.y1 = click_rect.y1; + + draw_rectangle(target, slider_handle, ui_style.bright); + } + } + + return result; +} + +internal i32 +step_file_view(Thread_Context *thread, View *view_, i32_Rect rect, + bool32 is_active, Input_Summary *user_input){ + view_->mouse_cursor_type = APP_MOUSE_CURSOR_IBEAM; + i32 result = 0; + File_View *view = (File_View*)view_; + Editing_File *file = view->file; + Style *style = view->style; + Font *font = style->font; + + real32 line_height = (real32)font->height; + real32 cursor_y = view_get_cursor_y(view); + real32 target_y = view->target_y; + real32 max_y = view_compute_height(view) - line_height*2; + i32 lowest_line = view_compute_lowest_line(view); + real32 max_target_y = view_compute_max_target_y(lowest_line, font->height, max_y); + real32 delta_y = 3.f*line_height; + real32 extra_top = 0.f; + extra_top += view_widget_height(view, font); + real32 taken_top_space = line_height + extra_top; + + if (user_input->mouse.my < rect.y0 + taken_top_space){ + view_->mouse_cursor_type = APP_MOUSE_CURSOR_ARROW; + } + else{ + view_->mouse_cursor_type = APP_MOUSE_CURSOR_IBEAM; + } + + if (user_input->mouse.wheel_used){ + real32 wheel_multiplier = 3.f; + real32 delta_target_y = delta_y*user_input->mouse.wheel_amount*wheel_multiplier; + target_y += delta_target_y; + + if (target_y < -taken_top_space) target_y = -taken_top_space; + if (target_y > max_target_y) target_y = max_target_y; + + real32 old_cursor_y = cursor_y; + if (cursor_y >= target_y + max_y) cursor_y = target_y + max_y; + if (cursor_y < target_y + taken_top_space) cursor_y = target_y + taken_top_space; + + if (cursor_y != old_cursor_y){ + view->cursor = + view_compute_cursor_from_xy(view, + view->preferred_x, + cursor_y); + } + + result = 1; + } + + while (cursor_y > target_y + max_y) target_y += delta_y; + while (cursor_y < target_y + taken_top_space) target_y -= delta_y; + + if (target_y > max_target_y) target_y = max_target_y; + if (target_y < -extra_top) target_y = -extra_top; + view->target_y = target_y; + + real32 cursor_x = view_get_cursor_x(view); + real32 target_x = view->target_x; + real32 max_x = view_compute_width(view); + if (cursor_x < target_x){ + target_x = (real32)Max(0, cursor_x - max_x/2); + } + else if (cursor_x >= target_x + max_x){ + target_x = (real32)(cursor_x - max_x/2); + } + + view->target_x = target_x; + + if (smooth_camera_step(&view->target_y, &view->scroll_y, &view->vel_y, 40.f, 1.f/4.f)){ + result = 1; + } + if (smooth_camera_step(&view->target_x, &view->scroll_x, &view->vel_x, 40.f, 1.f/4.f)){ + result = 1; + } + if (view->paste_effect.tick_down > 0){ + --view->paste_effect.tick_down; + result = 1; + } + + if (is_active && user_input->mouse.press_l){ + real32 max_y = view_compute_height(view); + real32 rx = (real32)(user_input->mouse.mx - rect.x0); + real32 ry = (real32)(user_input->mouse.my - rect.y0 - line_height - 2); + + if (ry >= extra_top){ + view_set_widget(view, FWIDG_NONE); + if (rx >= 0 && rx < max_x && ry >= 0 && ry < max_y){ + view_cursor_move(view, rx + view->scroll_x, ry + view->scroll_y, 1); + view->mode = {}; + } + } + result = 1; + } + + if (!is_active) view_set_widget(view, FWIDG_NONE); + + // NOTE(allen): framely undo stuff + { + i32 scrub_max = view->scrub_max; + i32 undo_count, redo_count, total_count; + undo_count = file->undo.undo.edit_count; + redo_count = file->undo.redo.edit_count; + total_count = undo_count + redo_count; + + switch (view->widget.type){ + case FWIDG_TIMELINES: + { + i32_Rect widg_rect = view_widget_rect(view, font); + + UI_State state = + ui_state_init(&view->widget.state, 0, user_input, + view->style, 0, 1); + + UI_Layout layout; + begin_layout(&layout, widg_rect); + + if (view->widget.timeline.undo_line){ + if (do_button(1, &state, &layout, "- Undo", 1)){ + view->widget.timeline.undo_line = 0; + } + + if (view->widget.timeline.undo_line){ + Widget_ID wid = make_id(&state, 2); + i32 new_count; + if (do_undo_slider(wid, &state, &layout, total_count, undo_count, 0, &new_count)){ + view->rewind_speed = 0; + view->rewind_amount = 0; + + for (i32 i = 0; i < scrub_max && new_count < undo_count; ++i){ + view_undo(view_->mem, view->layout, view); + --undo_count; + } + for (i32 i = 0; i < scrub_max && new_count > undo_count; ++i){ + view_redo(view_->mem, view->layout, view); + ++undo_count; + } + } + } + } + else{ + if (do_button(1, &state, &layout, "+ Undo", 1)){ + view->widget.timeline.undo_line = 1; + } + } + + if (view->widget.timeline.history_line){ + if (do_button(3, &state, &layout, "- History", 1)){ + view->widget.timeline.history_line = 0; + } + + Widget_ID wid = make_id(&state, 4); + if (view->widget.timeline.history_line){ + i32 new_count; + i32 mid = ((file->undo.history.edit_count + file->undo.edit_history_cursor) >> 1); + i32 count = file->undo.edit_history_cursor; + if (do_undo_slider(wid, &state, &layout, mid, count, &file->undo, &new_count)){ + for (i32 i = 0; i < scrub_max && new_count < count; ++i){ + view_history_step(view_->mem, view->layout, view, hist_backward); + } + for (i32 i = 0; i < scrub_max && new_count > count; ++i){ + view_history_step(view_->mem, view->layout, view, hist_forward); + } + } + } + } + else{ + if (do_button(3, &state, &layout, "+ History", 1)){ + view->widget.timeline.history_line = 1; + } + } + + view->widget.height = layout.y - widg_rect.y0; + + if (ui_finish_frame(&view->widget.state, &state, &layout, widg_rect, 0, 0)){ + result = 1; + } + }break; + } + + i32 rewind_max = view->rewind_max; + view->rewind_amount += view->rewind_speed; + for (i32 i = 0; view->rewind_amount <= -1.f; ++i, view->rewind_amount += 1.f){ + if (i < rewind_max) view_undo(view_->mem, view->layout, view); + } + + for (i32 i = 0; view->rewind_amount >= 1.f; ++i, view->rewind_amount -= 1.f){ + if (i < rewind_max) view_redo(view_->mem, view->layout, view); + } + + if (view->rewind_speed < 0.f && undo_count == 0){ + view->rewind_speed = 0.f; + view->rewind_amount = 0.f; + } + + if (view->rewind_speed > 0.f && redo_count == 0){ + view->rewind_speed = 0.f; + view->rewind_amount = 0.f; + } + } + + return result; +} + +enum File_Sync_State{ + SYNC_GOOD, + SYNC_BEHIND_OS, + SYNC_UNSAVED +}; + +inline File_Sync_State +buffer_get_sync(Editing_File *file){ + File_Sync_State result = SYNC_GOOD; + if (file->last_4ed_write_time != file->last_sys_write_time) + result = SYNC_BEHIND_OS; + else if (file->last_4ed_edit_time > file->last_sys_write_time) + result = SYNC_UNSAVED; + return result; +} + +internal i32 +draw_file_view(Thread_Context *thread, View *view_, i32_Rect rect, bool32 is_active, + Render_Target *target){ + File_View *view = (File_View*)view_; + Editing_File *file = view->file; + Style *style = view->style; + Font *font = style->font; + + Interactive_Bar bar; + bar.style = style->main.file_info_style; + bar.font = style->font; + bar.pos_x = (real32)rect.x0; + bar.pos_y = (real32)rect.y0; + bar.text_shift_y = 2; + bar.text_shift_x = 0; + bar.rect = rect; + bar.rect.y1 = bar.rect.y0 + font->height + 2; + rect.y0 += font->height + 2; + + i32 max_x = rect.x1 - rect.x0; + i32 max_y = rect.y1 - rect.y0 + font->height; + + Assert(file && file->buffer.data && !file->is_dummy); + + Opaque_Font_Advance opad = get_opaque_font_advance(font); + b32 tokens_use = file->tokens_complete && (file->token_stack.count > 0); + Cpp_Token_Stack token_stack = file->token_stack; + +#if 1 + Partition *part = &view_->mem->part; + Temp_Memory temp = begin_temp_memory(part); + + partition_align(part, 4); + i32 max = partition_remaining(part) / sizeof(Buffer_Render_Item); + Buffer_Render_Item *items = push_array(part, Buffer_Render_Item, max); + + i32 count; + buffer_get_render_data(&file->buffer, view->line_wrap_y, items, max, &count, + (real32)rect.x0, (real32)rect.y0, view->scroll_x, view->scroll_y, !view->unwrapped_lines, + (real32)max_x, (real32)max_y, opad.data, opad.stride, (real32)font->height); + Assert(count > 0); + + i32 cursor_begin, cursor_end; + u32 cursor_color, at_cursor_color; + if (view->show_temp_highlight){ + cursor_begin = view->temp_highlight.pos; + cursor_end = view->temp_highlight_end_pos; + cursor_color = style->main.highlight_color; + at_cursor_color = style->main.at_highlight_color; + } + else{ + cursor_begin = view->cursor.pos; + cursor_end = cursor_begin + 1; + cursor_color = style->main.cursor_color; + at_cursor_color = style->main.at_cursor_color; + } + + i32 token_i = 0; + u32 main_color = style->main.default_color; + if (tokens_use){ + Cpp_Get_Token_Result result = cpp_get_token(&token_stack, items->index); + main_color = *style_get_color(style, token_stack.tokens[result.token_index]); + token_i = result.token_index + 1; + } + + u32 mark_color = style->main.mark_color; + Buffer_Render_Item *item = items; + i32 prev_ind = -1; + for (i32 i = 0; i < count; ++i, ++item){ + i32 ind = item->index; + if (tokens_use && ind != prev_ind){ + Cpp_Token current_token = token_stack.tokens[token_i-1]; + + if (token_i < token_stack.count){ + if (ind >= token_stack.tokens[token_i].start){ + main_color = + *style_get_color(style, token_stack.tokens[token_i]); + current_token = token_stack.tokens[token_i]; + ++token_i; + } + else if (ind >= current_token.start + current_token.size){ + main_color = 0xFFFFFFFF; + } + } + +#if 0 + if (current_token.type == CPP_TOKEN_JUNK && + i >= current_token.start && i <= current_token.start + current_token.size){ + highlight_color = style->main.highlight_junk_color; + } +#endif + } + u32 char_color = main_color; + + if (cursor_begin <= ind && ind < cursor_end && (ind != prev_ind || cursor_begin < ind)){ + if (is_active) draw_rectangle(target, f32R(item->x0, item->y0, item->x1, item->y1), cursor_color); + else draw_rectangle_outline(target, f32R(item->x0, item->y0, item->x1, item->y1), cursor_color); + char_color = at_cursor_color; + } + if (ind == view->mark && prev_ind != ind){ + draw_rectangle_outline(target, f32R(item->x0, item->y0, item->x1, item->y1), mark_color); + } + font_draw_glyph(target, font, (u16)item->glyphid, + item->x0, item->y0, char_color); + prev_ind = ind; + } + + end_temp_memory(temp); + +#else + + i32 size = (i32)file->buffer.size; + u8 *data = (u8*)file->buffer.data; + + Full_Cursor start_cursor; + start_cursor = view_compute_cursor_from_xy(view, 0, view->scroll_y); + view->scroll_y_cursor = start_cursor; + + i32 start_character = start_cursor.pos; + + real32 shift_x = rect.x0 - view->scroll_x; + real32 shift_y = rect.y0 - view->scroll_y; + if (view->unwrapped_lines) shift_y += start_cursor.unwrapped_y; + else shift_y += start_cursor.wrapped_y; + + real32 pos_x = 0; + real32 pos_y = 0; + + u32 highlight_color = 0; + u32 main_color = style->main.default_color; + i32 token_i = 0; + + if (tokens_use){ + Cpp_Get_Token_Result result = cpp_get_token(&token_stack, start_character); + token_i = result.token_index; + + main_color = *style_get_color(style, token_stack.tokens[token_i]); + ++token_i; + } + + data[size] = 0; + for (i32 i = start_character; i <= size; ++i){ + u8 to_render; + to_render = data[i]; + + real32 ch_width = font_get_glyph_width(font, to_render); + real32 ch_advance = measure_character(opad.data, opad.stride, to_render); + + if (!view->unwrapped_lines && pos_x + ch_advance > max_x){ + pos_x = 0; + pos_y += font->height; + } + + u32 fade_color = 0xFFFF00FF; + real32 fade_amount = 0.f; + if (view->paste_effect.tick_down > 0 && + view->paste_effect.start <= i && i < view->paste_effect.end){ + fade_color = view->paste_effect.color; + fade_amount = (real32)(view->paste_effect.tick_down) / view->paste_effect.tick_max; + } + + highlight_color = 0; + if (tokens_use){ + Cpp_Token current_token = token_stack.tokens[token_i-1]; + + if (token_i < token_stack.count){ + if (i >= token_stack.tokens[token_i].start){ + main_color = + *style_get_color(style, token_stack.tokens[token_i]); + current_token = token_stack.tokens[token_i]; + ++token_i; + } + else if (i >= current_token.start + current_token.size){ + main_color = 0xFFFFFFFF; + } + } + + if (current_token.type == CPP_TOKEN_JUNK && + i >= current_token.start && i <= current_token.start + current_token.size){ + highlight_color = style->main.highlight_junk_color; + } + } + if (highlight_color == 0 && view->show_whitespace && char_is_whitespace(data[i])){ + highlight_color = style->main.highlight_white_color; + } + + i32 cursor_mode = 0; + if (view->show_temp_highlight){ + if (view->temp_highlight.pos <= i && i < view->temp_highlight_end_pos) + cursor_mode = 2; + } + else{ + if (view->cursor.pos == i) + cursor_mode = 1; + } + + real32_Rect cursor_rect = + real32XYWH(shift_x + pos_x, shift_y + pos_y, + 1 + ch_width, (real32)font->height); + + if (to_render == '\t' || to_render == 0){ + cursor_rect.x1 = cursor_rect.x0 + font->chardata[' '].xadvance; + } + + if (highlight_color != 0){ + real32_Rect highlight_rect = cursor_rect; + highlight_rect.x0 += 1; + draw_rectangle(target, highlight_rect, highlight_color); + } + + u32 cursor_color = 0; + switch (cursor_mode){ + case 1: + cursor_color = style->main.cursor_color; break; + case 2: + cursor_color = style->main.highlight_color; break; + } + + if (is_active){ + if (cursor_color & 0xFF000000) draw_rectangle(target, cursor_rect, cursor_color); + } + else{ + if (cursor_color & 0xFF000000)draw_rectangle_outline(target, cursor_rect, cursor_color); + } + if (i == view->mark){ + draw_rectangle_outline(target, cursor_rect, style->main.mark_color); + } + + u32 char_color = main_color; + u32 special_char_color = main_color; + if (to_render == '\r'){ + special_char_color = char_color = style->main.special_character_color; + } + if (is_active){ + switch (cursor_mode){ + case 1: + char_color = style->main.at_cursor_color; break; + case 2: + special_char_color = char_color = style->main.at_highlight_color; break; + } + } + + char_color = color_blend(char_color, fade_amount, fade_color); + special_char_color = color_blend(special_char_color, fade_amount, fade_color); + + if (to_render == '\r'){ + font_draw_glyph(target, font, '\\', + shift_x + pos_x, + shift_y + pos_y, + char_color); + pos_x += font_get_glyph_width(font, '\\'); + + real32 cw = font_get_glyph_width(font, 'r'); + draw_rectangle(target, + real32XYWH(shift_x + pos_x, + shift_y + pos_y, + cw, (real32)font->height), + highlight_color); + font_draw_glyph(target, font, 'r', + shift_x + pos_x, + shift_y + pos_y, + special_char_color); + pos_x += cw; + } + + else if (to_render == '\n'){ + pos_x = 0; + pos_y += font->height; + } + + else if (font->glyphs[to_render].exists){ + font_draw_glyph(target, font, to_render, + shift_x + pos_x, + shift_y + pos_y, + char_color); + pos_x += ch_width; + } + + else{ + pos_x += ch_width; + } + + if (pos_y > max_y){ + break; + } + } +#endif + + if (view->widget.type != FWIDG_NONE){ + UI_Style ui_style = get_ui_style_upper(style); + + Font *font = style->font; + i32_Rect widg_rect = view_widget_rect(view, font); + + draw_rectangle(target, widg_rect, ui_style.dark); + draw_rectangle_outline(target, widg_rect, ui_style.dim); + + UI_State state = + ui_state_init(&view->widget.state, target, 0, + view->style, 0, 0); + + UI_Layout layout; + begin_layout(&layout, widg_rect); + + switch (view->widget.type){ + case FWIDG_TIMELINES: + { + if (view->widget.timeline.undo_line){ + do_button(1, &state, &layout, "- Undo", 1); + + Widget_ID wid = make_id(&state, 2); + i32 undo_count, redo_count, total_count; + undo_count = file->undo.undo.edit_count; + redo_count = file->undo.redo.edit_count; + total_count = undo_count + redo_count; + do_undo_slider(wid, &state, &layout, total_count, undo_count, 0, 0); + } + else{ + do_button(1, &state, &layout, "+ Undo", 1); + } + + if (view->widget.timeline.history_line){ + do_button(3, &state, &layout, "- History", 1); + + Widget_ID wid = make_id(&state, 4); + i32 new_count; + i32 mid = ((file->undo.history.edit_count + file->undo.edit_history_cursor) >> 1); + do_undo_slider(wid, &state, &layout, mid, file->undo.edit_history_cursor, &file->undo, &new_count); + } + else{ + do_button(3, &state, &layout, "+ History", 1); + } + }break; + + 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; + } + + ui_finish_frame(&view->widget.state, &state, &layout, widg_rect, 0, 0); + } + + { + u32 back_color = bar.style.bar_color; + draw_rectangle(target, bar.rect, back_color); + + u32 base_color = bar.style.base_color; + intbar_draw_string(target, &bar, file->live_name, base_color); + intbar_draw_string(target, &bar, make_lit_string(" - "), base_color); + + char line_number_space[30]; + String line_number = make_string(line_number_space, 0, 30); + append(&line_number, "L#"); + append_int_to_str(view->cursor.line, &line_number); + + intbar_draw_string(target, &bar, line_number, base_color); + + switch (buffer_get_sync(file)){ + case SYNC_BEHIND_OS: + { + persist String out_of_sync = make_lit_string(" BEHIND OS"); + intbar_draw_string(target, &bar, out_of_sync, bar.style.pop2_color); + }break; + + case SYNC_UNSAVED: + { + persist String out_of_sync = make_lit_string(" *"); + intbar_draw_string(target, &bar, out_of_sync, bar.style.pop2_color); + }break; + } + } + + return 0; +} + +internal void +kill_buffer(General_Memory *general, Editing_File *file, Live_Views *live_set, Editing_Layout *layout){ + i32 panel_count = layout->panel_count; + Panel *panels = layout->panels, *panel; + panel = panels; + + for (i32 i = 0; i < panel_count; ++i){ + View *view = panel->view; + if (view){ + View *to_kill = view; + if (view->is_minor) to_kill = view->major; + File_View *fview = view_to_file_view(to_kill); + if (fview && fview->file == file){ + live_set_free_view(live_set, &fview->view_base); + if (to_kill == view) panel->view = 0; + else view->major = 0; + } + } + ++panel; + } + + file_close(general, file); + file_get_dummy(file); +} + +internal void +command_search(Command_Data*,Command_Binding); +internal void +command_rsearch(Command_Data*,Command_Binding); + +internal +HANDLE_COMMAND_SIG(handle_command_file_view){ + File_View *file_view = (File_View*)(view); + Editing_File *file = file_view->file; + + switch (file_view->widget.type){ + case FWIDG_NONE: + case FWIDG_TIMELINES: + { + file_view->next_mode = {}; + if (binding.function) binding.function(command, binding); + file_view->mode = file_view->next_mode; + + if (key.key.keycode == codes->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(codes, key, string); + + if (result.made_a_change || + binding.function == command_search || + binding.function == command_rsearch){ + bool32 step_forward = 0; + bool32 step_backward = 0; + + if (binding.function == command_search){ + step_forward = 1; + } + if (binding.function == command_rsearch){ + 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; + } + } + 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; + } + } + + String file_string = make_string(file->buffer.data, file->buffer.size); + i32 pos; + if (file_view->isearch.reverse){ + if (result.hit_backspace){ + start_pos = file_view->temp_highlight.pos + 1; + file_view->isearch.pos = start_pos; + } + else{ + pos = rfind_substr(file_string, start_pos - 1, *string); + if (pos >= 0){ + if (step_backward){ // TODO(allen): this if and it's mirror are suspicious + file_view->isearch.pos = pos; + start_pos = pos; + pos = rfind_substr(file_string, start_pos - 1, *string); + if (pos == -1){ + pos = start_pos; + } + } + view_set_temp_highlight(file_view, pos, pos+string->size); + } + } + } + + else{ + if (result.hit_backspace){ + start_pos = file_view->temp_highlight.pos - 1; + file_view->isearch.pos = start_pos; + } + else{ + pos = find_substr(file_string, start_pos + 1, *string); + if (pos < file->buffer.size){ + if (step_forward){ + file_view->isearch.pos = pos; + start_pos = pos; + pos = find_substr(file_string, start_pos + 1, *string); + if (pos == file->buffer.size){ + pos = start_pos; + } + } + view_set_temp_highlight(file_view, pos, pos+string->size); + } + } + } + } + + 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(codes, key, string); + + if (result.hit_newline || result.hit_ctrl_newline){ + i32 line_number = str_to_int(*string); + Font *font = file_view->style->font; + if (line_number < 1) line_number = 1; + file_view->cursor = + view_compute_cursor_from_unwrapped_xy(file_view, 0, (real32)(line_number-1)*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; + } +} + +inline void +free_file_view(View *view_){ + File_View *view = (File_View*)view_; + general_memory_free(&view_->mem->general, view->line_wrap_y); +} + +internal +DO_VIEW_SIG(do_file_view){ + i32 result = 0; + switch (message){ + case VMSG_RESIZE: + case VMSG_STYLE_CHANGE: + { + remeasure_file_view(view, rect); + }break; + case VMSG_DRAW: + { + result = draw_file_view(thread, view, rect, (view == active), target); + }break; + case VMSG_STEP: + { + result = step_file_view(thread, view, rect, (view == active), user_input); + }break; + case VMSG_FREE: + { + free_file_view(view); + }break; + } + return result; +} + +internal File_View* +file_view_init(View *view, Delay *delay, Editing_Layout *layout){ + view->type = VIEW_TYPE_FILE; + view->do_view = do_file_view; + view->handle_command = handle_command_file_view; + + File_View *result = (File_View*)view; + result->delay = delay; + result->layout = layout; + result->rewind_max = 4; + result->scrub_max = 1; + return result; +} + +// BOTTOM + diff --git a/buffer/4coder_buffer_abstract.cpp b/buffer/4coder_buffer_abstract.cpp new file mode 100644 index 00000000..9916f875 --- /dev/null +++ b/buffer/4coder_buffer_abstract.cpp @@ -0,0 +1,541 @@ +/* + * Mr. 4th Dimention - Allen Webster + * Four Tech + * + * public domain -- no warranty is offered or implied; use this code at your own risk + * + * 16.10.2015 + * + * Buffer data object + * type - Golden Array + * + */ + +// TOP + +#if BUFFER_EXPERIMENT_SCALPEL +#define Buffer_Type Buffer +#else +#define Buffer_Type Gap_Buffer +#endif + +#define CAT_(a,b) a##b +#define CAT(a,b) CAT_(a,b) + +#define Buffer_Stringify_Type CAT(Buffer_Type, _Stringify_Loop) +#define Buffer_Backify_Type CAT(Buffer_Type, _Backify_Loop) + +internal_4tech int +buffer_count_newlines(Buffer_Type *buffer, int start, int end){ + Buffer_Stringify_Type loop; + int i; + int count; + + assert_4tech(0 <= start); + assert_4tech(start <= end); + assert_4tech(end < buffer_size(buffer)); + + count = 0; + + for (loop = buffer_stringify_loop(buffer, start, end, end - start); + buffer_stringify_good(&loop); + buffer_stringify_next(&loop)){ + for (i = 0; i < loop.size; ++i){ + count += (loop.data[i] == '\n'); + } + } + + return(count); +} + +internal_4tech int +buffer_seek_whitespace_right(Buffer_Type *buffer, int pos){ + Buffer_Stringify_Type loop; + char *data; + int end; + int size; + + size = buffer_size(buffer); + loop = buffer_stringify_loop(buffer, pos, size, size); + + for (;buffer_stringify_good(&loop); + buffer_stringify_next(&loop)){ + end = loop.size + loop.absolute_pos; + data = loop.data - loop.absolute_pos; + for (; pos < end && is_whitespace(data[pos]); ++pos); + if (!is_whitespace(data[pos])) break; + } + + for (;buffer_stringify_good(&loop); + buffer_stringify_next(&loop)){ + end = loop.size + loop.absolute_pos; + data = loop.data - loop.absolute_pos; + for (; pos < end && !is_whitespace(data[pos]); ++pos); + if (is_whitespace(data[pos])) break; + } + + return(pos); +} + +internal_4tech int +buffer_seek_whitespace_left(Buffer_Type *buffer, int pos){ + Buffer_Backify_Type loop; + char *data; + int end; + int size; + + size = buffer_size(buffer); + loop = buffer_backify_loop(buffer, pos, 0, size); + + for (;buffer_backify_good(&loop); + buffer_backify_next(&loop)){ + end = loop.absolute_pos; + data = loop.data - end; + for (; pos > end && is_whitespace(data[pos]); --pos); + if (!is_whitespace(data[pos])) break; + } + + for (;buffer_backify_good(&loop); + buffer_backify_next(&loop)){ + end = loop.absolute_pos; + data = loop.data - end; + for (; pos > end && !is_whitespace(data[pos]); --pos); + if (is_whitespace(data[pos])) break; + } + + return(pos); +} + +typedef struct{ + int i; + int count; + int start; +} Buffer_Measure_Starts; + +internal_4tech int +buffer_measure_starts(Buffer_Measure_Starts *state, Buffer_Type *buffer){ + Buffer_Stringify_Type loop; + int *starts; + int max; + char *data; + int size, end; + int start, count, i; + int result; + + size = buffer_size(buffer); + starts = buffer->line_starts; + max = buffer->line_max; + + result = 0; + + i = state->i; + count = state->count; + start = state->start; + + for (loop = buffer_stringify_loop(buffer, i, size, size); + buffer_stringify_good(&loop); + buffer_stringify_next(&loop)){ + end = loop.size + loop.absolute_pos; + data = loop.data - loop.absolute_pos; + for (; i < end; ++i){ + if (data[i] == '\n'){ + if (count == max){ + result = 1; + goto buffer_measure_starts_end; + } + + starts[count++] = start; + start = i + 1; + } + } + } + + if (i == size){ + starts[count++] = start; + } + +buffer_measure_starts_end: + state->i = i; + state->count = count; + state->start = start; + + return(result); +} + +internal_4tech void +buffer_remeasure_starts(Buffer_Type *buffer, int line_start, int line_end, int line_shift, int text_shift){ + Buffer_Stringify_Type loop; + int *starts; + int line_count; + char *data; + int size, end; + int line_i, char_i, start; + + starts = buffer->line_starts; + line_count = buffer->line_count; + + assert_4tech(0 <= line_start); + assert_4tech(line_start <= line_end); + assert_4tech(line_end < line_count); + assert_4tech(line_count + line_shift <= buffer->line_max); + + ++line_end; + if (text_shift != 0){ + line_i = line_end; + starts += line_i; + for (; line_i < line_count; ++line_i, ++starts){ + *starts += text_shift; + } + starts = buffer->line_starts; + } + + if (line_shift != 0){ + memmove_4tech(starts + line_end + line_shift, starts + line_end, + sizeof(int)*(line_count - line_end)); + line_count += line_shift; + } + + line_end += line_shift; + size = buffer_size(buffer); + char_i = starts[line_start]; + line_i = line_start; + start = char_i; + + for (loop = buffer_stringify_loop(buffer, char_i, size, size); + buffer_stringify_good(&loop); + buffer_stringify_next(&loop)){ + end = loop.size + loop.absolute_pos; + data = loop.data - loop.absolute_pos; + for (; char_i < end; ++char_i){ + if (data[char_i] == '\n'){ + starts[line_i++] = start; + start = char_i + 1; + if (line_i >= line_end && start == starts[line_i]) goto buffer_remeasure_starts_end; + } + } + } + + if (char_i == size){ + starts[line_i++] = start; + } + +buffer_remeasure_starts_end: + assert_4tech(line_count >= 1); + buffer->line_count = line_count; +} + +internal_4tech void +buffer_remeasure_widths(Buffer_Type *buffer, void *advance_data, int stride, + int line_start, int line_end, int line_shift){ + Buffer_Stringify_Type loop; + int *starts; + float *widths; + int line_count; + char *data; + int size, end; + int i, j; + float width; + char ch; + + starts = buffer->line_starts; + widths = buffer->line_widths; + line_count = buffer->line_count; + + assert_4tech(0 <= line_start); + assert_4tech(line_start <= line_end); + assert_4tech(line_end < line_count); + assert_4tech(line_count <= buffer->widths_max); + + ++line_end; + if (line_shift != 0){ + memmove_4tech(widths + line_end + line_shift, widths + line_end, + sizeof(float)*(line_count - line_end)); + } + + line_end += line_shift; + i = line_start; + j = starts[i]; + + if (line_end == line_count) size = buffer_size(buffer); + else size = starts[line_end]; + + width = 0; + + for (loop = buffer_stringify_loop(buffer, j, size, size); + buffer_stringify_good(&loop); + buffer_stringify_next(&loop)){ + end = loop.size + loop.absolute_pos; + data = loop.data - loop.absolute_pos; + + for (; j < end; ++j){ + ch = data[j]; + if (ch == '\n'){ + widths[i] = width; + ++i; + assert_4tech(j + 1 == starts[i]); + width = 0; + } + else{ + width += measure_character(advance_data, stride, ch); + } + } + } +} + +inline_4tech void +buffer_measure_widths(Buffer_Type *buffer, void *advance_data, int stride){ + assert_4tech(buffer->line_count >= 1); + buffer_remeasure_widths(buffer, advance_data, stride, 0, buffer->line_count-1, 0); +} + +internal_4tech void +buffer_measure_wrap_y(Buffer_Type *buffer, float *wraps, + float font_height, float max_width){ + float *widths; + float y_pos; + int i, line_count; + + line_count = buffer->line_count; + widths = buffer->line_widths; + y_pos = 0; + + for (i = 0; i < line_count; ++i){ + wraps[i] = y_pos; + if (widths[i] == 0) y_pos += font_height; + else y_pos += font_height*ceil_4tech(widths[i]/max_width); + } +} + +internal_4tech int +buffer_get_line_index(Buffer_Type *buffer, int pos, int l_bound, int u_bound){ + int *lines; + int start, end; + int i; + + assert_4tech(0 <= l_bound); + assert_4tech(l_bound <= u_bound); + assert_4tech(u_bound <= buffer->line_count); + + lines = buffer->line_starts; + + start = l_bound; + end = u_bound; + for (;;){ + i = (start + end) >> 1; + if (lines[i] < pos) start = i; + else if (lines[i] > pos) end = i; + else{ + start = i; + break; + } + assert_4tech(start < end); + if (start == end - 1) break; + } + + return(start); +} + +internal_4tech int +buffer_get_line_index_from_wrapped_y(float *wraps, float y, float font_height, int l_bound, int u_bound){ + int start, end, i, result; + start = l_bound; + end = u_bound; + for (;;){ + i = (start + end) / 2; + if (wraps[i]+font_height <= y) start = i; + else if (wraps[i] > y) end = i; + else{ + result = i; + break; + } + if (start >= end - 1){ + result = start; + break; + } + } + return(result); +} + +inline_4tech Full_Cursor +make_cursor_hint(int line_index, int *starts, float *wrap_ys, float font_height){ + Full_Cursor hint; + hint.pos = starts[line_index]; + hint.line = line_index + 1; + hint.character = 1; + hint.unwrapped_y = (f32)(line_index * font_height); + hint.unwrapped_x = 0; + hint.wrapped_y = wrap_ys[line_index]; + hint.wrapped_x = 0; + return(hint); +} + +internal_4tech Full_Cursor +buffer_cursor_from_pos(Buffer_Type *buffer, int pos, float *wraps, + float max_width, float font_height, void *advance_data, int stride){ + Full_Cursor result; + int line_index; + + line_index = buffer_get_line_index(buffer, pos, 0, buffer->line_count); + result = make_cursor_hint(line_index, buffer->line_starts, wraps, font_height); + result = buffer_cursor_seek(buffer, seek_pos(pos), max_width, font_height, + advance_data, stride, result); + + return(result); +} + +internal_4tech Full_Cursor +buffer_cursor_from_unwrapped_xy(Buffer_Type *buffer, float x, float y, int round_down, float *wraps, + float max_width, float font_height, void *advance_data, int stride){ + Full_Cursor result; + int line_index; + + line_index = (int)(y / font_height); + if (line_index >= buffer->line_count) line_index = buffer->line_count - 1; + if (line_index < 0) line_index = 0; + + result = make_cursor_hint(line_index, buffer->line_starts, wraps, font_height); + result = buffer_cursor_seek(buffer, seek_unwrapped_xy(x, y, round_down), max_width, font_height, + advance_data, stride, result); + + return(result); +} + +internal_4tech Full_Cursor +buffer_cursor_from_wrapped_xy(Buffer_Type *buffer, float x, float y, int round_down, float *wraps, + float max_width, float font_height, void *advance_data, int stride){ + Full_Cursor result; + int line_index; + + line_index = buffer_get_line_index_from_wrapped_y(wraps, y, font_height, 0, buffer->line_count); + result = make_cursor_hint(line_index, buffer->line_starts, wraps, font_height); + result = buffer_cursor_seek(buffer, seek_wrapped_xy(x, y, round_down), max_width, font_height, + advance_data, stride, result); + + return(result); +} + +internal_4tech void +buffer_get_render_data(Buffer_Type *buffer, float *wraps, Buffer_Render_Item *items, int max, int *count, + float port_x, float port_y, float scroll_x, float scroll_y, Full_Cursor start_cursor, int wrapped, + float width, float height, void *advance_data, int stride, float font_height){ + Buffer_Stringify_Type loop; + Buffer_Render_Item *item; + char *data; + int size, end; + float shift_x, shift_y; + float x, y; + int i, item_i; + float ch_width, ch_width_sub; + char ch; + + size = buffer_size(buffer); + + shift_x = port_x - scroll_x; + shift_y = port_y - scroll_y; + if (wrapped) shift_y += start_cursor.wrapped_y; + else shift_y += start_cursor.unwrapped_y; + + x = shift_x; + y = shift_y; + item_i = 0; + item = items + item_i; + + for (loop = buffer_stringify_loop(buffer, start_cursor.pos, size, size); + buffer_stringify_good(&loop); + buffer_stringify_next(&loop)){ + + end = loop.size + loop.absolute_pos; + data = loop.data - loop.absolute_pos; + + for (i = loop.absolute_pos; i < end; ++i){ + ch = data[i]; + ch_width = measure_character(advance_data, stride, ch); + + if (ch_width + x > width + shift_x && wrapped){ + x = shift_x; + y += font_height; + } + if (y > height + shift_y) goto buffer_get_render_data_end; + + switch (ch){ + case '\n': + write_render_item_inline(item, i, ' ', x, y, advance_data, stride, font_height); + ++item_i; + ++item; + + x = shift_x; + y += font_height; + break; + + case 0: + ch_width = write_render_item_inline(item, i, '\\', x, y, advance_data, stride, font_height); + ++item_i; + ++item; + x += ch_width; + + ch_width = write_render_item_inline(item, i, '0', x, y, advance_data, stride, font_height); + ++item_i; + ++item; + x += ch_width; + break; + + case '\r': + ch_width = write_render_item_inline(item, i, '\\', x, y, advance_data, stride, font_height); + ++item_i; + ++item; + x += ch_width; + + ch_width = write_render_item_inline(item, i, 'r', x, y, advance_data, stride, font_height); + ++item_i; + ++item; + x += ch_width; + break; + + case '\t': + ch_width_sub = write_render_item_inline(item, i, '\\', x, y, advance_data, stride, font_height); + ++item_i; + ++item; + + write_render_item_inline(item, i, 't', x + ch_width_sub, y, advance_data, stride, font_height); + ++item_i; + ++item; + x += ch_width; + break; + + default: + write_render_item(item, i, ch, x, y, ch_width, font_height); + ++item_i; + ++item; + x += ch_width; + break; + } + if (y > height + shift_y) goto buffer_get_render_data_end; + } + } + +buffer_get_render_data_end: + // TODO(allen): handle this with a control state + assert_4tech(item_i <= max); + *count = item_i; +} + +internal_4tech void +buffer_get_render_data(Buffer_Type *buffer, float *wraps, Buffer_Render_Item *items, int max, int *count, + float port_x, float port_y, float scroll_x, float scroll_y, int wrapped, + float width, float height, void *advance_data, int stride, float font_height){ + Full_Cursor start_cursor; + if (wrapped){ + start_cursor = buffer_cursor_from_wrapped_xy(buffer, 0, scroll_y, 0, wraps, + width, font_height, advance_data, stride); + } + else{ + start_cursor = buffer_cursor_from_unwrapped_xy(buffer, 0, scroll_y, 0, wraps, + width, font_height, advance_data, stride); + } + buffer_get_render_data(buffer, wraps, items, max, count, + port_x, port_y, scroll_x, scroll_y, start_cursor, wrapped, + width, height, advance_data, stride, font_height); +} + +// BOTTOM + diff --git a/buffer/4coder_gap_buffer.cpp b/buffer/4coder_gap_buffer.cpp new file mode 100644 index 00000000..b160353e --- /dev/null +++ b/buffer/4coder_gap_buffer.cpp @@ -0,0 +1,302 @@ +/* + * Mr. 4th Dimention - Allen Webster + * Four Tech + * + * public domain -- no warranty is offered or implied; use this code at your own risk + * + * 23.10.2015 + * + * Buffer data object + * type - Gap Buffer + * + */ + +// TOP + +typedef struct{ + char *data; + int size1, gap_size, size2, max; + + float *line_widths; + int *line_starts; + int line_count; + int line_max; + int widths_max; +} Gap_Buffer; + +inline_4tech void +gap_buffer_initialize(Gap_Buffer *buffer, char *data, int size){ + int size1, size2, gap_size; + + assert_4tech(buffer->max >= size); + + size1 = size >> 1; + size2 = size - size1; + gap_size = buffer->max - size1 - size2; + buffer->size1 = size1; + buffer->size2 = size2; + buffer->gap_size = gap_size; + + if (buffer->size1 > 0){ + memcpy_4tech(buffer->data, data, size1); + } + + if (buffer->size2 > 0){ + memcpy_4tech(buffer->data + size1 + gap_size, data + size1, size2); + } +} + +inline_4tech int +buffer_size(Gap_Buffer *buffer){ + int size; + size = buffer->size1 + buffer->size2; + return(size); +} + +typedef struct{ + Gap_Buffer *buffer; + char *data, *base; + int absolute_pos; + int pos, end; + int size; + int page_size; + int separated; +} Gap_Buffer_Stringify_Loop; + +inline_4tech Gap_Buffer_Stringify_Loop +buffer_stringify_loop(Gap_Buffer *buffer, int start, int end, int page_size){ + Gap_Buffer_Stringify_Loop result; + if (0 <= start && start < end && end <= buffer->size1 + buffer->size2){ + result.buffer = buffer; + result.base = buffer->data; + result.page_size = page_size; + result.absolute_pos = start; + + if (end <= buffer->size1) result.end = end; + else result.end = end + buffer->gap_size; + + if (start < buffer->size1){ + if (end <= buffer->size1) result.separated = 0; + else result.separated = 1; + result.pos = start; + } + else{ + result.separated = 0; + result.pos = start + buffer->gap_size; + } + if (result.separated) result.size = buffer->size1 - start; + else result.size = end - start; + if (result.size > page_size) result.size = page_size; + result.data = buffer->data + result.pos; + } + else result.buffer = 0; + return(result); +} + +inline_4tech int +buffer_stringify_good(Gap_Buffer_Stringify_Loop *loop){ + int result; + result = (loop->buffer != 0); + return(result); +} + +inline_4tech void +buffer_stringify_next(Gap_Buffer_Stringify_Loop *loop){ + int size1, temp_end; + if (loop->separated){ + size1 = loop->buffer->size1; + if (loop->pos + loop->size == size1){ + loop->separated = 0; + loop->pos = loop->buffer->gap_size + size1; + loop->absolute_pos = size1; + temp_end = loop->end; + } + else{ + loop->pos += loop->page_size; + loop->absolute_pos += loop->page_size; + temp_end = size1; + } + } + else{ + if (loop->pos + loop->size == loop->end){ + loop->buffer = 0; + temp_end = loop->pos; + } + else{ + loop->pos += loop->page_size; + loop->absolute_pos += loop->page_size; + temp_end = loop->end; + } + } + loop->size = temp_end - loop->pos; + if (loop->size > loop->page_size) loop->size = loop->page_size; + loop->data = loop->base + loop->pos; +} + +typedef struct{ + Gap_Buffer *buffer; + char *data, *base; + int pos, end; + int size; + int absolute_pos; + int page_size; + int separated; +} Gap_Buffer_Backify_Loop; + +inline_4tech Gap_Buffer_Backify_Loop +buffer_backify_loop(Gap_Buffer *buffer, int start, int end, int page_size){ + Gap_Buffer_Backify_Loop result; + int chunk2_start; + + if (0 <= end && end < start && start <= buffer->size1 + buffer->size2){ + chunk2_start = buffer->size1 + buffer->gap_size; + + result.buffer = buffer; + result.base = buffer->data; + result.page_size = page_size; + + if (end < buffer->size1) result.end = end; + else result.end = end + buffer->gap_size; + + if (start <= buffer->size1){ + result.separated = 0; + result.pos = start - page_size; + } + else{ + if (end < buffer->size1) result.separated = 1; + else result.separated = 0; + result.pos = start - page_size + buffer->gap_size; + } + if (result.separated){ + if (result.pos < chunk2_start) result.pos = chunk2_start; + } + else{ + if (result.pos < result.end) result.pos = result.end; + } + result.size = start - result.pos; + result.absolute_pos = result.pos; + if (result.absolute_pos > buffer->size1) result.absolute_pos -= buffer->gap_size; + result.data = result.base + result.pos; + } + else result.buffer = 0; + return(result); +} + +inline_4tech int +buffer_backify_good(Gap_Buffer_Backify_Loop *loop){ + int result; + result = (loop->buffer != 0); + return(result); +} + +inline_4tech void +buffer_backify_next(Gap_Buffer_Backify_Loop *loop){ + Gap_Buffer *buffer; + int temp_end; + int chunk2_start; + buffer = loop->buffer; + chunk2_start = buffer->size1 + buffer->gap_size; + if (loop->separated){ + if (loop->pos == chunk2_start){ + loop->separated = 0; + temp_end = buffer->size1; + loop->pos = temp_end - loop->page_size; + loop->absolute_pos = loop->pos; + if (loop->pos < loop->end){ + loop->absolute_pos += (loop->end - loop->pos); + loop->pos = loop->end; + } + } + else{ + temp_end = loop->pos; + loop->pos -= loop->page_size; + loop->absolute_pos -= loop->page_size; + if (loop->pos < chunk2_start){ + loop->pos = chunk2_start; + loop->absolute_pos = buffer->size1; + } + } + } + else{ + if (loop->pos == loop->end){ + temp_end = 0; + loop->buffer = 0; + } + else{ + temp_end = loop->pos; + loop->pos -= loop->page_size; + loop->absolute_pos -= loop->page_size; + if (loop->pos < loop->end){ + loop->absolute_pos += (loop->end - loop->pos); + loop->pos = loop->end; + } + } + } + loop->size = temp_end - loop->pos; + loop->data = loop->base + loop->pos; +} + +inline_4tech void +buffer_stringify(Gap_Buffer *buffer, int start, int end, char *out){ + for (Gap_Buffer_Stringify_Loop loop = buffer_stringify_loop(buffer, start, end, end - start); + buffer_stringify_good(&loop); + buffer_stringify_next(&loop)){ + memcpy_4tech(out, loop.data, loop.size); + out += loop.size; + } +} + +internal_4tech Full_Cursor +buffer_cursor_seek(Gap_Buffer *buffer, Buffer_Seek seek, float max_width, float font_height, + void *advance_data, int stride, Full_Cursor cursor){ + char *data; + int size1, size2, gap_size; + int total_size, end, i; + int step; + int result; + + Seek_State state; + char *advances; + int xy_seek; + char ch; + + data = buffer->data; + size1 = buffer->size1; + gap_size = buffer->gap_size; + size2 = buffer->size2; + total_size = size1 + gap_size + size2; + + advances = (char*)advance_data; + xy_seek = (seek.type == buffer_seek_wrapped_xy || seek.type == buffer_seek_unwrapped_xy); + state.cursor = cursor; + + result = 1; + i = cursor.pos; + end = size1; + for (step = 0; step < 2; ++step){ + for (; i < end && result; ++i){ + ch = data[i]; + result = cursor_seek_step(&state, seek, xy_seek, max_width, font_height, + advances, stride, size1 + size2, ch); + } + end = total_size; + i += gap_size; + } + if (result){ + result = cursor_seek_step(&state, seek, xy_seek, max_width, font_height, + advances, stride, size1 + size2, 0); + assert_4tech(result == 0); + } + + return(state.cursor); +} + +// TODO(allen): unfinished below here + +internal_4tech int +gap_buffer_replace_range(Gap_Buffer *buffer, int start, int end, char *str, int len, int *shift_amount){ + return(0); +} + +// BOTTOM + diff --git a/buffer/4coder_golden_array.cpp b/buffer/4coder_golden_array.cpp index a96d2ab3..17842cdd 100644 --- a/buffer/4coder_golden_array.cpp +++ b/buffer/4coder_golden_array.cpp @@ -13,31 +13,26 @@ // TOP -#ifndef defines_4tech -#define inline_4tech inline -#define internal_4tech static -#define memset_4tech memset -#define memcpy_4tech memcpy -#define memmove_4tech memmove -#define defines_4tech 1 -#define debug_4tech(x) x -#define assert_4tech assert -#endif - typedef struct{ char *data; int size, max; - float *line_widths; int *line_starts; + float *line_widths; int line_count; int line_max; int widths_max; } Buffer; +inline_4tech int +buffer_size(Buffer *buffer){ + return buffer->size; +} + typedef struct{ Buffer *buffer; char *data, *end; + int absolute_pos; int size; int page_size; } Buffer_Stringify_Loop; @@ -47,6 +42,7 @@ buffer_stringify_loop(Buffer *buffer, int start, int end, int page_size){ Buffer_Stringify_Loop result; if (0 <= start && start < end && end <= buffer->size){ result.buffer = buffer; + result.absolute_pos = start; result.data = buffer->data + start; result.size = end - start; result.end = buffer->data + end; @@ -69,11 +65,59 @@ buffer_stringify_next(Buffer_Stringify_Loop *loop){ if (loop->data + loop->size == loop->end) loop->buffer = 0; else{ loop->data += loop->page_size; + loop->absolute_pos += loop->page_size; loop->size = (int)(loop->end - loop->data); if (loop->size > loop->page_size) loop->size = loop->page_size; } } +typedef struct{ + Buffer *buffer; + char *data, *end; + int absolute_pos; + int size; + int page_size; +} Buffer_Backify_Loop; + +inline_4tech Buffer_Backify_Loop +buffer_backify_loop(Buffer *buffer, int start, int end, int page_size){ + Buffer_Backify_Loop result; + if (0 <= end && end < start && start <= buffer->size){ + result.buffer = buffer; + result.end = buffer->data + end; + result.page_size = page_size; + result.size = start - end; + if (result.size > page_size) result.size = page_size; + result.absolute_pos = start - result.size; + result.data = buffer->data + result.absolute_pos; + } + else result.buffer = 0; + return(result); +} + +inline_4tech int +buffer_backify_good(Buffer_Backify_Loop *loop){ + int result; + result = (loop->buffer != 0); + return(result); +} + +inline_4tech void +buffer_backify_next(Buffer_Backify_Loop *loop){ + char *old_data; + if (loop->data == loop->end) loop->buffer = 0; + else{ + old_data = loop->data; + loop->data -= loop->page_size; + loop->absolute_pos -= loop->page_size; + if (loop->data < loop->end){ + loop->size = (int)(old_data - loop->end); + loop->data = loop->end; + loop->absolute_pos = 0; + } + } +} + inline_4tech void buffer_stringify(Buffer *buffer, int start, int end, char *out){ for (Buffer_Stringify_Loop loop = buffer_stringify_loop(buffer, start, end, end - start); @@ -84,227 +128,25 @@ buffer_stringify(Buffer *buffer, int start, int end, char *out){ } } -internal_4tech int -buffer_count_newlines(Buffer *buffer, int start, int end){ - char *data; - int i; - int count; - - assert_4tech(0 <= start); - assert_4tech(start <= end); - assert_4tech(end < buffer->size); - - data = buffer->data; - count = 0; - - for (i = start; i < end; ++i){ - count += (data[i] == '\n'); - } - - return(count); -} - -typedef struct{ - int i; - int count; - int start; -} Buffer_Measure_Starts; - -internal_4tech int -buffer_measure_starts(Buffer_Measure_Starts *state, Buffer *buffer){ - int *starts; - int max; - char *data; - int size; - int start, count, i; - int result; - - starts = buffer->line_starts; - max = buffer->line_max; - - data = buffer->data; - size = buffer->size; - - assert_4tech(size < buffer->max); - data[size] = '\n'; - - result = 0; - - i = state->i; - count = state->count; - start = state->start; - - for (; i <= size; ++i){ - if (data[i] == '\n'){ - if (count == max){ - result = 1; - break; - } - - starts[count++] = start; - start = i + 1; - } - } - - state->i = i; - state->count = count; - state->start = start; - - return(result); -} - +#if 0 internal_4tech void -buffer_remeasure_starts(Buffer *buffer, int line_start, int line_end, int line_shift, int text_shift){ - int *starts; - int line_count; - char *data; - int size; - int line_i, char_i, start; - - starts = buffer->line_starts; - line_count = buffer->line_count; - - assert_4tech(0 <= line_start); - assert_4tech(line_start <= line_end); - assert_4tech(line_end < line_count); - assert_4tech(line_count + line_shift <= buffer->line_max); - - ++line_end; - if (text_shift != 0){ - line_i = line_end; - starts += line_i; - for (; line_i < line_count; ++line_i, ++starts){ - *starts += text_shift; - } - starts = buffer->line_starts; - } - - if (line_shift != 0){ - memmove_4tech(starts + line_end + line_shift, starts + line_end, - sizeof(int)*(line_count - line_end)); - line_count += line_shift; - } - - size = buffer->size; - data = buffer->data; - char_i = starts[line_start]; - line_i = line_start; - - assert_4tech(size < buffer->max); - data[size] = '\n'; - - start = char_i; - line_end += line_shift; - for (; char_i <= size; ++char_i){ - if (data[char_i] == '\n'){ - starts[line_i++] = start; - start = char_i + 1; - if (line_i >= line_end && start == starts[line_i]) break; - } - } - - assert_4tech(line_count >= 1); - buffer->line_count = line_count; -} - -inline_4tech float -measure_character(void *advance_data, int stride, char character){ - char *advances; - float width; - - advances = (char*)advance_data; - switch (character){ - case 0: width = *(float*)(advances + stride * '\\') + *(float*)(advances + stride * '0'); break; - case '\n': width = 0; break; - case '\r': width = *(float*)(advances + stride * '\\') + *(float*)(advances + stride * '\r'); break; - default: width = *(float*)(advances + stride * character); - } - - return(width); -} - -internal_4tech void -buffer_remeasure_widths(Buffer *buffer, void *advance_data, int stride, - int line_start, int line_end, int line_shift){ - int *starts; +buffer_measure_wrap_y(Buffer *buffer, float *wraps, + float font_height, float max_width){ float *widths; - int line_count; - char *data; - int i, j; - float width; - char ch; - - starts = buffer->line_starts; - widths = buffer->line_widths; + float y_pos; + int i, line_count; + line_count = buffer->line_count; - - assert_4tech(0 <= line_start); - assert_4tech(line_start <= line_end); - assert_4tech(line_end < line_count); - assert_4tech(line_count <= buffer->widths_max); + widths = buffer->line_widths; + y_pos = 0; - ++line_end; - if (line_shift != 0){ - memmove_4tech(widths + line_end + line_shift, widths + line_end, - sizeof(float)*(line_count - line_end)); - line_count += line_shift; - } - - data = buffer->data; - - assert_4tech(buffer->size < buffer->max); - data[buffer->size] = '\n'; - - i = line_start; - j = starts[i]; - - line_end += line_shift; - for (; i < line_end; ++i){ - assert_4tech(j == starts[i]); - width = 0; - - for (ch = data[j]; ch != '\n'; ch = data[++j]){ - width += measure_character(advance_data, stride, ch); - } - ++j; - - widths[i] = width; + for (i = 0; i < line_count; ++i){ + wraps[i] = y_pos; + if (widths[i] == 0) y_pos += font_height; + else y_pos += font_height*ceil_4tech(widths[i]/max_width); } } - -inline_4tech void -buffer_measure_widths(Buffer *buffer, void *advance_data, int stride){ - assert_4tech(buffer->line_count >= 1); - buffer_remeasure_widths(buffer, advance_data, stride, 0, buffer->line_count-1, 0); -} - -internal_4tech int -buffer_get_line_index(Buffer *buffer, int pos, int l_bound, int u_bound){ - int *lines; - int start, end; - int i; - - assert_4tech(0 <= l_bound); - assert_4tech(l_bound <= u_bound); - assert_4tech(u_bound <= buffer->line_count); - - start = l_bound; - end = u_bound; - lines = buffer->line_starts; - for (;;){ - i = (start + end) >> 1; - if (lines[i] < pos) start = i; - else if (lines[i] > pos) end = i; - else{ - start = i; - break; - } - assert_4tech(start < end); - if (start == end - 1) break; - } - - return(start); -} +#endif internal_4tech int buffer_replace_range(Buffer *buffer, int start, int end, char *str, int len, int *shift_amount){ @@ -405,78 +247,17 @@ buffer_update_cursors(Cursor_With_Index *sorted_positions, int count, int start, for (; position >= sorted_positions && position->pos >= start; --position) position->pos = start; } -typedef enum{ - buffer_seek_pos, - buffer_seek_wrapped_xy, - buffer_seek_unwrapped_xy, - buffer_seek_line_char -} Buffer_Seek_Type; - -typedef struct{ - Buffer_Seek_Type type; - union{ - struct { int pos; }; - struct { int round_down; float x, y; }; - struct { int line, character; }; - }; -} Buffer_Seek; - -inline_4tech Buffer_Seek -seek_pos(int pos){ - Buffer_Seek result; - result.type = buffer_seek_pos; - result.pos = pos; - return(result); -} - -inline_4tech Buffer_Seek -seek_wrapped_xy(float x, float y, int round_down){ - Buffer_Seek result; - result.type = buffer_seek_wrapped_xy; - result.x = x; - result.y = y; - result.round_down = round_down; - return(result); -} - -inline_4tech Buffer_Seek -seek_unwrapped_xy(float x, float y, int round_down){ - Buffer_Seek result; - result.type = buffer_seek_unwrapped_xy; - result.x = x; - result.y = y; - result.round_down = round_down; - return(result); -} - -inline_4tech Buffer_Seek -seek_line_char(int line, int character){ - Buffer_Seek result; - result.type = buffer_seek_line_char; - result.line = line; - result.character = character; - return(result); -} - -typedef struct{ - int pos; - int line, character; - float unwrapped_x, unwrapped_y; - float wrapped_x, wrapped_y; -} Full_Cursor; - internal_4tech Full_Cursor buffer_cursor_seek(Buffer *buffer, Buffer_Seek seek, float max_width, float font_height, void *advance_data, int stride, Full_Cursor cursor){ - Full_Cursor prev_cursor; - char *data, *advances; + char *data; int size; - char ch; - float ch_width; - int get_out; + Seek_State state; + char *advances; + char ch; int xy_seek; - float x, y, px; + int result; data = buffer->data; size = buffer->size; @@ -484,182 +265,18 @@ buffer_cursor_seek(Buffer *buffer, Buffer_Seek seek, float max_width, float font data[size] = 0; advances = (char*)advance_data; - - x = 0; - y = 0; - px = 0; xy_seek = (seek.type == buffer_seek_wrapped_xy || seek.type == buffer_seek_unwrapped_xy); + state.cursor = cursor; - for (;;){ - prev_cursor = cursor; - ch = data[cursor.pos]; - - switch (ch){ - case '\n': - ++cursor.line; - cursor.unwrapped_y += font_height; - cursor.wrapped_y += font_height; - cursor.character = 0; - cursor.unwrapped_x = 0; - cursor.wrapped_x = 0; - break; - - default: - ++cursor.character; - if (ch == '\r') ch_width = *(float*)(advances + stride * '\\') + *(float*)(advances + stride * 'r'); - else ch_width = *(float*)(advances + stride * ch); - - if (cursor.wrapped_x + ch_width >= max_width){ - cursor.wrapped_y += font_height; - cursor.wrapped_x = 0; - prev_cursor = cursor; - } - - cursor.unwrapped_x += ch_width; - cursor.wrapped_x += ch_width; - - break; - } - - ++cursor.pos; - - if (cursor.pos > size){ - cursor = prev_cursor; - break; - } - - get_out = 0; - - switch (seek.type){ - case buffer_seek_pos: - if (cursor.pos > seek.pos){ - cursor = prev_cursor; - get_out = 1; - }break; - - case buffer_seek_wrapped_xy: - x = cursor.wrapped_x; px = prev_cursor.wrapped_x; - y = cursor.wrapped_y; break; - - case buffer_seek_unwrapped_xy: - x = cursor.unwrapped_x; px = prev_cursor.unwrapped_x; - y = cursor.unwrapped_y; break; - - case buffer_seek_line_char: - if (cursor.line == seek.line && cursor.character >= seek.character){ - get_out = 1; - } - else if (cursor.line > seek.line){ - cursor = prev_cursor; - get_out = 1; - }break; - } - - if (get_out) break; - if (xy_seek){ - if (y > seek.y){ - cursor = prev_cursor; - break; - } - - if (y > seek.y - font_height && x >= seek.x){ - if (!seek.round_down){ - if ((seek.x - px) < (x - seek.x)) cursor = prev_cursor; - break; - } - - if (x > seek.x){ - cursor = prev_cursor; - break; - } - } - } - } + do{ + ch = data[state.cursor.pos]; + result = cursor_seek_step(&state, seek, xy_seek, max_width, font_height, + advances, stride, size, ch); + }while(result); - return(cursor); + return(state.cursor); } -inline_4tech Full_Cursor -make_cursor_hint(int line_index, int *starts, float *wrap_ys, float font_height){ - Full_Cursor hint; - hint.pos = starts[line_index]; - hint.line = line_index + 1; - hint.character = 1; - hint.unwrapped_y = (f32)(line_index * font_height); - hint.unwrapped_x = 0; - hint.wrapped_y = wrap_ys[line_index]; - hint.wrapped_x = 0; - return(hint); -} - -internal_4tech Full_Cursor -buffer_cursor_from_pos(Buffer *buffer, int pos, float *wraps, - float max_width, float font_height, void *advance_data, int stride){ - Full_Cursor result; - int line_index; - - line_index = buffer_get_line_index(buffer, pos, 0, buffer->line_count); - result = make_cursor_hint(line_index, buffer->line_starts, wraps, font_height); - result = buffer_cursor_seek(buffer, seek_pos(pos), max_width, font_height, - advance_data, stride, result); - - return(result); -} - -internal_4tech Full_Cursor -buffer_cursor_from_unwrapped_xy(Buffer *buffer, float x, float y, int round_down, float *wraps, - float max_width, float font_height, void *advance_data, int stride){ - Full_Cursor result; - int line_index; - - line_index = (int)(y / font_height); - if (line_index >= buffer->line_count) line_index = buffer->line_count - 1; - if (line_index < 0) line_index = 0; - - result = make_cursor_hint(line_index, buffer->line_starts, wraps, font_height); - result = buffer_cursor_seek(buffer, seek_unwrapped_xy(x, y, round_down), max_width, font_height, - advance_data, stride, result); - - return(result); -} - -internal_4tech Full_Cursor -buffer_cursor_from_wrapped_xy(Buffer *buffer, float x, float y, int round_down, float *wraps, - float max_width, float font_height, void *advance_data, int stride){ - Full_Cursor result; - int line_index; - int start, end, i; - - // NOTE(allen): binary search lines on wrapped y position - // TODO(allen): pull this out once other wrap handling code is ready - start = 0; - end = buffer->line_count; - for (;;){ - i = (start + end) / 2; - if (wraps[i]+font_height <= y) start = i; - else if (wraps[i] > y) end = i; - else{ - line_index = i; - break; - } - if (start >= end - 1){ - line_index = start; - break; - } - } - - result = make_cursor_hint(line_index, buffer->line_starts, wraps, font_height); - result = buffer_cursor_seek(buffer, seek_wrapped_xy(x, y, round_down), max_width, font_height, - advance_data, stride, result); - - return(result); -} - -typedef struct{ - int str_start, len; - int start, end; -} Buffer_Edit; - internal_4tech void buffer_invert_edit_shift(Buffer *buffer, Buffer_Edit edit, Buffer_Edit *inverse, char *strings, int *str_pos, int max, int shift_amount){ int pos; @@ -916,166 +533,5 @@ buffer_eol_convert_out(Buffer *buffer){ buffer->size = size; } -typedef struct{ - int index; - int glyphid; - float x0, y0; - float x1, y1; -} Buffer_Render_Item; - -internal_4tech void -buffer_get_render_data(Buffer *buffer, float *wraps, Buffer_Render_Item *items, int max, int *count, - float port_x, float port_y, float scroll_x, float scroll_y, int wrapped, - float width, float height, void *advance_data, int stride, float font_height){ - Full_Cursor start_cursor; - Buffer_Render_Item *item; - char *data; - int size; - float shift_x, shift_y; - float x, y; - int i, item_i; - float ch_width; - char ch; - - data = buffer->data; - size = buffer->size; - assert_4tech(size < buffer->max); - data[size] = 0; - - shift_x = port_x - scroll_x; - shift_y = port_y - scroll_y; - if (wrapped){ - start_cursor = buffer_cursor_from_wrapped_xy(buffer, 0, scroll_y, 0, wraps, - width, font_height, advance_data, stride); - shift_y += start_cursor.wrapped_y; - } - else{ - start_cursor = buffer_cursor_from_unwrapped_xy(buffer, 0, scroll_y, 0, wraps, - width, font_height, advance_data, stride); - shift_y += start_cursor.unwrapped_y; - } - - i = start_cursor.pos; - - x = shift_x; - y = shift_y; - item_i = 0; - item = items + item_i; - - for (; i <= size; ++i){ - ch = data[i]; - ch_width = measure_character(advance_data, stride, ch); - - if (ch_width + x > width + shift_x && wrapped){ - x = shift_x; - y += font_height; - } - if (y > height + shift_y) break; - - switch (ch){ - case '\n': - ch_width = measure_character(advance_data, stride, ' '); - item->index = i; - item->glyphid = ' '; - item->x0 = x; - item->y0 = y; - item->x1 = x + ch_width; - item->y1 = y + font_height; - ++item_i; - ++item; - - x = shift_x; - y += font_height; - break; - - case 0: - ch_width = measure_character(advance_data, stride, '\\'); - item->index = i; - item->glyphid = '\\'; - item->x0 = x; - item->y0 = y; - item->x1 = x + ch_width; - item->y1 = y + font_height; - ++item_i; - ++item; - x += ch_width; - - ch_width = measure_character(advance_data, stride, '0'); - item->index = i; - item->glyphid = '0'; - item->x0 = x; - item->y0 = y; - item->x1 = x + ch_width; - item->y1 = y + font_height; - ++item_i; - ++item; - x += ch_width; - break; - - case '\r': - ch_width = measure_character(advance_data, stride, '\\'); - item->index = i; - item->glyphid = '\\'; - item->x0 = x; - item->y0 = y; - item->x1 = x + ch_width; - item->y1 = y + font_height; - ++item_i; - ++item; - x += ch_width; - - ch_width = measure_character(advance_data, stride, 'r'); - item->index = i; - item->glyphid = 'r'; - item->x0 = x; - item->y0 = y; - item->x1 = x + ch_width; - item->y1 = y + font_height; - ++item_i; - ++item; - x += ch_width; - break; - - case '\t': - item->index = i; - item->glyphid = '\\'; - item->x0 = x; - item->y0 = y; - item->x1 = x + measure_character(advance_data, stride, '\\'); - item->y1 = y + font_height; - ++item_i; - ++item; - - item->index = i; - item->glyphid = 't'; - item->x0 = (item-1)->x1; - item->y0 = y; - item->x1 = item->x0 + measure_character(advance_data, stride, 't'); - item->y1 = y + font_height; - ++item_i; - ++item; - x += ch_width; - break; - - default: - item->index = i; - item->glyphid = ch; - item->x0 = x; - item->y0 = y; - item->x1 = x + ch_width; - item->y1 = y + font_height; - ++item_i; - ++item; - x += ch_width; - break; - } - if (y > height + shift_y) break; - } - - // TODO(allen): handle this with a control state - assert_4tech(item_i <= max); - *count = item_i; -} - // BOTTOM diff --git a/buffer/4coder_shared.cpp b/buffer/4coder_shared.cpp new file mode 100644 index 00000000..59816c2b --- /dev/null +++ b/buffer/4coder_shared.cpp @@ -0,0 +1,256 @@ +/* + * Mr. 4th Dimention - Allen Webster + * Four Tech + * + * public domain -- no warranty is offered or implied; use this code at your own risk + * + * 23.10.2015 + * + * Items shared by gap buffer types + * + */ + +// TOP + +#ifndef defines_4tech +#define inline_4tech inline +#define internal_4tech static +#define memset_4tech memset +#define memcpy_4tech memcpy +#define memmove_4tech memmove +#define defines_4tech 1 +#define debug_4tech(x) x +#define assert_4tech assert + +#define ceil_4tech CEIL32 +#endif + +inline_4tech float +measure_character(void *advance_data, int stride, char character){ + char *advances; + float width; + + advances = (char*)advance_data; + switch (character){ + case 0: width = *(float*)(advances + stride * '\\') + *(float*)(advances + stride * '0'); break; + case '\n': width = 0; break; + case '\r': width = *(float*)(advances + stride * '\\') + *(float*)(advances + stride * '\r'); break; + default: width = *(float*)(advances + stride * character); + } + + return(width); +} + +typedef struct{ + int str_start, len; + int start, end; +} Buffer_Edit; + +typedef enum{ + buffer_seek_pos, + buffer_seek_wrapped_xy, + buffer_seek_unwrapped_xy, + buffer_seek_line_char +} Buffer_Seek_Type; + + +typedef struct{ + Buffer_Seek_Type type; + union{ + struct { int pos; }; + struct { int round_down; float x, y; }; + struct { int line, character; }; + }; +} Buffer_Seek; + +inline_4tech Buffer_Seek +seek_pos(int pos){ + Buffer_Seek result; + result.type = buffer_seek_pos; + result.pos = pos; + return(result); +} + +inline_4tech Buffer_Seek +seek_wrapped_xy(float x, float y, int round_down){ + Buffer_Seek result; + result.type = buffer_seek_wrapped_xy; + result.x = x; + result.y = y; + result.round_down = round_down; + return(result); +} + +inline_4tech Buffer_Seek +seek_unwrapped_xy(float x, float y, int round_down){ + Buffer_Seek result; + result.type = buffer_seek_unwrapped_xy; + result.x = x; + result.y = y; + result.round_down = round_down; + return(result); +} + +inline_4tech Buffer_Seek +seek_line_char(int line, int character){ + Buffer_Seek result; + result.type = buffer_seek_line_char; + result.line = line; + result.character = character; + return(result); +} + +typedef struct{ + int pos; + int line, character; + float unwrapped_x, unwrapped_y; + float wrapped_x, wrapped_y; +} Full_Cursor; + +typedef struct{ + int index; + int glyphid; + float x0, y0; + float x1, y1; +} Buffer_Render_Item; + +inline_4tech void +write_render_item(Buffer_Render_Item *item, int index, int glyphid, + float x, float y, float w, float h){ + item->index = index; + item->glyphid = glyphid; + item->x0 = x; + item->y0 = y; + item->x1 = x + w; + item->y1 = y + h; +} + +inline_4tech float +write_render_item_inline(Buffer_Render_Item *item, int index, int glyphid, + float x, float y, void *advance_data, int stride, float h){ + float ch_width; + ch_width = measure_character(advance_data, stride, (char)glyphid); + write_render_item(item, index, glyphid, x, y, ch_width, h); + return(ch_width); +} + +typedef struct{ + Full_Cursor cursor, prev_cursor; +} Seek_State; + +internal_4tech int +cursor_seek_step(Seek_State *state, Buffer_Seek seek, int xy_seek, float max_width, float font_height, + char *advances, int stride, int size, char ch){ + Full_Cursor cursor, prev_cursor; + float ch_width; + int result; + float x, px, y; + + cursor = state->cursor; + prev_cursor = state->prev_cursor; + + result = 1; + prev_cursor = cursor; + switch (ch){ + case '\n': + ++cursor.line; + cursor.unwrapped_y += font_height; + cursor.wrapped_y += font_height; + cursor.character = 1; + cursor.unwrapped_x = 0; + cursor.wrapped_x = 0; + break; + + default: + ++cursor.character; + if (ch == '\r') ch_width = *(float*)(advances + stride * '\\') + *(float*)(advances + stride * 'r'); + else ch_width = *(float*)(advances + stride * ch); + + if (cursor.wrapped_x + ch_width >= max_width){ + cursor.wrapped_y += font_height; + cursor.wrapped_x = 0; + prev_cursor = cursor; + } + + cursor.unwrapped_x += ch_width; + cursor.wrapped_x += ch_width; + + break; + } + + ++cursor.pos; + + if (cursor.pos > size){ + cursor = prev_cursor; + result = 0; + goto cursor_seek_step_end; + } + + x = y = px = 0; + + switch (seek.type){ + case buffer_seek_pos: + if (cursor.pos > seek.pos){ + cursor = prev_cursor; + result = 0; + goto cursor_seek_step_end; + }break; + + case buffer_seek_wrapped_xy: + x = cursor.wrapped_x; px = prev_cursor.wrapped_x; + y = cursor.wrapped_y; break; + + case buffer_seek_unwrapped_xy: + x = cursor.unwrapped_x; px = prev_cursor.unwrapped_x; + y = cursor.unwrapped_y; break; + + case buffer_seek_line_char: + if (cursor.line == seek.line && cursor.character >= seek.character){ + result = 0; + goto cursor_seek_step_end; + } + else if (cursor.line > seek.line){ + cursor = prev_cursor; + result = 0; + goto cursor_seek_step_end; + }break; + } + + if (xy_seek){ + if (y > seek.y){ + cursor = prev_cursor; + result = 0; + goto cursor_seek_step_end; + } + + if (y > seek.y - font_height && x >= seek.x){ + if (!seek.round_down){ + if (ch != '\n' && (seek.x - px) < (x - seek.x)) cursor = prev_cursor; + result = 0; + goto cursor_seek_step_end; + } + + if (x > seek.x){ + cursor = prev_cursor; + result = 0; + goto cursor_seek_step_end; + } + } + } + +cursor_seek_step_end: + state->cursor = cursor; + state->prev_cursor = prev_cursor; + + return(result); +} + +inline_4tech int +is_whitespace(char c){ + int result; + result = (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f' || c == '\v'); + return(result); +} + +// BOTTOM + diff --git a/win32_4ed.cpp b/win32_4ed.cpp index 520cdef3..4417e0ab 100644 --- a/win32_4ed.cpp +++ b/win32_4ed.cpp @@ -54,6 +54,8 @@ #define FPS 30 #define FRAME_TIME (1000000 / FPS) +#define BUFFER_EXPERIMENT_SCALPEL 0 + #include "4ed_meta.h" #define FCPP_FORBID_MALLOC @@ -91,7 +93,11 @@ struct Sys_Bubble : public Bubble{ #include "4ed_command.cpp" #include "4ed_layout.cpp" #include "4ed_style.cpp" +#if BUFFER_EXPERIMENT_SCALPEL +#include "4ed_file_view_golden_array.cpp" +#else #include "4ed_file_view.cpp" +#endif #include "4ed_color_view.cpp" #include "4ed_interactive_view.cpp" #include "4ed_menu_view.cpp"