2017-01-03 20:05:35 +00:00
|
|
|
/*
|
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
*
|
|
|
|
* 03.01.2017
|
|
|
|
*
|
|
|
|
* File layer for 4coder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
internal String_Const_u8
|
|
|
|
string_from_file_name(Editing_File_Name *name){
|
|
|
|
return(SCu8(name->name_space, name->name_size));
|
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
////////////////////////////////
|
2017-02-24 02:30:29 +00:00
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
internal void
|
2019-09-02 18:59:36 +00:00
|
|
|
file_edit_positions_set_cursor(File_Edit_Positions *edit_pos, i64 pos){
|
2019-02-10 09:18:34 +00:00
|
|
|
edit_pos->cursor_pos = pos;
|
2018-03-24 21:43:57 +00:00
|
|
|
edit_pos->last_set_type = EditPos_CursorSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
2019-09-02 18:59:36 +00:00
|
|
|
file_edit_positions_set_scroll(File_Edit_Positions *edit_pos, Buffer_Scroll scroll){
|
2018-03-24 21:43:57 +00:00
|
|
|
edit_pos->scroll = scroll;
|
|
|
|
edit_pos->last_set_type = EditPos_ScrollSet;
|
|
|
|
}
|
2017-01-03 20:05:35 +00:00
|
|
|
|
2019-02-04 03:51:43 +00:00
|
|
|
internal void
|
2019-02-10 00:20:55 +00:00
|
|
|
file_edit_positions_push(Editing_File *file, File_Edit_Positions edit_pos){
|
2019-02-04 03:51:43 +00:00
|
|
|
if (file->state.edit_pos_stack_top + 1 < ArrayCount(file->state.edit_pos_stack)){
|
|
|
|
file->state.edit_pos_stack_top += 1;
|
|
|
|
file->state.edit_pos_stack[file->state.edit_pos_stack_top] = edit_pos;
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 03:51:43 +00:00
|
|
|
internal File_Edit_Positions
|
2019-02-10 00:20:55 +00:00
|
|
|
file_edit_positions_pop(Editing_File *file){
|
2019-02-04 03:51:43 +00:00
|
|
|
File_Edit_Positions edit_pos = {};
|
|
|
|
if (file->state.edit_pos_stack_top >= 0){
|
|
|
|
edit_pos = file->state.edit_pos_stack[file->state.edit_pos_stack_top];
|
|
|
|
file->state.edit_pos_stack_top -= 1;
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
2019-02-09 22:48:53 +00:00
|
|
|
else{
|
|
|
|
edit_pos = file->state.edit_pos_most_recent;
|
|
|
|
}
|
2019-02-04 03:51:43 +00:00
|
|
|
return(edit_pos);
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
////////////////////////////////
|
2017-01-03 20:05:35 +00:00
|
|
|
|
2019-09-02 21:32:52 +00:00
|
|
|
internal Face*
|
|
|
|
file_get_face(Models *models, Editing_File *file){
|
|
|
|
return(font_set_face_from_id(&models->font_set, file->settings.face_id));
|
|
|
|
}
|
|
|
|
|
2019-10-18 02:54:02 +00:00
|
|
|
internal Access_Flag
|
2019-02-13 22:14:27 +00:00
|
|
|
file_get_access_flags(Editing_File *file){
|
2019-10-18 02:54:02 +00:00
|
|
|
Access_Flag flags = Access_Read|Access_Visible;
|
|
|
|
if (!file->settings.read_only){
|
|
|
|
flags |= Access_Write;
|
2019-02-13 22:14:27 +00:00
|
|
|
}
|
|
|
|
return(flags);
|
|
|
|
}
|
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal b32
|
2019-01-29 05:36:17 +00:00
|
|
|
file_needs_save(Editing_File *file){
|
2018-03-24 21:43:57 +00:00
|
|
|
b32 result = false;
|
2019-02-26 21:05:02 +00:00
|
|
|
if (HasFlag(file->state.dirty, DirtyState_UnsavedChanges)){
|
2018-03-24 21:43:57 +00:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
2017-01-03 20:05:35 +00:00
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal b32
|
2019-01-29 05:36:17 +00:00
|
|
|
file_can_save(Editing_File *file){
|
2018-03-24 21:43:57 +00:00
|
|
|
b32 result = false;
|
2019-02-25 23:42:13 +00:00
|
|
|
if (HasFlag(file->state.dirty, DirtyState_UnsavedChanges) ||
|
|
|
|
HasFlag(file->state.dirty, DirtyState_UnloadedChanges)){
|
2018-03-24 21:43:57 +00:00
|
|
|
result = true;
|
|
|
|
}
|
2017-01-03 20:05:35 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal b32
|
2018-03-24 21:43:57 +00:00
|
|
|
file_is_ready(Editing_File *file){
|
|
|
|
b32 result = false;
|
|
|
|
if (file != 0 && file->is_loading == 0){
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal void
|
2018-05-07 02:47:22 +00:00
|
|
|
file_set_unimportant(Editing_File *file, b32 val){
|
|
|
|
if (val){
|
|
|
|
file->state.dirty = DirtyState_UpToDate;
|
|
|
|
}
|
2019-08-13 04:19:02 +00:00
|
|
|
file->settings.unimportant = (b8)(val);
|
2018-05-07 02:47:22 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal void
|
2018-03-24 21:43:57 +00:00
|
|
|
file_set_to_loading(Editing_File *file){
|
2019-08-13 04:19:02 +00:00
|
|
|
block_zero_struct(&file->state);
|
|
|
|
block_zero_struct(&file->settings);
|
2018-03-24 21:43:57 +00:00
|
|
|
file->is_loading = true;
|
|
|
|
}
|
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal void
|
2019-02-25 23:42:13 +00:00
|
|
|
file_add_dirty_flag(Editing_File *file, Dirty_State state){
|
2018-03-24 21:43:57 +00:00
|
|
|
if (!file->settings.unimportant){
|
2019-02-25 23:42:13 +00:00
|
|
|
file->state.dirty |= state;
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
file->state.dirty = DirtyState_UpToDate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:42:13 +00:00
|
|
|
internal void
|
|
|
|
file_clear_dirty_flags(Editing_File *file){
|
|
|
|
file->state.dirty = DirtyState_UpToDate;
|
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
internal void
|
|
|
|
file_name_terminate(Editing_File_Name *name){
|
|
|
|
umem size = name->name_size;
|
|
|
|
size = clamp_top(size, sizeof(name->name_space) - 1);
|
|
|
|
name->name_space[size] = 0;
|
|
|
|
name->name_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2019-08-04 00:49:40 +00:00
|
|
|
// TODO(allen): file_name should be String_Const_u8
|
2018-03-24 21:43:57 +00:00
|
|
|
internal b32
|
2019-10-03 17:57:44 +00:00
|
|
|
save_file_to_name(Models *models, Editing_File *file, u8 *file_name){
|
2018-03-24 21:43:57 +00:00
|
|
|
b32 result = false;
|
2019-02-13 22:14:27 +00:00
|
|
|
b32 using_actual_file_name = false;
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-02-13 22:14:27 +00:00
|
|
|
if (file_name == 0){
|
2019-06-01 23:58:28 +00:00
|
|
|
file_name_terminate(&file->canon);
|
|
|
|
file_name = file->canon.name_space;
|
2019-02-13 22:14:27 +00:00
|
|
|
using_actual_file_name = true;
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 22:14:27 +00:00
|
|
|
if (file_name != 0){
|
2019-10-14 22:57:47 +00:00
|
|
|
if (models->save_file != 0){
|
|
|
|
models->save_file(&models->app_links, file->id);
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Gap_Buffer *buffer = &file->state.buffer;
|
2018-08-18 08:16:52 +00:00
|
|
|
b32 dos_write_mode = file->settings.dos_write_mode;
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-10-01 02:06:21 +00:00
|
|
|
Scratch_Block scratch(models->tctx, Scratch_Share);
|
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
if (!using_actual_file_name){
|
2019-08-04 00:49:40 +00:00
|
|
|
String_Const_u8 s_file_name = SCu8(file_name);
|
2019-10-03 17:57:44 +00:00
|
|
|
String_Const_u8 canonical_file_name = system_get_canonical(scratch, s_file_name);
|
2019-08-04 00:49:40 +00:00
|
|
|
if (string_match(canonical_file_name, string_from_file_name(&file->canon))){
|
2019-02-13 22:14:27 +00:00
|
|
|
using_actual_file_name = true;
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
String_Const_u8 saveable_string = buffer_stringify(scratch, buffer, Ii64(0, buffer_size(buffer)));
|
|
|
|
|
2019-10-03 17:57:44 +00:00
|
|
|
File_Attributes new_attributes = system_save_file(scratch, (char*)file_name, saveable_string);
|
2019-10-08 01:08:42 +00:00
|
|
|
if (new_attributes.last_write_time > 0 &&
|
|
|
|
using_actual_file_name){
|
|
|
|
file->state.save_state = FileSaveState_SavedWaitingForNotification;
|
|
|
|
file_clear_dirty_flags(file);
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
2019-10-03 17:57:44 +00:00
|
|
|
LogEventF(log_string(M), scratch, file->id, 0, system_thread_get_id(),
|
2019-08-16 02:54:06 +00:00
|
|
|
"save file [last_write_time=0x%llx]", new_attributes.last_write_time);
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 20:05:35 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal b32
|
2019-10-03 17:57:44 +00:00
|
|
|
save_file(Models *models, Editing_File *file){
|
|
|
|
return(save_file_to_name(models, file, 0));
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
internal Buffer_Cursor
|
|
|
|
file_compute_cursor(Editing_File *file, Buffer_Seek seek){
|
|
|
|
Buffer_Cursor result = {};
|
2017-01-03 20:05:35 +00:00
|
|
|
switch (seek.type){
|
|
|
|
case buffer_seek_pos:
|
|
|
|
{
|
2019-09-02 18:59:36 +00:00
|
|
|
result = buffer_cursor_from_pos(&file->state.buffer, seek.pos);
|
2017-01-03 20:05:35 +00:00
|
|
|
}break;
|
2019-09-02 18:59:36 +00:00
|
|
|
case buffer_seek_line_col:
|
2017-01-03 20:05:35 +00:00
|
|
|
{
|
2019-09-02 18:59:36 +00:00
|
|
|
result = buffer_cursor_from_line_col(&file->state.buffer, seek.line, seek.col);
|
2017-01-03 20:05:35 +00:00
|
|
|
}break;
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
internal void
|
2019-09-02 18:59:36 +00:00
|
|
|
file_create_from_string(Models *models, Editing_File *file, String_Const_u8 val, File_Attributes attributes){
|
2019-10-01 02:06:21 +00:00
|
|
|
Thread_Context *tctx = models->tctx;
|
|
|
|
Scratch_Block scratch(tctx, Scratch_Share);
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-10-01 02:06:21 +00:00
|
|
|
Base_Allocator *allocator = tctx->allocator;
|
2019-02-13 23:15:22 +00:00
|
|
|
block_zero_struct(&file->state);
|
2019-09-02 18:59:36 +00:00
|
|
|
buffer_init(&file->state.buffer, val.str, val.size, allocator);
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
if (buffer_size(&file->state.buffer) < (i64)val.size){
|
2019-02-13 23:15:22 +00:00
|
|
|
file->settings.dos_write_mode = true;
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
2019-02-25 23:42:13 +00:00
|
|
|
file_clear_dirty_flags(file);
|
2019-02-13 23:15:22 +00:00
|
|
|
file->attributes = attributes;
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-09-02 21:32:52 +00:00
|
|
|
file->settings.face_id = models->global_face_id;
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
buffer_measure_starts(scratch, &file->state.buffer);
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-09-04 05:31:35 +00:00
|
|
|
file->lifetime_object = lifetime_alloc_object(&models->lifetime_allocator, DynamicWorkspace_Buffer, file);
|
2019-10-01 02:06:21 +00:00
|
|
|
history_init(models, &file->state.history);
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
file->state.cached_layouts_arena = make_arena(allocator);
|
|
|
|
file->state.line_layout_table = make_table_Data_u64(allocator, 500);
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
file->settings.is_initialized = true;
|
2019-08-16 02:54:06 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
Temp_Memory temp = begin_temp(scratch);
|
|
|
|
String_Const_u8 name = SCu8(file->unique_name.name_space, file->unique_name.name_size);
|
|
|
|
name = string_escape(scratch, name);
|
2019-10-03 17:57:44 +00:00
|
|
|
LogEventF(log_string(M), scratch, file->id, 0, system_thread_get_id(),
|
2019-08-24 01:34:42 +00:00
|
|
|
"init file [lwt=0x%llx] [name=\"%.*s\"]",
|
2019-08-16 02:54:06 +00:00
|
|
|
attributes.last_write_time, string_expand(name));
|
|
|
|
end_temp(temp);
|
|
|
|
}
|
2019-09-04 05:31:35 +00:00
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2019-10-14 22:57:47 +00:00
|
|
|
if (models->begin_buffer != 0){
|
|
|
|
models->begin_buffer(&models->app_links, file->id);
|
2019-09-04 05:31:35 +00:00
|
|
|
}
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
internal void
|
2019-10-01 02:06:21 +00:00
|
|
|
file_free(Models *models, Editing_File *file){
|
|
|
|
Lifetime_Allocator *lifetime_allocator = &models->lifetime_allocator;
|
|
|
|
Working_Set *working_set = &models->working_set;
|
|
|
|
|
2019-09-04 05:31:35 +00:00
|
|
|
lifetime_free_object(lifetime_allocator, file->lifetime_object);
|
2018-09-14 19:02:02 +00:00
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
Gap_Buffer *buffer = &file->state.buffer;
|
|
|
|
if (buffer->data){
|
2019-09-02 18:59:36 +00:00
|
|
|
base_free(buffer->allocator, buffer->data);
|
|
|
|
base_free(buffer->allocator, buffer->line_starts);
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 02:06:21 +00:00
|
|
|
history_free(models, &file->state.history);
|
2019-02-12 10:21:02 +00:00
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
linalloc_clear(&file->state.cached_layouts_arena);
|
|
|
|
table_free(&file->state.line_layout_table);
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 10:03:48 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
internal i32
|
|
|
|
file_get_current_record_index(Editing_File *file){
|
|
|
|
return(file->state.current_record_index);
|
|
|
|
}
|
|
|
|
|
2019-04-05 23:30:24 +00:00
|
|
|
internal Managed_Scope
|
|
|
|
file_get_managed_scope(Editing_File *file){
|
|
|
|
Managed_Scope scope = 0;
|
|
|
|
if (file != 0){
|
|
|
|
Assert(file->lifetime_object != 0);
|
|
|
|
scope = (Managed_Scope)file->lifetime_object->workspace.scope_id;
|
|
|
|
}
|
|
|
|
return(scope);
|
|
|
|
}
|
|
|
|
|
2019-09-02 18:59:36 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
internal Buffer_Layout_Item_List
|
2019-09-02 21:32:52 +00:00
|
|
|
file_get_line_layout(Models *models, Editing_File *file, f32 width, Face *face, i64 line_number){
|
2019-09-02 18:59:36 +00:00
|
|
|
Buffer_Layout_Item_List result = {};
|
|
|
|
|
|
|
|
i64 line_count = buffer_line_count(&file->state.buffer);
|
|
|
|
if (1 <= line_number && line_number <= line_count){
|
2019-09-02 21:32:52 +00:00
|
|
|
Line_Layout_Key key = {};
|
|
|
|
key.face_id = face->id;
|
|
|
|
key.face_version_number = face->version_number;
|
|
|
|
key.width = width;
|
|
|
|
key.line_number = line_number;
|
|
|
|
|
|
|
|
Data key_data = make_data_struct(&key);
|
|
|
|
|
|
|
|
Buffer_Layout_Item_List *list = 0;
|
|
|
|
|
|
|
|
Table_Lookup lookup = table_lookup(&file->state.line_layout_table, key_data);
|
|
|
|
if (lookup.found_match){
|
|
|
|
u64 val = 0;
|
|
|
|
table_read(&file->state.line_layout_table, lookup, &val);
|
|
|
|
list = (Buffer_Layout_Item_List*)IntAsPtr(val);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
list = push_array(&file->state.cached_layouts_arena, Buffer_Layout_Item_List, 1);
|
|
|
|
Interval_i64 line_range = buffer_get_pos_range_from_line_number(&file->state.buffer, line_number);
|
2019-10-01 02:06:21 +00:00
|
|
|
*list = buffer_layout(models->tctx, &file->state.cached_layouts_arena,
|
2019-09-02 21:32:52 +00:00
|
|
|
&file->state.buffer, line_range, face, width);
|
|
|
|
key_data = push_data_copy(&file->state.cached_layouts_arena, key_data);
|
|
|
|
table_insert(&file->state.line_layout_table, key_data, (u64)PtrAsInt(list));
|
2019-09-02 18:59:36 +00:00
|
|
|
}
|
2019-09-02 21:32:52 +00:00
|
|
|
block_copy_struct(&result, list);
|
2019-09-02 18:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
|
|
|
file_clear_layout_cache(Editing_File *file){
|
|
|
|
linalloc_clear(&file->state.cached_layouts_arena);
|
|
|
|
table_clear(&file->state.line_layout_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Line_Shift_Vertical
|
2019-09-02 21:32:52 +00:00
|
|
|
file_line_shift_y(Models *models, Editing_File *file, f32 width, Face *face, i64 line_number, f32 y_delta){
|
2019-09-02 18:59:36 +00:00
|
|
|
Line_Shift_Vertical result = {};
|
|
|
|
|
|
|
|
f32 line_y = 0.f;
|
|
|
|
|
|
|
|
if (y_delta < 0.f){
|
|
|
|
// NOTE(allen): Iterating upward
|
|
|
|
b32 has_result = false;
|
|
|
|
for (;;){
|
|
|
|
if (line_y <= y_delta){
|
|
|
|
has_result = true;
|
|
|
|
result.line = line_number;
|
|
|
|
result.y_delta = line_y;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line_number -= 1;
|
|
|
|
if (line_number <= 0){
|
|
|
|
line_number = 1;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-02 21:32:52 +00:00
|
|
|
Buffer_Layout_Item_List line = file_get_line_layout(models, file, width, face, line_number);
|
2019-09-02 18:59:36 +00:00
|
|
|
line_y -= line.height;
|
|
|
|
}
|
|
|
|
if (!has_result){
|
|
|
|
result.line = line_number;
|
|
|
|
result.y_delta = line_y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
// NOTE(allen): Iterating downward
|
|
|
|
b32 has_result = false;
|
|
|
|
i64 line_count = buffer_line_count(&file->state.buffer);
|
|
|
|
for (;;line_number += 1){
|
2019-09-02 21:32:52 +00:00
|
|
|
Buffer_Layout_Item_List line = file_get_line_layout(models, file, width, face, line_number);
|
2019-09-02 18:59:36 +00:00
|
|
|
f32 next_y = line_y + line.height;
|
|
|
|
if (y_delta < next_y){
|
|
|
|
has_result = true;
|
|
|
|
result.line = line_number;
|
|
|
|
result.y_delta = line_y;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (line_number >= line_count){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line_y = next_y;
|
|
|
|
}
|
|
|
|
if (!has_result){
|
|
|
|
result.line = line_number;
|
|
|
|
result.y_delta = line_y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal f32
|
2019-09-02 21:32:52 +00:00
|
|
|
file_line_y_difference(Models *models, Editing_File *file, f32 width, Face *face, i64 line_a, i64 line_b){
|
2019-09-02 18:59:36 +00:00
|
|
|
f32 result = 0.f;
|
|
|
|
if (line_a != line_b){
|
|
|
|
Interval_i64 line_range = Ii64(line_a, line_b);
|
|
|
|
for (i64 i = line_range.min; i < line_range.max; i += 1){
|
2019-09-02 21:32:52 +00:00
|
|
|
Buffer_Layout_Item_List line = file_get_line_layout(models, file, width, face, i);
|
2019-09-02 18:59:36 +00:00
|
|
|
result += line.height;
|
|
|
|
}
|
|
|
|
if (line_a < line_b){
|
|
|
|
result *= -1.f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal i64
|
2019-09-02 21:32:52 +00:00
|
|
|
file_pos_at_relative_xy(Models *models, Editing_File *file, f32 width, Face *face, i64 base_line, Vec2_f32 relative_xy){
|
|
|
|
Line_Shift_Vertical shift = file_line_shift_y(models, file, width, face, base_line, relative_xy.y);
|
2019-09-02 18:59:36 +00:00
|
|
|
relative_xy.y -= shift.y_delta;
|
2019-09-02 21:32:52 +00:00
|
|
|
Buffer_Layout_Item_List line = file_get_line_layout(models, file, width, face, shift.line);
|
2019-09-02 18:59:36 +00:00
|
|
|
return(buffer_layout_nearest_pos_to_xy(line, relative_xy));
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Vec2_f32
|
2019-09-02 21:32:52 +00:00
|
|
|
file_relative_xy_of_pos(Models *models, Editing_File *file, f32 width, Face *face, i64 base_line, i64 pos){
|
2019-09-02 18:59:36 +00:00
|
|
|
i64 line_number = buffer_get_line_index(&file->state.buffer, pos) + 1;
|
2019-09-02 21:32:52 +00:00
|
|
|
Buffer_Layout_Item_List line = file_get_line_layout(models, file, width, face, line_number);
|
2019-09-02 18:59:36 +00:00
|
|
|
Vec2_f32 result = buffer_layout_xy_center_of_pos(line, pos);
|
2019-09-02 21:32:52 +00:00
|
|
|
result.y += file_line_y_difference(models, file, width, face, line_number, base_line);
|
2019-09-02 18:59:36 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Buffer_Point
|
2019-09-02 21:32:52 +00:00
|
|
|
file_normalize_buffer_point(Models *models, Editing_File *file, f32 width, Face *face, Buffer_Point point){
|
|
|
|
Line_Shift_Vertical shift = file_line_shift_y(models, file, width, face, point.line_number, point.pixel_shift.y);
|
2019-09-02 18:59:36 +00:00
|
|
|
point.line_number = shift.line;
|
|
|
|
point.pixel_shift.y -= shift.y_delta;
|
|
|
|
point.pixel_shift.x = clamp_bot(0.f, point.pixel_shift.x);
|
|
|
|
point.pixel_shift.y = clamp_bot(0.f, point.pixel_shift.y);
|
|
|
|
return(point);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Vec2_f32
|
2019-09-02 21:32:52 +00:00
|
|
|
file_buffer_point_difference(Models *models, Editing_File *file, f32 width, Face *face, Buffer_Point a, Buffer_Point b){
|
|
|
|
f32 y_difference = file_line_y_difference(models, file, width, face, a.line_number, b.line_number);
|
2019-09-02 18:59:36 +00:00
|
|
|
Vec2_f32 result = a.pixel_shift - b.pixel_shift;
|
|
|
|
result.y += y_difference;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Line_Shift_Character
|
2019-09-02 21:32:52 +00:00
|
|
|
file_line_shift_characters(Models *models, Editing_File *file, f32 width, Face *face, i64 line_number, i64 character_delta){
|
2019-09-02 18:59:36 +00:00
|
|
|
Line_Shift_Character result = {};
|
|
|
|
|
|
|
|
i64 line_character = 0;
|
|
|
|
|
|
|
|
if (character_delta < 0){
|
|
|
|
// NOTE(allen): Iterating upward
|
|
|
|
b32 has_result = false;
|
|
|
|
for (;;){
|
|
|
|
if (line_character <= character_delta){
|
|
|
|
has_result = true;
|
|
|
|
result.line = line_number;
|
|
|
|
result.character_delta = line_character;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line_number -= 1;
|
|
|
|
if (line_number <= 0){
|
|
|
|
line_number = 1;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-02 21:32:52 +00:00
|
|
|
Buffer_Layout_Item_List line = file_get_line_layout(models, file, width, face, line_number);
|
2019-09-02 18:59:36 +00:00
|
|
|
line_character -= line.character_count;
|
|
|
|
}
|
|
|
|
if (!has_result){
|
|
|
|
result.line = line_number;
|
|
|
|
result.character_delta = line_character;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
// NOTE(allen): Iterating downward
|
|
|
|
b32 has_result = false;
|
|
|
|
i64 line_count = buffer_line_count(&file->state.buffer);
|
|
|
|
for (;;line_number += 1){
|
2019-09-02 21:32:52 +00:00
|
|
|
Buffer_Layout_Item_List line = file_get_line_layout(models, file, width, face, line_number);
|
2019-09-02 18:59:36 +00:00
|
|
|
i64 next_character = line_character + line.character_count;
|
|
|
|
if (character_delta < next_character){
|
|
|
|
has_result = true;
|
|
|
|
result.line = line_number;
|
|
|
|
result.character_delta = line_character;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (line_number >= line_count){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line_character = next_character;
|
|
|
|
}
|
|
|
|
if (!has_result){
|
|
|
|
result.line = line_number;
|
|
|
|
result.character_delta = line_character;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal i64
|
2019-09-02 21:32:52 +00:00
|
|
|
file_line_character_difference(Models *models, Editing_File *file, f32 width, Face *face, i64 line_a, i64 line_b){
|
2019-09-02 18:59:36 +00:00
|
|
|
i64 result = 0;
|
|
|
|
if (line_a != line_b){
|
|
|
|
Interval_i64 line_range = Ii64(line_a, line_b);
|
|
|
|
for (i64 i = line_range.min; i < line_range.max; i += 1){
|
2019-09-02 21:32:52 +00:00
|
|
|
Buffer_Layout_Item_List line = file_get_line_layout(models, file, width, face, i);
|
2019-09-02 18:59:36 +00:00
|
|
|
result += line.character_count;
|
|
|
|
}
|
|
|
|
if (line_a < line_b){
|
|
|
|
result *= -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal i64
|
2019-09-02 21:32:52 +00:00
|
|
|
file_pos_from_relative_character(Models *models, Editing_File *file, f32 width, Face *face, i64 base_line, i64 relative_character){
|
|
|
|
Line_Shift_Character shift = file_line_shift_characters(models, file, width, face, base_line, relative_character);
|
2019-09-02 18:59:36 +00:00
|
|
|
relative_character -= shift.character_delta;
|
2019-09-02 21:32:52 +00:00
|
|
|
Buffer_Layout_Item_List line = file_get_line_layout(models, file, width, face, shift.line);
|
2019-09-02 18:59:36 +00:00
|
|
|
return(buffer_layout_get_pos_at_character(line, relative_character));
|
|
|
|
}
|
|
|
|
|
|
|
|
internal i64
|
2019-09-02 21:32:52 +00:00
|
|
|
file_relative_character_from_pos(Models *models, Editing_File *file, f32 width, Face *face, i64 base_line, i64 pos){
|
2019-09-02 18:59:36 +00:00
|
|
|
i64 line_number = buffer_get_line_index(&file->state.buffer, pos) + 1;
|
2019-09-02 21:32:52 +00:00
|
|
|
Buffer_Layout_Item_List line = file_get_line_layout(models, file, width, face, line_number);
|
2019-09-02 18:59:36 +00:00
|
|
|
i64 result = buffer_layout_character_from_pos(line, pos);
|
2019-09-02 21:32:52 +00:00
|
|
|
result += file_line_character_difference(models, file, width, face, line_number, base_line);
|
2019-09-02 18:59:36 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2017-01-03 20:05:35 +00:00
|
|
|
// BOTTOM
|
2017-02-24 02:30:29 +00:00
|
|
|
|