codified the view/file getting rule

master
Allen Webster 2016-06-14 16:40:57 -04:00
parent df6eaf7e29
commit f8c5738dc1
3 changed files with 293 additions and 391 deletions

View File

@ -983,9 +983,9 @@ update_command_data(App_Vars *vars, Command_Data *cmd){
globalvar Command_Function command_table[cmdid_count];
extern "C"{
//extern "C"{
#include "4ed_api_implementation.cpp"
}
//}
struct Command_In{
Command_Data *cmd;

View File

@ -25,6 +25,12 @@ fill_buffer_summary(Buffer_Summary *buffer, Editing_File *file, Working_Set *wor
}
}
internal void
fill_buffer_summary(Buffer_Summary *buffer, Editing_File *file, Command_Data *cmd){
Working_Set *working_set = &cmd->models->working_set;
fill_buffer_summary(buffer, file, working_set);
}
internal void
fill_view_summary(View_Summary *view, View *vptr, Live_Views *live_set, Working_Set *working_set){
i32 lock_level;
@ -74,6 +80,11 @@ fill_view_summary(View_Summary *view, View *vptr, Live_Views *live_set, Working_
}
}
inline void
fill_view_summary(View_Summary *view, View *vptr, Command_Data *cmd){
fill_view_summary(view, vptr, &cmd->vars->live_set, &cmd->models->working_set);
}
internal Editing_File*
get_file_from_identifier(System_Functions *system, Working_Set *working_set, Buffer_Identifier buffer){
i32 buffer_id = buffer.id;
@ -92,7 +103,7 @@ get_file_from_identifier(System_Functions *system, Working_Set *working_set, Buf
return(file);
}
String
internal String
make_string_terminated(Partition *part, char *str, int len){
char *space = (char*)push_array(part, char, len + 1);
String string = make_string(str, len, len+1);
@ -102,6 +113,36 @@ make_string_terminated(Partition *part, char *str, int len){
return(string);
}
internal Editing_File*
imp_get_file(Command_Data *cmd, Buffer_Summary *buffer){
Editing_File *file = 0;
Working_Set *working_set = &cmd->models->working_set;;
if (buffer->exists){
file = working_set_get_active_file(working_set, buffer->buffer_id);
if (file != 0 && !file_is_ready(file)){
file = 0;
}
}
return(file);
}
internal View*
imp_get_view(Command_Data *cmd, View_Summary *view){
View *vptr = 0;
Live_Views *live_set = cmd->live_set;
int view_id = view->view_id - 1;
if (view->exists){
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
}
}
return(vptr);
}
EXEC_COMMAND_SIG(external_exec_command){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Command_Function function = command_table[command_id];
@ -409,13 +450,11 @@ REFRESH_BUFFER_SIG(external_refresh_buffer){
BUFFER_SEEK_SIG(external_buffer_seek){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Editing_File *file;
Working_Set *working_set;
int result = 0;
int result = false;
if (buffer->exists){
working_set = &cmd->models->working_set;
file = working_set_get_active_file(working_set, buffer->buffer_id);
if (file && file_is_ready(file)){
file = imp_get_file(cmd, buffer);
if (file){
// TODO(allen): reduce duplication?
{
i32 size = buffer_size(&file->state.buffer);
@ -496,8 +535,7 @@ BUFFER_SEEK_SIG(external_buffer_seek){
result = new_pos;
}
fill_buffer_summary(buffer, file, working_set);
}
fill_buffer_summary(buffer, file, cmd);
}
return(result);
@ -505,22 +543,17 @@ BUFFER_SEEK_SIG(external_buffer_seek){
BUFFER_READ_RANGE_SIG(external_buffer_read_range){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Editing_File *file;
Working_Set *working_set;
int result = 0;
Editing_File *file = imp_get_file(cmd, buffer);
int result = false;
int size;
if (buffer->exists){
working_set = &cmd->models->working_set;
file = working_set_get_active_file(working_set, buffer->buffer_id);
if (file && file_is_ready(file)){
if (file){
size = buffer_size(&file->state.buffer);
if (0 <= start && start <= end && end <= size){
result = 1;
result = true;
buffer_stringify(&file->state.buffer, start, end, out);
}
fill_buffer_summary(buffer, file, working_set);
}
fill_buffer_summary(buffer, file, cmd);
}
return(result);
@ -528,20 +561,13 @@ BUFFER_READ_RANGE_SIG(external_buffer_read_range){
BUFFER_REPLACE_RANGE_SIG(external_buffer_replace_range){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Editing_File *file;
Working_Set *working_set;
Editing_File *file = imp_get_file(cmd, buffer);
Models *models;
int result = false;
int size = 0;
int next_cursor = 0, pos = 0;
int result = 0;
int size;
int next_cursor, pos;
if (buffer->exists){
models = cmd->models;
working_set = &models->working_set;
file = working_set_get_active_file(working_set, buffer->buffer_id);
if (file && file_is_ready(file)){
if (file){
size = buffer_size(&file->state.buffer);
if (0 <= start && start <= end && end <= size){
result = 1;
@ -551,57 +577,27 @@ BUFFER_REPLACE_RANGE_SIG(external_buffer_replace_range){
else if (pos < end) next_cursor = start;
else next_cursor = pos + end - start - len;
file_replace_range(cmd->system, models, file, start, end, str, len, next_cursor);
}
fill_buffer_summary(buffer, file, working_set);
file_replace_range(cmd->system, cmd->models,
file, start, end, str, len, next_cursor);
}
fill_buffer_summary(buffer, file, cmd);
}
return(result);
}
#if 0
BUFFER_SET_POS_SIG(external_buffer_set_pos){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Editing_File *file;
Working_Set *working_set;
int result = 0;
int size;
if (buffer->exists){
working_set = &cmd->models->working_set;
file = working_set_get_active_file(working_set, buffer->buffer_id);
if (file && file_is_ready(file)){
result = 1;
size = buffer_size(&file->state.buffer);
if (pos < 0) pos = 0;
if (pos > size) pos = size;
file->state.cursor_pos = pos;
fill_buffer_summary(buffer, file, working_set);
}
}
return(result);
}
#endif
BUFFER_SET_SETTING_SIG(external_buffer_set_setting){
Command_Data *cmd = (Command_Data*)app->cmd_context;
System_Functions *system = cmd->system;
Models *models = cmd->models;
Editing_File *file;
Working_Set *working_set;
Editing_File *file = imp_get_file(cmd, buffer);
int result = false;
i32 new_mapid = 0;
if (buffer->exists){
working_set = &models->working_set;
file = working_set_get_active_file(working_set, buffer->buffer_id);
if (file && file_is_ready(file)){
if (file){
result = true;
switch (setting){
case BufferSetting_Lex:
@ -650,9 +646,7 @@ BUFFER_SET_SETTING_SIG(external_buffer_set_setting){
}
}break;
}
}
fill_buffer_summary(buffer, file, working_set);
fill_buffer_summary(buffer, file, cmd);
}
return(result);
@ -663,20 +657,15 @@ BUFFER_SAVE_SIG(external_buffer_save){
System_Functions *system = cmd->system;
Models *models = cmd->models;
Editing_File *file;
Working_Set *working_set;
Editing_File *file = imp_get_file(cmd, buffer);
int result = false;
if (buffer->exists){
working_set = &models->working_set;
file = working_set_get_active_file(working_set, buffer->buffer_id);
if (file && !file->is_dummy && file_is_ready(file)){
if (file){
result = true;
String name = make_string(filename, filename_len);
view_save_file(system, models, file, 0, name, 0);
}
}
return(result);
}
@ -689,7 +678,7 @@ GET_VIEW_FIRST_SIG(external_get_view_first){
Panel *panel = layout->used_sentinel.next;
Assert(panel != &layout->used_sentinel);
fill_view_summary(&view, panel->view, &cmd->vars->live_set, &cmd->models->working_set);
fill_view_summary(&view, panel->view, cmd);
return(view);
}
@ -755,16 +744,10 @@ VIEW_AUTO_TAB_SIG(external_view_auto_tab){
int result = false;
Live_Views *live_set;
Editing_File *file;
View *vptr;
int view_id;
Editing_File *file = 0;
View *vptr = imp_get_view(cmd, view);
if (view->exists){
live_set = cmd->live_set;
view_id = view->view_id - 1;
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
if (vptr){
file = vptr->file_data.file;
if (file && file->state.token_stack.tokens &&
@ -777,7 +760,8 @@ VIEW_AUTO_TAB_SIG(external_view_auto_tab){
opts.tab_width = tab_width;
view_auto_tab_tokens(system, models, vptr, start, end, opts);
}
fill_view_summary(view, vptr, cmd);
}
}
@ -786,25 +770,18 @@ VIEW_AUTO_TAB_SIG(external_view_auto_tab){
VIEW_COMPUTE_CURSOR_SIG(external_view_compute_cursor){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Live_Views *live_set;
View *vptr;
Editing_File *file;
View *vptr = imp_get_view(cmd, view);
Editing_File *file = 0;
Full_Cursor result = {0};
int view_id;
if (view->exists){
live_set = cmd->live_set;
view_id = view->view_id - 1;
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
if (vptr){
file = vptr->file_data.file;
if (file && !file->is_loading){
if (seek.type == buffer_seek_line_char && seek.character <= 0){
seek.character = 1;
}
result = view_compute_cursor(vptr, seek);
fill_view_summary(view, vptr, live_set, &cmd->models->working_set);
}
fill_view_summary(view, vptr, cmd);
}
}
@ -813,20 +790,14 @@ VIEW_COMPUTE_CURSOR_SIG(external_view_compute_cursor){
VIEW_SET_CURSOR_SIG(external_view_set_cursor){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Live_Views *live_set;
View *vptr;
Editing_File *file;
int result = 0;
int view_id;
View *vptr = imp_get_view(cmd, view);
Editing_File *file = 0;
int result = false;
if (view->exists){
live_set = cmd->live_set;
view_id = view->view_id - 1;
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
if (vptr){
file = vptr->file_data.file;
if (file && !file->is_loading){
result = 1;
result = true;
if (seek.type == buffer_seek_line_char && seek.character <= 0){
seek.character = 1;
}
@ -834,29 +805,22 @@ VIEW_SET_CURSOR_SIG(external_view_set_cursor){
if (set_preferred_x){
vptr->recent->preferred_x = view_get_cursor_x(vptr);
}
fill_view_summary(view, vptr, live_set, &cmd->models->working_set);
fill_view_summary(view, vptr, cmd);
file->state.cursor_pos = vptr->recent->cursor.pos;
}
}
}
return(result);
}
VIEW_SET_MARK_SIG(external_view_set_mark){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Live_Views *live_set;
View *vptr;
Full_Cursor cursor;
int result = 0;
int view_id;
View *vptr = imp_get_view(cmd, view);
Full_Cursor cursor = {0};
int result = false;
if (view->exists){
live_set = cmd->live_set;
view_id = view->view_id - 1;
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
result = 1;
if (vptr){
result = true;
if (seek.type != buffer_seek_pos){
cursor = view_compute_cursor(vptr, seek);
vptr->recent->mark = cursor.pos;
@ -864,8 +828,7 @@ VIEW_SET_MARK_SIG(external_view_set_mark){
else{
vptr->recent->mark = seek.pos;
}
fill_view_summary(view, vptr, live_set, &cmd->models->working_set);
}
fill_view_summary(view, vptr, cmd);
}
return(result);
@ -873,25 +836,18 @@ VIEW_SET_MARK_SIG(external_view_set_mark){
VIEW_SET_HIGHLIGHT_SIG(external_view_set_highlight){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Live_Views *live_set;
View *vptr;
int result = 0;
int view_id;
View *vptr = imp_get_view(cmd, view);
int result = false;
if (view->exists){
live_set = cmd->live_set;
view_id = view->view_id - 1;
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
result = 1;
if (vptr){
result = true;
if (turn_on){
view_set_temp_highlight(vptr, start, end);
}
else{
vptr->file_data.show_temp_highlight = 0;
}
fill_view_summary(view, vptr, live_set, &cmd->models->working_set);
}
fill_view_summary(view, vptr, cmd);
}
return(result);
@ -899,33 +855,23 @@ VIEW_SET_HIGHLIGHT_SIG(external_view_set_highlight){
VIEW_SET_BUFFER_SIG(external_view_set_buffer){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Live_Views *live_set;
View *vptr;
Editing_File *file;
Working_Set *working_set;
Models *models;
int result = 0;
int view_id;
View *vptr = imp_get_view(cmd, view);
Models *models = cmd->models;
Editing_File *file = 0;
int result = false;
if (view->exists){
models = cmd->models;
live_set = cmd->live_set;
view_id = view->view_id - 1;
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
working_set = &models->working_set;
file = working_set_get_active_file(working_set, buffer_id);
if (vptr){
file = working_set_get_active_file(&models->working_set, buffer_id);
if (file){
result = 1;
result = true;
if (file != vptr->file_data.file){
view_set_file(vptr, file, models);
view_show_file(vptr);
}
}
fill_view_summary(view, vptr, live_set, working_set);
}
fill_view_summary(view, vptr, cmd);
}
return(result);
@ -933,72 +879,37 @@ VIEW_SET_BUFFER_SIG(external_view_set_buffer){
VIEW_POST_FADE_SIG(external_view_post_fade){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Live_Views *live_set;
View *vptr;
Models *models;
int view_id;
View *vptr = imp_get_view(cmd, view);
int result = false;
if (view->exists){
models = cmd->models;
live_set = cmd->live_set;
view_id = view->view_id - 1;
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
if (end > start){
int size = end - start;
if (vptr){
if (size > 0){
result = true;
view_post_paste_effect(vptr, ticks, start, size, color);
}
}
}
return(result);
}
// TODO(allen): standardize the safe get view/buffer code
VIEW_SET_PASTE_REWRITE__SIG(external_view_set_paste_rewrite_){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Live_Views *live_set;
View *vptr;
Models *models;
int view_id;
if (view->exists){
models = cmd->models;
live_set = cmd->live_set;
view_id = view->view_id - 1;
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
View *vptr = imp_get_view(cmd, view);
if (vptr){
vptr->next_mode.rewrite = true;
}
}
}
VIEW_GET_PASTE_REWRITE__SIG(external_view_get_paste_rewrite_){
Command_Data *cmd = (Command_Data*)app->cmd_context;
Live_Views *live_set;
View *vptr;
Models *models;
int view_id;
View *vptr = imp_get_view(cmd, view);
int result = false;
if (view->exists){
models = cmd->models;
live_set = cmd->live_set;
view_id = view->view_id - 1;
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
if (vptr){
result = vptr->mode.rewrite;
}
}
return(result);
}
@ -1014,7 +925,8 @@ VIEW_OPEN_FILE_SIG(external_view_open_file){
int result = false;
// TODO(allen): do in background option
// TODO(allen): do in background
// option happens in parallel.
Partition *part = &models->mem.part;
Temp_Memory temp = begin_temp_memory(part);
@ -1045,28 +957,19 @@ VIEW_OPEN_FILE_SIG(external_view_open_file){
VIEW_KILL_BUFFER_SIG(external_view_kill_buffer){
Command_Data *cmd = (Command_Data*)app->cmd_context;
System_Functions *system = cmd->system;
Live_Views *live_set;
View *vptr;
Editing_File *file;
Working_Set *working_set;
Models *models;
Models *models = cmd->models;
Working_Set *working_set = &models->working_set;
View *vptr = imp_get_view(cmd, view);
Editing_File *file = 0;
int result = false;
int view_id;
if (view->exists){
models = cmd->models;
live_set = cmd->live_set;
view_id = view->view_id - 1;
if (view_id >= 0 && view_id < live_set->max){
vptr = live_set->views + view_id;
working_set = &models->working_set;
if (vptr){
file = get_file_from_identifier(system, working_set, buffer);
if (file){
result = true;
try_kill_file(system, models, file, vptr, string_zero());
fill_view_summary(view, vptr, live_set, working_set);
}
fill_view_summary(view, vptr, cmd);
}
}

View File

@ -27,7 +27,6 @@ Buffer_Summary Get_Buffer_By_Name(Application_Links *app, char *filename, int le
int Refresh_Buffer(Application_Links *app, Buffer_Summary *buffer);
int Buffer_Read_Range(Application_Links *app, Buffer_Summary *buffer, int start, int end, char *out);
int Buffer_Replace_Range(Application_Links *app, Buffer_Summary *buffer, int start, int end, char *str, int len);
//int Buffer_Set_Pos(Application_Links *app, Buffer_Summary *buffer, int pos);
int Buffer_Seek(Application_Links *app, Buffer_Summary *buffer, int start_pos, int seek_forward, unsigned int flags);
int Buffer_Set_Setting(Application_Links *app, Buffer_Summary *buffer, int setting, int value);