forked from B-Y-P/4coder-community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4ed_file.cpp
568 lines (485 loc) · 19.3 KB
/
4ed_file.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* Mr. 4th Dimention - Allen Webster
*
* 03.01.2017
*
* File layer for 4coder
*
*/
// TOP
internal String_Const_u8
string_from_file_name(Editing_File_Name *name){
return(SCu8(name->name_space, name->name_size));
}
////////////////////////////////
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
file_edit_positions_push(Editing_File *file, File_Edit_Positions edit_pos){
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;
}
}
internal File_Edit_Positions
file_edit_positions_pop(Editing_File *file){
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;
}
else{
edit_pos = file->state.edit_pos_most_recent;
}
return(edit_pos);
}
////////////////////////////////
internal Face*
file_get_face(Models *models, Editing_File *file){
return(font_set_face_from_id(&models->font_set, file->settings.face_id));
}
internal Access_Flag
file_get_access_flags(Editing_File *file){
Access_Flag flags = Access_Read|Access_Visible;
if (!file->settings.read_only){
flags |= Access_Write;
}
return(flags);
}
internal b32
file_needs_save(Editing_File *file){
b32 result = false;
if (HasFlag(file->state.dirty, DirtyState_UnsavedChanges)){
result = true;
}
return(result);
}
internal b32
file_can_save(Editing_File *file){
b32 result = false;
if (HasFlag(file->state.dirty, DirtyState_UnsavedChanges) ||
HasFlag(file->state.dirty, DirtyState_UnloadedChanges)){
result = true;
}
return(result);
}
internal void
file_set_unimportant(Editing_File *file, b32 val){
if (val){
file->state.dirty = DirtyState_UpToDate;
}
file->settings.unimportant = (b8)(val);
}
internal void
file_add_dirty_flag(Editing_File *file, Dirty_State state){
if (!file->settings.unimportant){
file->state.dirty |= state;
}
else{
file->state.dirty = DirtyState_UpToDate;
}
}
internal void
file_clear_dirty_flags(Editing_File *file){
file->state.dirty = DirtyState_UpToDate;
}
////////////////////////////////
internal void
file_name_terminate(Editing_File_Name *name){
u64 size = name->name_size;
size = clamp_top(size, sizeof(name->name_space) - 1);
name->name_space[size] = 0;
name->name_size = size;
}
////////////////////////////////
// TODO(allen): file_name should be String_Const_u8
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 = {};
app.tctx = tctx;
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);
if (string_match(canonical_file_name, string_from_file_name(&file->canon))){
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){
file->state.save_state = FileSaveState_SavedWaitingForNotification;
file_clear_dirty_flags(file);
}
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);
}
////////////////////////////////
internal Buffer_Cursor
file_compute_cursor(Editing_File *file, Buffer_Seek seek){
Buffer_Cursor result = {};
switch (seek.type){
case buffer_seek_pos:
{
result = buffer_cursor_from_pos(&file->state.buffer, seek.pos);
}break;
case buffer_seek_line_col:
{
result = buffer_cursor_from_line_col(&file->state.buffer, seek.line, seek.col);
}break;
}
return(result);
}
////////////////////////////////
function Layout_Function*
file_get_layout_func(Editing_File *file){
return(file->settings.layout_func);
}
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);
name = string_escape(scratch, name);
LogEventF(log_string(M), scratch, file->id, 0, system_thread_get_id(),
"init file [lwt=0x%llx] [name=\"%.*s\"]",
attributes.last_write_time, string_expand(name));
end_temp(temp);
}
////////////////////////////////
if (models->begin_buffer != 0){
Application_Links app = {};
app.tctx = tctx;
app.cmd_context = models;
models->begin_buffer(&app, file->id);
}
}
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);
}
////////////////////////////////
internal i32
file_get_current_record_index(Editing_File *file){
return(file->state.current_record_index);
}
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);
}
////////////////////////////////
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 = {};
key.face_id = face->id;
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;
table_read(&file->state.line_layout_table, lookup, &val);
list = (Layout_Item_List*)IntAsPtr(val);
}
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;
*list = layout_func(&app, &file->state.cached_layouts_arena,
file->id, line_range, face->id, 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));
}
block_copy_struct(&result, list);
}
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
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;
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;
}
Layout_Item_List line = file_get_line_layout(tctx, models, file, layout_func,
width, face, line_number);
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){
Layout_Item_List line = file_get_line_layout(tctx, models, file, layout_func,
width, face, line_number);
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
file_line_y_difference(Thread_Context *tctx, Models *models, Editing_File *file,
Layout_Function *layout_func, f32 width, Face *face,
i64 line_a, i64 line_b){
f32 result = 0.f;
if (line_a != line_b){
Range_i64 line_range = Ii64(line_a, line_b);
for (i64 i = line_range.min; i < line_range.max; i += 1){
Layout_Item_List line = file_get_line_layout(tctx, models, file, layout_func, width, face, i);
result += line.height;
}
if (line_a < line_b){
result *= -1.f;
}
}
return(result);
}
internal i64
file_pos_at_relative_xy(Thread_Context *tctx, Models *models, Editing_File *file,
Layout_Function *layout_func, f32 width, Face *face,
i64 base_line, Vec2_f32 relative_xy){
Line_Shift_Vertical shift = file_line_shift_y(tctx, models, file, layout_func, width, face, base_line, relative_xy.y);
relative_xy.y -= shift.y_delta;
Layout_Item_List line = file_get_line_layout(tctx, models, file, layout_func, width, face, shift.line);
return(layout_nearest_pos_to_xy(line, relative_xy));
}
internal Rect_f32
file_relative_box_of_pos(Thread_Context *tctx, Models *models, Editing_File *file,
Layout_Function *layout_func, f32 width, Face *face,
i64 base_line, i64 pos){
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);
}
function Vec2_f32
file_relative_xy_of_pos(Thread_Context *tctx, Models *models, Editing_File *file,
Layout_Function *layout_func, f32 width, Face *face,
i64 base_line, i64 pos){
Rect_f32 rect = file_relative_box_of_pos(tctx, models, file, layout_func, width, face, base_line, pos);
return(rect_center(rect));
}
function Rect_f32
file_padded_box_of_pos(Thread_Context *tctx, Models *models, Editing_File *file,
Layout_Function *layout_func, f32 width, Face *face,
i64 base_line, i64 pos){
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);
}
internal Buffer_Point
file_normalize_buffer_point(Thread_Context *tctx, Models *models, Editing_File *file,
Layout_Function *layout_func, f32 width, Face *face,
Buffer_Point point){
Line_Shift_Vertical shift = file_line_shift_y(tctx, models, file, layout_func, width, face, point.line_number, point.pixel_shift.y);
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
file_buffer_point_difference(Thread_Context *tctx, Models *models, Editing_File *file,
Layout_Function *layout_func, f32 width, Face *face,
Buffer_Point a, Buffer_Point b){
f32 y_difference = file_line_y_difference(tctx, models, file, layout_func, width, face, a.line_number, b.line_number);
Vec2_f32 result = a.pixel_shift - b.pixel_shift;
result.y += y_difference;
return(result);
}
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;
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;
}
Layout_Item_List line = file_get_line_layout(tctx, models, file, layout_func, width, face, line_number);
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){
Layout_Item_List line = file_get_line_layout(tctx, models, file, layout_func, width, face, line_number);
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
file_line_character_difference(Thread_Context *tctx, Models *models, Editing_File *file, Layout_Function *layout_func, f32 width, Face *face, i64 line_a, i64 line_b){
i64 result = 0;
if (line_a != line_b){
Range_i64 line_range = Ii64(line_a, line_b);
for (i64 i = line_range.min; i < line_range.max; i += 1){
Layout_Item_List line = file_get_line_layout(tctx, models, file, layout_func, width, face, i);
result += line.character_count;
}
if (line_a < line_b){
result *= -1;
}
}
return(result);
}
internal i64
file_pos_from_relative_character(Thread_Context *tctx, Models *models, Editing_File *file,
Layout_Function *layout_func, f32 width, Face *face,
i64 base_line, i64 relative_character){
Line_Shift_Character shift = file_line_shift_characters(tctx, models, file, layout_func, width, face, base_line, relative_character);
relative_character -= shift.character_delta;
Layout_Item_List line = file_get_line_layout(tctx, models, file, layout_func, width, face, shift.line);
return(layout_get_pos_at_character(line, relative_character));
}
internal i64
file_relative_character_from_pos(Thread_Context *tctx, Models *models, Editing_File *file, Layout_Function *layout_func, f32 width, Face *face,
i64 base_line, i64 pos){
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);
i64 result = layout_character_from_pos(line, pos);
result += file_line_character_difference(tctx, models, file, layout_func, width, face, line_number, base_line);
return(result);
}
// BOTTOM