2017-01-03 20:05:35 +00:00
|
|
|
/*
|
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
*
|
|
|
|
* 03.01.2017
|
|
|
|
*
|
|
|
|
* File layer for 4coder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal Buffer_Slot_ID
|
2017-02-24 02:30:29 +00:00
|
|
|
to_file_id(i32 id){
|
2019-07-13 00:43:17 +00:00
|
|
|
Buffer_Slot_ID result = {};
|
2017-02-24 02:30:29 +00:00
|
|
|
result.id = id;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
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-02-10 09:18:34 +00:00
|
|
|
file_edit_positions_set_cursor(File_Edit_Positions *edit_pos, i32 pos){
|
|
|
|
edit_pos->cursor_pos = pos;
|
2018-03-24 21:43:57 +00:00
|
|
|
edit_pos->last_set_type = EditPos_CursorSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
2019-02-26 19:59:57 +00:00
|
|
|
file_edit_positions_set_scroll(File_Edit_Positions *edit_pos, GUI_Scroll_Vars scroll, i32 max_y){
|
|
|
|
scroll.target_y = clamp(0, scroll.target_y, max_y);
|
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-02-13 22:14:27 +00:00
|
|
|
internal u32
|
|
|
|
file_get_access_flags(Editing_File *file){
|
|
|
|
u32 flags = 0;
|
|
|
|
if (file->settings.read_only){
|
|
|
|
flags |= AccessProtected;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
file->settings.unimportant = (b8)(val != false);
|
|
|
|
}
|
|
|
|
|
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){
|
|
|
|
memset(&file->state, 0, sizeof(file->state));
|
|
|
|
memset(&file->settings, 0, sizeof(file->settings));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
internal b32
|
2019-06-01 23:58:28 +00:00
|
|
|
save_file_to_name(System_Functions *system, 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){
|
2018-03-24 21:43:57 +00:00
|
|
|
Mem_Options *mem = &models->mem;
|
2018-08-18 08:16:52 +00:00
|
|
|
if (models->hook_save_file != 0){
|
2018-03-24 21:43:57 +00:00
|
|
|
models->hook_save_file(&models->app_links, file->id.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-08-18 08:16:52 +00:00
|
|
|
i32 max = 0;
|
2018-03-24 21:43:57 +00:00
|
|
|
if (dos_write_mode){
|
|
|
|
max = buffer_size(buffer) + buffer->line_count + 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
max = buffer_size(buffer);
|
|
|
|
}
|
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
Arena *scratch = &mem->arena;
|
|
|
|
Temp_Memory temp = begin_temp(scratch);
|
2018-03-24 21:43:57 +00:00
|
|
|
char empty = 0;
|
2018-08-18 08:16:52 +00:00
|
|
|
char *data = 0;
|
2018-03-24 21:43:57 +00:00
|
|
|
if (max == 0){
|
|
|
|
data = ∅
|
|
|
|
}
|
|
|
|
else{
|
2019-06-01 23:58:28 +00:00
|
|
|
data = (char*)push_array(scratch, char, max);
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
Assert(data != 0);
|
|
|
|
|
2018-08-18 08:16:52 +00:00
|
|
|
i32 size = 0;
|
2018-03-24 21:43:57 +00:00
|
|
|
if (dos_write_mode){
|
|
|
|
size = buffer_convert_out(buffer, data, max);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
size = max;
|
|
|
|
buffer_stringify(buffer, 0, size, data);
|
|
|
|
}
|
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
if (!using_actual_file_name){
|
2018-03-24 21:43:57 +00:00
|
|
|
char space[512];
|
2019-06-01 23:58:28 +00:00
|
|
|
umem length = cstring_length(file_name);
|
|
|
|
system->get_canonical((char*)file_name, (u32)length, space, sizeof(space));
|
|
|
|
if (string_match(SCu8(space), 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-06-01 23:58:28 +00:00
|
|
|
File_Attributes new_attributes = system->save_file((char*)file_name, data, size);
|
2019-02-13 23:15:22 +00:00
|
|
|
if (new_attributes.last_write_time > 0){
|
|
|
|
if (using_actual_file_name){
|
|
|
|
file->state.ignore_behind_os = 1;
|
|
|
|
}
|
|
|
|
file->attributes = new_attributes;
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:42:13 +00:00
|
|
|
file_clear_dirty_flags(file);
|
2019-06-01 23:58:28 +00:00
|
|
|
end_temp(temp);
|
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
|
2018-03-24 21:43:57 +00:00
|
|
|
save_file(System_Functions *system, Models *models, Editing_File *file){
|
|
|
|
b32 result = save_file_to_name(system, models, file, 0);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2019-06-19 02:31:59 +00:00
|
|
|
internal Partial_Cursor
|
|
|
|
file_compute_partial_cursor(Editing_File *file, Buffer_Seek seek){
|
|
|
|
Partial_Cursor result = {};
|
2017-01-03 20:05:35 +00:00
|
|
|
switch (seek.type){
|
|
|
|
case buffer_seek_pos:
|
|
|
|
{
|
2019-06-20 23:43:27 +00:00
|
|
|
result = buffer_partial_from_pos(&file->state.buffer, (i32)seek.pos);
|
2017-01-03 20:05:35 +00:00
|
|
|
}break;
|
|
|
|
case buffer_seek_line_char:
|
|
|
|
{
|
2019-06-20 23:43:27 +00:00
|
|
|
result = buffer_partial_from_line_character(&file->state.buffer, (i32)seek.line, (i32)seek.character);
|
2017-01-03 20:05:35 +00:00
|
|
|
}break;
|
2019-02-12 02:33:11 +00:00
|
|
|
// TODO(allen): do(support buffer_seek_character_pos and character_pos coordiantes in partial cursor system)
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
internal Full_Cursor
|
2019-07-24 07:41:40 +00:00
|
|
|
file_compute_cursor__inner(Models *models, Editing_File *file, Buffer_Seek seek, b32 return_hint){
|
|
|
|
System_Functions *system = models->system;
|
|
|
|
Face *face = font_set_face_from_id(&models->font_set, file->settings.font_id);
|
2019-07-21 18:16:34 +00:00
|
|
|
Assert(face != 0);
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2018-11-20 08:18:54 +00:00
|
|
|
Full_Cursor result = {};
|
2018-03-24 21:43:57 +00:00
|
|
|
|
|
|
|
Buffer_Cursor_Seek_Params params;
|
|
|
|
params.buffer = &file->state.buffer;
|
|
|
|
params.seek = seek;
|
|
|
|
params.system = system;
|
2019-07-21 18:16:34 +00:00
|
|
|
params.face = face;
|
2018-03-24 21:43:57 +00:00
|
|
|
params.wrap_line_index = file->state.wrap_line_index;
|
|
|
|
params.character_starts = file->state.character_starts;
|
|
|
|
params.virtual_white = file->settings.virtual_white;
|
|
|
|
params.return_hint = return_hint;
|
|
|
|
params.cursor_out = &result;
|
|
|
|
|
2018-11-20 08:18:54 +00:00
|
|
|
Buffer_Cursor_Seek_State state = {};
|
|
|
|
Buffer_Layout_Stop stop = {};
|
2018-03-24 21:43:57 +00:00
|
|
|
|
|
|
|
i32 size = buffer_size(params.buffer);
|
|
|
|
|
|
|
|
f32 line_shift = 0.f;
|
2019-02-10 00:20:55 +00:00
|
|
|
b32 do_wrap = false;
|
2018-03-24 21:43:57 +00:00
|
|
|
i32 wrap_unit_end = 0;
|
|
|
|
|
2019-02-10 00:20:55 +00:00
|
|
|
b32 first_wrap_determination = true;
|
2018-03-24 21:43:57 +00:00
|
|
|
i32 wrap_array_index = 0;
|
|
|
|
|
|
|
|
do{
|
|
|
|
stop = buffer_cursor_seek(&state, params, line_shift, do_wrap, wrap_unit_end);
|
|
|
|
switch (stop.status){
|
|
|
|
case BLStatus_NeedWrapDetermination:
|
|
|
|
{
|
|
|
|
if (stop.pos >= size){
|
2019-02-10 00:20:55 +00:00
|
|
|
do_wrap = false;
|
2018-03-24 21:43:57 +00:00
|
|
|
wrap_unit_end = max_i32;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if (first_wrap_determination){
|
|
|
|
wrap_array_index = binary_search(file->state.wrap_positions, stop.pos, 0, file->state.wrap_position_count);
|
|
|
|
++wrap_array_index;
|
|
|
|
if (file->state.wrap_positions[wrap_array_index] == stop.pos){
|
2019-02-10 00:20:55 +00:00
|
|
|
do_wrap = true;
|
2018-03-24 21:43:57 +00:00
|
|
|
wrap_unit_end = file->state.wrap_positions[wrap_array_index];
|
|
|
|
}
|
|
|
|
else{
|
2019-02-10 00:20:55 +00:00
|
|
|
do_wrap = false;
|
2018-03-24 21:43:57 +00:00
|
|
|
wrap_unit_end = file->state.wrap_positions[wrap_array_index];
|
|
|
|
}
|
2019-02-10 00:20:55 +00:00
|
|
|
first_wrap_determination = false;
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
Assert(stop.pos == wrap_unit_end);
|
2019-02-10 00:20:55 +00:00
|
|
|
do_wrap = true;
|
2018-03-24 21:43:57 +00:00
|
|
|
++wrap_array_index;
|
|
|
|
wrap_unit_end = file->state.wrap_positions[wrap_array_index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case BLStatus_NeedWrapLineShift:
|
|
|
|
case BLStatus_NeedLineShift:
|
|
|
|
{
|
|
|
|
line_shift = file->state.line_indents[stop.wrap_line_index];
|
|
|
|
}break;
|
|
|
|
}
|
|
|
|
}while(stop.status != BLStatus_Finished);
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
2017-01-03 20:05:35 +00:00
|
|
|
|
2019-02-10 00:20:55 +00:00
|
|
|
internal Full_Cursor
|
2019-07-24 07:41:40 +00:00
|
|
|
file_compute_cursor(Models *models, Editing_File *file, Buffer_Seek seek){
|
|
|
|
return(file_compute_cursor__inner(models, file, seek, false));
|
2019-02-10 00:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal Full_Cursor
|
2019-07-24 07:41:40 +00:00
|
|
|
file_compute_cursor_hint(Models *models, Editing_File *file, Buffer_Seek seek){
|
|
|
|
return(file_compute_cursor__inner(models, file, seek, true));
|
2019-02-10 00:20:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
////////////////////////////////
|
2017-01-03 20:05:35 +00:00
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
internal i32
|
2018-08-18 08:16:52 +00:00
|
|
|
file_grow_starts_as_needed(Heap *heap, Gap_Buffer *buffer, i32 additional_lines){
|
2018-03-24 21:43:57 +00:00
|
|
|
b32 result = GROW_NOT_NEEDED;
|
|
|
|
i32 max = buffer->line_max;
|
|
|
|
i32 count = buffer->line_count;
|
|
|
|
i32 target_lines = count + additional_lines;
|
|
|
|
if (target_lines > max || max == 0){
|
2019-06-01 23:58:28 +00:00
|
|
|
max = round_up_i32(target_lines + max, KB(1));
|
2018-08-18 08:16:52 +00:00
|
|
|
i32 *new_lines = heap_array(heap, i32, max);
|
|
|
|
if (new_lines != 0){
|
2018-03-24 21:43:57 +00:00
|
|
|
result = GROW_SUCCESS;
|
2018-08-18 08:16:52 +00:00
|
|
|
memcpy(new_lines, buffer->line_starts, sizeof(*new_lines)*count);
|
|
|
|
heap_free(heap, buffer->line_starts);
|
2018-03-24 21:43:57 +00:00
|
|
|
buffer->line_starts = new_lines;
|
2018-08-18 08:16:52 +00:00
|
|
|
buffer->line_max = max;
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
result = GROW_FAILED;
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
internal void
|
2018-08-18 08:16:52 +00:00
|
|
|
file_measure_starts(Heap *heap, Gap_Buffer *buffer){
|
2018-03-24 21:43:57 +00:00
|
|
|
if (buffer->line_starts == 0){
|
|
|
|
i32 max = buffer->line_max = KB(1);
|
2018-08-18 08:16:52 +00:00
|
|
|
buffer->line_starts = heap_array(heap, i32, max);
|
2018-03-24 21:43:57 +00:00
|
|
|
Assert(buffer->line_starts != 0);
|
|
|
|
}
|
|
|
|
|
2018-11-20 08:18:54 +00:00
|
|
|
Buffer_Measure_Starts state = {};
|
2018-03-24 21:43:57 +00:00
|
|
|
for (;buffer_measure_starts(&state, buffer);){
|
|
|
|
i32 count = state.count;
|
|
|
|
i32 max = ((buffer->line_max + 1) << 1);
|
2018-08-18 08:16:52 +00:00
|
|
|
i32 *new_lines = heap_array(heap, i32, max);
|
|
|
|
Assert(new_lines != 0);
|
|
|
|
memcpy(new_lines, buffer->line_starts, sizeof(*new_lines)*count);
|
|
|
|
heap_free(heap, buffer->line_starts);
|
2018-03-24 21:43:57 +00:00
|
|
|
buffer->line_starts = new_lines;
|
|
|
|
buffer->line_max = max;
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
internal void
|
2018-08-18 08:16:52 +00:00
|
|
|
file_allocate_metadata_as_needed(Heap *heap, Gap_Buffer *buffer, void **mem, i32 *mem_max_count, i32 count, i32 item_size){
|
2018-03-24 21:43:57 +00:00
|
|
|
if (*mem == 0){
|
2019-06-01 23:58:28 +00:00
|
|
|
i32 max = round_up_i32(((count + 1)*2), KB(1));
|
2018-08-18 08:16:52 +00:00
|
|
|
*mem = heap_allocate(heap, max*item_size);
|
2018-03-24 21:43:57 +00:00
|
|
|
*mem_max_count = max;
|
|
|
|
Assert(*mem != 0);
|
|
|
|
}
|
|
|
|
else if (*mem_max_count < count){
|
|
|
|
i32 old_max = *mem_max_count;
|
2019-06-01 23:58:28 +00:00
|
|
|
i32 max = round_up_i32(((count + 1)*2), KB(1));
|
2018-08-18 08:16:52 +00:00
|
|
|
void *new_mem = heap_allocate(heap, item_size*max);
|
|
|
|
memcpy(new_mem, *mem, item_size*old_max);
|
|
|
|
heap_free(heap, *mem);
|
2018-03-24 21:43:57 +00:00
|
|
|
*mem = new_mem;
|
|
|
|
*mem_max_count = max;
|
|
|
|
Assert(*mem != 0);
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal void
|
2018-08-18 08:16:52 +00:00
|
|
|
file_allocate_character_starts_as_needed(Heap *heap, Editing_File *file){
|
|
|
|
file_allocate_metadata_as_needed(heap,
|
2018-05-07 02:47:22 +00:00
|
|
|
&file->state.buffer, (void**)&file->state.character_starts,
|
|
|
|
&file->state.character_start_max, file->state.buffer.line_count + 1, sizeof(i32));
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
internal void
|
2018-08-18 08:16:52 +00:00
|
|
|
file_allocate_indents_as_needed(Heap *heap, Editing_File *file, i32 min_last_index){
|
2018-03-24 21:43:57 +00:00
|
|
|
i32 min_amount = min_last_index + 1;
|
2018-08-18 08:16:52 +00:00
|
|
|
file_allocate_metadata_as_needed(heap,
|
2018-05-07 02:47:22 +00:00
|
|
|
&file->state.buffer, (void**)&file->state.line_indents,
|
|
|
|
&file->state.line_indent_max, min_amount, sizeof(f32));
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal void
|
2018-08-18 08:16:52 +00:00
|
|
|
file_allocate_wraps_as_needed(Heap *heap, Editing_File *file){
|
|
|
|
file_allocate_metadata_as_needed(heap,
|
2018-05-07 02:47:22 +00:00
|
|
|
&file->state.buffer, (void**)&file->state.wrap_line_index,
|
|
|
|
&file->state.wrap_max, file->state.buffer.line_count + 1, sizeof(f32));
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 13:06:42 +00:00
|
|
|
internal void
|
2018-08-18 08:16:52 +00:00
|
|
|
file_allocate_wrap_positions_as_needed(Heap *heap, Editing_File *file, i32 min_last_index){
|
2018-03-24 21:43:57 +00:00
|
|
|
i32 min_amount = min_last_index + 1;
|
2018-08-18 08:16:52 +00:00
|
|
|
file_allocate_metadata_as_needed(heap,
|
2018-05-07 02:47:22 +00:00
|
|
|
&file->state.buffer, (void**)&file->state.wrap_positions,
|
|
|
|
&file->state.wrap_position_max, min_amount, sizeof(f32));
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
internal void
|
2019-06-01 23:58:28 +00:00
|
|
|
file_create_from_string(System_Functions *system, Models *models, Editing_File *file, String_Const_u8 val, File_Attributes attributes){
|
2018-08-18 08:16:52 +00:00
|
|
|
Heap *heap = &models->mem.heap;
|
2019-06-01 23:58:28 +00:00
|
|
|
Arena *scratch = &models->mem.arena;
|
2018-03-24 21:43:57 +00:00
|
|
|
Application_Links *app_links = &models->app_links;
|
|
|
|
|
2019-02-13 23:15:22 +00:00
|
|
|
block_zero_struct(&file->state);
|
2018-03-24 21:43:57 +00:00
|
|
|
Gap_Buffer_Init init = buffer_begin_init(&file->state.buffer, val.str, val.size);
|
2019-02-14 00:24:03 +00:00
|
|
|
for (;buffer_init_need_more(&init);){
|
2018-03-24 21:43:57 +00:00
|
|
|
i32 page_size = buffer_init_page_size(&init);
|
2019-06-01 23:58:28 +00:00
|
|
|
page_size = round_up_i32(page_size, KB(4));
|
2018-03-24 21:43:57 +00:00
|
|
|
if (page_size < KB(4)){
|
|
|
|
page_size = KB(4);
|
|
|
|
}
|
2018-08-18 08:16:52 +00:00
|
|
|
void *data = heap_allocate(heap, page_size);
|
2018-03-24 21:43:57 +00:00
|
|
|
buffer_init_provide_page(&init, data, page_size);
|
2017-02-12 06:01:01 +00:00
|
|
|
}
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
b32 init_success = buffer_end_init(&init);
|
|
|
|
Assert(init_success);
|
2018-03-24 21:43:57 +00:00
|
|
|
|
|
|
|
if (buffer_size(&file->state.buffer) < 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
|
|
|
|
|
|
|
Face_ID font_id = models->global_font_id;
|
|
|
|
file->settings.font_id = font_id;
|
2019-07-24 07:41:40 +00:00
|
|
|
Face *face = font_set_face_from_id(&models->font_set, font_id);
|
2019-07-21 18:16:34 +00:00
|
|
|
Assert(face != 0);
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-02-14 00:24:03 +00:00
|
|
|
file_measure_starts(heap, &file->state.buffer);
|
|
|
|
|
|
|
|
file_allocate_character_starts_as_needed(heap, file);
|
2019-07-21 18:16:34 +00:00
|
|
|
buffer_measure_character_starts(system, &file->state.buffer, file->state.character_starts, 0, file->settings.virtual_white);
|
2019-02-14 00:24:03 +00:00
|
|
|
|
2019-07-21 18:16:34 +00:00
|
|
|
file_measure_wraps(system, &models->mem, file, face);
|
2019-02-14 00:24:03 +00:00
|
|
|
//adjust_views_looking_at_files_to_new_cursor(system, models, file);
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2018-09-14 19:02:02 +00:00
|
|
|
file->lifetime_object = lifetime_alloc_object(heap, &models->lifetime_allocator, DynamicWorkspace_Buffer, file);
|
2019-04-05 23:30:24 +00:00
|
|
|
history_init(&models->app_links, &file->state.history);
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-02-04 01:33:44 +00:00
|
|
|
// TODO(allen): do(cleanup the create and settings dance)
|
|
|
|
// Right now we have this thing where we call hook_open_file, which may or may not
|
|
|
|
// trigger a lex job or serial lex, or might just flag the buffer to say it wants
|
|
|
|
// tokens. Then we check if we need to lex still and do it here too. Better would
|
|
|
|
// be to make sure it always happens in one way.
|
2019-01-31 12:38:24 +00:00
|
|
|
Open_File_Hook_Function *hook_open_file = models->hook_open_file;
|
2018-03-24 21:43:57 +00:00
|
|
|
if (hook_open_file != 0){
|
|
|
|
hook_open_file(app_links, file->id.id);
|
|
|
|
}
|
2018-11-21 07:48:42 +00:00
|
|
|
|
2019-02-04 01:33:44 +00:00
|
|
|
if (file->settings.tokens_exist){
|
|
|
|
if (file->state.token_array.tokens == 0){
|
|
|
|
file_first_lex(system, models, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
file_mark_edit_finished(&models->working_set, file);
|
2018-11-21 07:48:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
file->settings.is_initialized = true;
|
2017-01-03 20:05:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
internal void
|
2019-06-01 23:58:28 +00:00
|
|
|
file_free(System_Functions *system, Heap *heap, Lifetime_Allocator *lifetime_allocator, Working_Set *working_set, Editing_File *file){
|
2018-03-24 21:43:57 +00:00
|
|
|
if (file->state.still_lexing){
|
|
|
|
system->cancel_job(BACKGROUND_THREADS, file->state.lex_job);
|
|
|
|
if (file->state.swap_array.tokens){
|
2018-08-18 08:16:52 +00:00
|
|
|
heap_free(heap, file->state.swap_array.tokens);
|
2018-03-24 21:43:57 +00:00
|
|
|
file->state.swap_array.tokens = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (file->state.token_array.tokens){
|
2018-08-18 08:16:52 +00:00
|
|
|
heap_free(heap, file->state.token_array.tokens);
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 19:02:02 +00:00
|
|
|
lifetime_free_object(heap, lifetime_allocator, file->lifetime_object);
|
|
|
|
|
2018-03-24 21:43:57 +00:00
|
|
|
Gap_Buffer *buffer = &file->state.buffer;
|
|
|
|
if (buffer->data){
|
2018-08-18 08:16:52 +00:00
|
|
|
heap_free(heap, buffer->data);
|
|
|
|
heap_free(heap, buffer->line_starts);
|
2018-03-24 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 08:16:52 +00:00
|
|
|
heap_free(heap, file->state.wrap_line_index);
|
|
|
|
heap_free(heap, file->state.character_starts);
|
|
|
|
heap_free(heap, file->state.line_indents);
|
2018-03-24 21:43:57 +00:00
|
|
|
|
2019-02-08 10:03:48 +00:00
|
|
|
history_free(heap, &file->state.history);
|
2019-02-12 10:21:02 +00:00
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
file_unmark_edit_finished(working_set, file);
|
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-03-30 23:18:13 +00:00
|
|
|
internal b32
|
|
|
|
file_tokens_are_ready(Editing_File *file){
|
|
|
|
return(file->state.token_array.tokens != 0 && file->state.tokens_complete && !file->state.still_lexing);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-01-03 20:05:35 +00:00
|
|
|
// BOTTOM
|
2017-02-24 02:30:29 +00:00
|
|
|
|