diff --git a/code/4ed_file.cpp b/code/4ed_file.cpp index 50d922f9..b159eeff 100644 --- a/code/4ed_file.cpp +++ b/code/4ed_file.cpp @@ -19,13 +19,11 @@ string_from_file_name(Editing_File_Name *name){ internal void file_edit_positions_set_cursor(File_Edit_Positions *edit_pos, i64 pos){ edit_pos->cursor_pos = pos; - edit_pos->last_set_type = EditPos_CursorSet; } internal void file_edit_positions_set_scroll(File_Edit_Positions *edit_pos, Buffer_Scroll scroll){ edit_pos->scroll = scroll; - edit_pos->last_set_type = EditPos_ScrollSet; } internal void @@ -124,13 +122,13 @@ internal b32 save_file_to_name(Thread_Context *tctx, Models *models, Editing_File *file, u8 *file_name){ b32 result = false; b32 using_actual_file_name = false; - + if (file_name == 0){ file_name_terminate(&file->canon); file_name = file->canon.name_space; using_actual_file_name = true; } - + if (file_name != 0){ if (models->save_file != 0){ Application_Links app = {}; @@ -138,12 +136,12 @@ save_file_to_name(Thread_Context *tctx, Models *models, Editing_File *file, u8 * app.cmd_context = models; models->save_file(&app, file->id); } - + Gap_Buffer *buffer = &file->state.buffer; b32 dos_write_mode = file->settings.dos_write_mode; - + Scratch_Block scratch(tctx); - + if (!using_actual_file_name){ String_Const_u8 s_file_name = SCu8(file_name); String_Const_u8 canonical_file_name = system_get_canonical(scratch, s_file_name); @@ -151,9 +149,9 @@ save_file_to_name(Thread_Context *tctx, Models *models, Editing_File *file, u8 * using_actual_file_name = true; } } - + String_Const_u8 saveable_string = buffer_stringify(scratch, buffer, Ii64(0, buffer_size(buffer))); - + File_Attributes new_attributes = system_save_file(scratch, (char*)file_name, saveable_string); if (new_attributes.last_write_time > 0 && using_actual_file_name){ @@ -163,7 +161,7 @@ save_file_to_name(Thread_Context *tctx, Models *models, Editing_File *file, u8 * LogEventF(log_string(M), scratch, file->id, 0, system_thread_get_id(), "save file [last_write_time=0x%llx]", new_attributes.last_write_time); } - + return(result); } @@ -195,30 +193,30 @@ file_get_layout_func(Editing_File *file){ internal void file_create_from_string(Thread_Context *tctx, Models *models, Editing_File *file, String_Const_u8 val, File_Attributes attributes){ Scratch_Block scratch(tctx); - + Base_Allocator *allocator = tctx->allocator; block_zero_struct(&file->state); buffer_init(&file->state.buffer, val.str, val.size, allocator); - + if (buffer_size(&file->state.buffer) < (i64)val.size){ file->settings.dos_write_mode = true; } file_clear_dirty_flags(file); file->attributes = attributes; - + file->settings.layout_func = models->layout_func; file->settings.face_id = models->global_face_id; - + buffer_measure_starts(scratch, &file->state.buffer); - + file->lifetime_object = lifetime_alloc_object(&models->lifetime_allocator, DynamicWorkspace_Buffer, file); history_init(tctx, models, &file->state.history); - + file->state.cached_layouts_arena = make_arena(allocator); file->state.line_layout_table = make_table_Data_u64(allocator, 500); - + file->settings.is_initialized = true; - + { Temp_Memory temp = begin_temp(scratch); String_Const_u8 name = SCu8(file->unique_name.name_space, file->unique_name.name_size); @@ -228,9 +226,9 @@ file_create_from_string(Thread_Context *tctx, Models *models, Editing_File *file attributes.last_write_time, string_expand(name)); end_temp(temp); } - + //////////////////////////////// - + if (models->begin_buffer != 0){ Application_Links app = {}; app.tctx = tctx; @@ -243,17 +241,17 @@ internal void file_free(Thread_Context *tctx, Models *models, Editing_File *file){ Lifetime_Allocator *lifetime_allocator = &models->lifetime_allocator; Working_Set *working_set = &models->working_set; - + lifetime_free_object(lifetime_allocator, file->lifetime_object); - + Gap_Buffer *buffer = &file->state.buffer; if (buffer->data){ base_free(buffer->allocator, buffer->data); base_free(buffer->allocator, buffer->line_starts); } - + history_free(tctx, &file->state.history); - + linalloc_clear(&file->state.cached_layouts_arena); table_free(&file->state.line_layout_table); } @@ -281,7 +279,7 @@ internal Layout_Item_List file_get_line_layout(Thread_Context *tctx, Models *models, Editing_File *file, Layout_Function *layout_func, f32 width, Face *face, i64 line_number){ Layout_Item_List result = {}; - + i64 line_count = buffer_line_count(&file->state.buffer); if (1 <= line_number && line_number <= line_count){ Line_Layout_Key key = {}; @@ -289,11 +287,11 @@ file_get_line_layout(Thread_Context *tctx, Models *models, Editing_File *file, key.face_version_number = face->version_number; key.width = width; key.line_number = line_number; - + String_Const_u8 key_data = make_data_struct(&key); - + Layout_Item_List *list = 0; - + Table_Lookup lookup = table_lookup(&file->state.line_layout_table, key_data); if (lookup.found_match){ u64 val = 0; @@ -303,7 +301,7 @@ file_get_line_layout(Thread_Context *tctx, Models *models, Editing_File *file, else{ list = push_array(&file->state.cached_layouts_arena, Layout_Item_List, 1); Range_i64 line_range = buffer_get_pos_range_from_line_number(&file->state.buffer, line_number); - + Application_Links app = {}; app.tctx = tctx; app.cmd_context = models; @@ -314,7 +312,7 @@ file_get_line_layout(Thread_Context *tctx, Models *models, Editing_File *file, } block_copy_struct(&result, list); } - + return(result); } @@ -329,9 +327,9 @@ file_line_shift_y(Thread_Context *tctx, Models *models, Editing_File *file, Layout_Function *layout_func, f32 width, Face *face, i64 line_number, f32 y_delta){ Line_Shift_Vertical result = {}; - + f32 line_y = 0.f; - + if (y_delta < 0.f){ // NOTE(allen): Iterating upward b32 has_result = false; @@ -380,7 +378,7 @@ file_line_shift_y(Thread_Context *tctx, Models *models, Editing_File *file, result.y_delta = line_y; } } - + return(result); } @@ -419,11 +417,11 @@ file_relative_box_of_pos(Thread_Context *tctx, Models *models, Editing_File *fil i64 line_number = buffer_get_line_index(&file->state.buffer, pos) + 1; Layout_Item_List line = file_get_line_layout(tctx, models, file, layout_func, width, face, line_number); Rect_f32 result = layout_box_of_pos(line, pos); - + f32 y_difference = file_line_y_difference(tctx, models, file, layout_func, width, face, line_number, base_line); result.y0 += y_difference; result.y1 += y_difference; - + return(result); } @@ -442,11 +440,11 @@ file_padded_box_of_pos(Thread_Context *tctx, Models *models, Editing_File *file, i64 line_number = buffer_get_line_index(&file->state.buffer, pos) + 1; Layout_Item_List line = file_get_line_layout(tctx, models, file, layout_func, width, face, line_number); Rect_f32 result = layout_padded_box_of_pos(line, pos); - + f32 y_difference = file_line_y_difference(tctx, models, file, layout_func, width, face, line_number, base_line); result.y0 += y_difference; result.y1 += y_difference; - + return(result); } @@ -475,9 +473,9 @@ file_buffer_point_difference(Thread_Context *tctx, Models *models, Editing_File internal Line_Shift_Character file_line_shift_characters(Thread_Context *tctx, Models *models, Editing_File *file, Layout_Function *layout_func, f32 width, Face *face, i64 line_number, i64 character_delta){ Line_Shift_Character result = {}; - + i64 line_character = 0; - + if (character_delta < 0){ // NOTE(allen): Iterating upward b32 has_result = false; @@ -524,7 +522,7 @@ file_line_shift_characters(Thread_Context *tctx, Models *models, Editing_File *f result.character_delta = line_character; } } - + return(result); } diff --git a/code/4ed_file.h b/code/4ed_file.h index e292bbb2..909567f8 100644 --- a/code/4ed_file.h +++ b/code/4ed_file.h @@ -12,14 +12,7 @@ #if !defined(FRED_FILE_H) #define FRED_FILE_H -typedef i32 Edit_Pos_Set_Type; -enum{ - EditPos_None, - EditPos_CursorSet, - EditPos_ScrollSet -}; struct File_Edit_Positions{ - Edit_Pos_Set_Type last_set_type; Buffer_Scroll scroll; i64 cursor_pos; }; @@ -50,19 +43,19 @@ enum{ struct Editing_File_State{ Gap_Buffer buffer; - + History history; i32 current_record_index; - + Dirty_State dirty; File_Save_State save_state; - + File_Edit_Positions edit_pos_most_recent; File_Edit_Positions edit_pos_stack[16]; i32 edit_pos_stack_top; - + Child_Process_ID attached_child_process; - + Arena cached_layouts_arena; Table_Data_u64 line_layout_table; }; diff --git a/code/4ed_view.cpp b/code/4ed_view.cpp index 96427bd6..090793b5 100644 --- a/code/4ed_view.cpp +++ b/code/4ed_view.cpp @@ -42,12 +42,12 @@ internal void free_query_slot(Query_Set *set, Query_Bar *match_bar){ Query_Slot *slot = 0; Query_Slot *prev = 0; - + for (slot = set->used_slot; slot != 0; slot = slot->next){ if (slot->query_bar == match_bar) break; prev = slot; } - + if (slot){ if (prev){ prev->next = slot->next; @@ -97,17 +97,17 @@ internal View* live_set_alloc_view(Lifetime_Allocator *lifetime_allocator, Live_Views *live_set, Panel *panel){ Assert(live_set->count < live_set->max); ++live_set->count; - + View *result = live_set->free_sentinel.next; dll_remove(result); block_zero_struct(result); - + result->in_use = true; init_query_set(&result->query_set); result->lifetime_object = lifetime_alloc_object(lifetime_allocator, DynamicWorkspace_View, result); panel->view = result; result->panel = panel; - + return(result); } @@ -115,13 +115,13 @@ internal void live_set_free_view(Lifetime_Allocator *lifetime_allocator, Live_Views *live_set, View *view){ Assert(live_set->count > 0); --live_set->count; - + view->next = live_set->free_sentinel.next; view->prev = &live_set->free_sentinel; live_set->free_sentinel.next = view; view->next->prev = view; view->in_use = false; - + lifetime_free_object(lifetime_allocator, view->lifetime_object); } @@ -335,31 +335,31 @@ view_move_view_to_cursor(Thread_Context *tctx, Models *models, View *view, Buffe Face *face = file_get_face(models, file); Rect_f32 rect = view_get_buffer_rect(tctx, models, view); Vec2_f32 view_dim = rect_dim(rect); - + Layout_Function *layout_func = file_get_layout_func(file); - + File_Edit_Positions edit_pos = view_get_edit_pos(view); Vec2_f32 p = file_relative_xy_of_pos(tctx, models, file, layout_func, view_dim.x, face, scroll->target.line_number, edit_pos.cursor_pos); p -= scroll->target.pixel_shift; - + f32 line_height = face->metrics.line_height; f32 normal_advance = face->metrics.normal_advance; - + Vec2_f32 margin = view->cursor_margin; Vec2_f32 push_in = view->cursor_push_in_multiplier; - + Vec2_f32 lim_dim = view_dim*0.45f; margin.x = clamp_top(margin.x, lim_dim.x); margin.y = clamp_top(margin.y, lim_dim.y); - + Vec2_f32 push_in_lim_dim = hadamard(lim_dim, V2f32(1.f/line_height, 1.f/normal_advance)) - margin; push_in_lim_dim.x = clamp_bot(0.f, push_in_lim_dim.x); push_in_lim_dim.y = clamp_bot(0.f, push_in_lim_dim.y); push_in.x = clamp_top(push_in.x, push_in_lim_dim.x); push_in.y = clamp_top(push_in.y, push_in_lim_dim.y); - + Vec2_f32 target_p_relative = {}; if (p.y < margin.y){ target_p_relative.y = p.y - margin.y - line_height*push_in.y; @@ -377,7 +377,7 @@ view_move_view_to_cursor(Thread_Context *tctx, Models *models, View *view, Buffe scroll->target = view_normalize_buffer_point(tctx, models, view, scroll->target); scroll->target.pixel_shift.x = f32_round32(scroll->target.pixel_shift.x); scroll->target.pixel_shift.y = f32_round32(scroll->target.pixel_shift.y); - + return(target_p_relative != V2f32(0.f, 0.f)); } @@ -387,16 +387,16 @@ view_move_cursor_to_view(Thread_Context *tctx, Models *models, View *view, Buffe Face *face = file_get_face(models, file); Rect_f32 rect = view_get_buffer_rect(tctx, models, view); Vec2_f32 view_dim = rect_dim(rect); - + Layout_Function *layout_func = file_get_layout_func(file); - + Vec2_f32 p = file_relative_xy_of_pos(tctx, models, file, layout_func, view_dim.x, face, scroll.target.line_number, *pos_in_out); p -= scroll.target.pixel_shift; - + f32 line_height = face->metrics.line_height; - + b32 adjusted_y = true; if (p.y < 0.f){ p.y = line_height*1.5f; @@ -407,7 +407,7 @@ view_move_cursor_to_view(Thread_Context *tctx, Models *models, View *view, Buffe else{ adjusted_y = false; } - + b32 result = false; if (adjusted_y){ p += scroll.target.pixel_shift; @@ -416,7 +416,7 @@ view_move_cursor_to_view(Thread_Context *tctx, Models *models, View *view, Buffe scroll.target.line_number, p); result = true; } - + return(result); } @@ -450,7 +450,6 @@ view_set_cursor_and_scroll(Thread_Context *tctx, Models *models, View *view, i64 Vec2_f32 p = view_relative_xy_of_pos(tctx, models, view, cursor.line, pos); view->preferred_x = p.x; file_edit_positions_set_scroll(&edit_pos, scroll); - edit_pos.last_set_type = EditPos_None; view_set_edit_pos(view, edit_pos); } @@ -459,9 +458,9 @@ view_set_cursor_and_scroll(Thread_Context *tctx, Models *models, View *view, i64 internal void view_set_file(Thread_Context *tctx, Models *models, View *view, Editing_File *file){ Assert(file != 0); - + Editing_File *old_file = view->file; - + if (models->view_change_buffer != 0){ Application_Links app = {}; app.tctx = tctx; @@ -469,21 +468,21 @@ view_set_file(Thread_Context *tctx, Models *models, View *view, Editing_File *fi models->view_change_buffer(&app, view_get_id(&models->view_set, view), (old_file != 0)?old_file->id:0, file->id); } - + if (old_file != 0){ file_touch(&models->working_set, old_file); file_edit_positions_push(old_file, view_get_edit_pos(view)); } - + view->file = file; - + File_Edit_Positions edit_pos = file_edit_positions_pop(file); view_set_edit_pos(view, edit_pos); view->mark = edit_pos.cursor_pos; Buffer_Cursor cursor = view_compute_cursor(view, seek_pos(edit_pos.cursor_pos)); Vec2_f32 p = view_relative_xy_of_pos(tctx, models, view, cursor.line, edit_pos.cursor_pos); view->preferred_x = p.x; - + models->layout.panel_state_dirty = true; } @@ -544,7 +543,7 @@ co_handle_request(Thread_Context *tctx, Models *models, Coroutine *co, Co_Out *o in.face_id = (face != 0)?face->id:0; result = coroutine_run(&models->coroutines, co, &in, out); }break; - + case CoRequest_ModifyFace: { Face_Description *description = out->face_description; @@ -553,13 +552,13 @@ co_handle_request(Thread_Context *tctx, Models *models, Coroutine *co, Co_Out *o in.success = font_set_modify_face(&models->font_set, face_id, description); result = coroutine_run(&models->coroutines, co, &in, out); }break; - + case CoRequest_AcquireGlobalFrameMutex: { system_acquire_global_frame_mutex(tctx); result = coroutine_run(&models->coroutines, co, 0, out); }break; - + case CoRequest_ReleaseGlobalFrameMutex: { system_release_global_frame_mutex(tctx); @@ -594,18 +593,18 @@ function void view_init(Thread_Context *tctx, Models *models, View *view, Editing_File *initial_buffer, Custom_Command_Function *event_context_base){ view_set_file(tctx, models, view, initial_buffer); - + view->node_arena = make_arena_system(); - + View_Context first_ctx = {}; first_ctx.render_caller = models->render_caller; first_ctx.delta_rule = models->delta_rule; first_ctx.delta_rule_memory_size = models->delta_rule_memory_size; view_push_context(view, &first_ctx); - + view->cursor_margin = V2f32(0.f, 0.f); view->cursor_push_in_multiplier = V2f32(1.5f, 1.5f); - + view->co = coroutine_create(&models->coroutines, view_event_context_base__inner); view->co->user_data = view; Co_In in = {}; @@ -678,10 +677,10 @@ co_full_abort(Thread_Context *tctx, Models *models, View *view){ function b32 co_send_event(Thread_Context *tctx, Models *models, View *view, Input_Event *event){ b32 event_was_handled = false; - + Coroutine *co = view->co; Co_Out *co_out = &view->co_out; - + { models->current_input_unhandled = false; Co_In in = {}; @@ -695,7 +694,7 @@ co_send_event(Thread_Context *tctx, Models *models, View *view, Input_Event *eve } event_was_handled = !models->current_input_unhandled; } - + return(event_was_handled); } diff --git a/code/4ed_view.h b/code/4ed_view.h index c95ffe54..6adba45b 100644 --- a/code/4ed_view.h +++ b/code/4ed_view.h @@ -62,27 +62,27 @@ struct View{ View *prev; struct Panel *panel; b32 in_use; - + Editing_File *file; Lifetime_Object *lifetime_object; - + File_Edit_Positions edit_pos_; i64 mark; f32 preferred_x; Vec2_f32 cursor_margin; Vec2_f32 cursor_push_in_multiplier; - + b8 new_scroll_target; b8 hide_scrollbar; b8 hide_file_bar; b8 show_whitespace; - + Coroutine *co; Co_Out co_out; - + Arena node_arena; View_Context_Node *ctx; - + Query_Set query_set; };