2019-02-24 07:22:16 +00:00
|
|
|
/*
|
|
|
|
* Serial inserts helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
|
|
|
static Buffer_Insertion
|
2019-06-20 23:43:27 +00:00
|
|
|
begin_buffer_insertion_at(Application_Links *app, Buffer_ID buffer_id, i64 at){
|
2019-02-24 07:22:16 +00:00
|
|
|
Buffer_Insertion result = {};
|
|
|
|
result.app = app;
|
|
|
|
result.buffer = buffer_id;
|
|
|
|
result.at = at;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:06:49 +00:00
|
|
|
static Buffer_Insertion
|
2019-06-20 23:43:27 +00:00
|
|
|
begin_buffer_insertion_at_buffered(Application_Links *app, Buffer_ID buffer_id, i64 at, Cursor *cursor){
|
2019-04-02 20:06:49 +00:00
|
|
|
Buffer_Insertion result = begin_buffer_insertion_at(app, buffer_id, at);
|
|
|
|
result.buffering = true;
|
2019-06-01 23:58:28 +00:00
|
|
|
result.cursor = cursor;
|
|
|
|
result.temp = begin_temp(cursor);
|
2019-04-02 20:06:49 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2019-07-13 03:19:24 +00:00
|
|
|
internal Buffer_Insertion
|
|
|
|
begin_buffer_insertion_at_buffered(Application_Links *app, Buffer_ID buffer_id, i64 at, Arena *buffer_memory, umem buffer_memory_size){
|
|
|
|
Cursor *cursor = push_array(buffer_memory, Cursor, 1);
|
|
|
|
*cursor = make_cursor(push_array(buffer_memory, u8, buffer_memory_size), buffer_memory_size);
|
|
|
|
return(begin_buffer_insertion_at_buffered(app, buffer_id, at, cursor));
|
|
|
|
}
|
|
|
|
|
2019-02-24 07:22:16 +00:00
|
|
|
static Buffer_Insertion
|
|
|
|
begin_buffer_insertion(Application_Links *app){
|
2019-06-19 02:31:59 +00:00
|
|
|
View_ID view = get_active_view(app, AccessAll);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, AccessAll);
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 cursor_pos = view_get_cursor_pos(app, view);
|
2019-04-02 20:06:49 +00:00
|
|
|
Buffer_Insertion result = begin_buffer_insertion_at(app, buffer, cursor_pos);
|
2019-02-24 07:22:16 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-06-01 23:58:28 +00:00
|
|
|
insert_string__no_buffering(Buffer_Insertion *insertion, String_Const_u8 string){
|
2019-06-20 23:43:27 +00:00
|
|
|
buffer_replace_range(insertion->app, insertion->buffer, Ii64(insertion->at), string);
|
2019-02-24 07:22:16 +00:00
|
|
|
insertion->at += string.size;
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:06:49 +00:00
|
|
|
static void
|
|
|
|
insert__flush(Buffer_Insertion *insertion){
|
2019-06-01 23:58:28 +00:00
|
|
|
Cursor *cursor = insertion->cursor;
|
|
|
|
umem pos = insertion->temp.temp_memory_cursor.pos;
|
|
|
|
String_Const_u8 string = SCu8(cursor->base + pos, cursor->pos - pos);
|
2019-04-02 20:06:49 +00:00
|
|
|
insert_string__no_buffering(insertion, string);
|
2019-06-01 23:58:28 +00:00
|
|
|
end_temp(insertion->temp);
|
2019-04-02 20:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
2019-06-01 23:58:28 +00:00
|
|
|
insert__reserve(Buffer_Insertion *insertion, umem size){
|
|
|
|
char *space = push_array(insertion->cursor, char, size);
|
2019-04-02 20:06:49 +00:00
|
|
|
if (space == 0){
|
|
|
|
insert__flush(insertion);
|
2019-06-01 23:58:28 +00:00
|
|
|
space = push_array(insertion->cursor, char, size);
|
2019-04-02 20:06:49 +00:00
|
|
|
}
|
|
|
|
return(space);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-06-01 23:58:28 +00:00
|
|
|
end_buffer_insertion(Buffer_Insertion *insertion){
|
|
|
|
if (insertion->buffering){
|
|
|
|
insert__flush(insertion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
insert_string(Buffer_Insertion *insertion, String_Const_u8 string){
|
2019-04-02 20:06:49 +00:00
|
|
|
if (!insertion->buffering){
|
|
|
|
insert_string__no_buffering(insertion, string);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
char *space = insert__reserve(insertion, string.size);
|
|
|
|
if (space != 0){
|
2019-09-27 23:56:05 +00:00
|
|
|
block_copy(space, string.str, string.size);
|
2019-04-02 20:06:49 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
insert_string__no_buffering(insertion, string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
static umem
|
2019-02-24 07:22:16 +00:00
|
|
|
insertf(Buffer_Insertion *insertion, char *format, ...){
|
2019-10-01 02:06:21 +00:00
|
|
|
Scratch_Block scratch(insertion->app);
|
2019-02-24 07:22:16 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2019-06-20 03:43:02 +00:00
|
|
|
String_Const_u8 string = push_u8_stringfv(scratch, format, args);
|
2019-02-24 07:22:16 +00:00
|
|
|
va_end(args);
|
2019-04-01 06:14:31 +00:00
|
|
|
insert_string(insertion, string);
|
|
|
|
return(string.size);
|
2019-02-24 07:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
insertc(Buffer_Insertion *insertion, char C){
|
2019-06-01 23:58:28 +00:00
|
|
|
insert_string(insertion, SCu8(&C, 1));
|
2019-02-24 07:22:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 23:08:42 +00:00
|
|
|
static b32
|
2019-02-26 23:17:53 +00:00
|
|
|
insert_line_from_buffer(Buffer_Insertion *insertion, Buffer_ID buffer_id, i32 line, i32 truncate_at){
|
2019-06-02 03:07:57 +00:00
|
|
|
b32 success = is_valid_line(insertion->app, buffer_id, line);
|
|
|
|
if (success){
|
|
|
|
Scratch_Block scratch(insertion->app);
|
|
|
|
insert_string(insertion, push_buffer_line(insertion->app, scratch, buffer_id, line));
|
2019-02-24 07:22:16 +00:00
|
|
|
}
|
|
|
|
return(success);
|
|
|
|
}
|
|
|
|
|
2019-02-26 23:08:42 +00:00
|
|
|
static b32
|
2019-02-26 23:17:53 +00:00
|
|
|
insert_line_from_buffer(Buffer_Insertion *insertion, Buffer_ID buffer_id, i32 line){
|
2019-02-24 07:22:16 +00:00
|
|
|
return(insert_line_from_buffer(insertion, buffer_id, line, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// BOTTOM
|
|
|
|
|