smart cursors

master
Allen Webster 2015-10-18 21:13:15 -04:00
parent 6918ae0be4
commit c100efd1af
6 changed files with 1174 additions and 1036 deletions

View File

@ -126,7 +126,6 @@ HOOK_SIG(my_file_settings){
push_parameter(cmd_context, par_lex_as_cpp_file, treat_as_code); push_parameter(cmd_context, par_lex_as_cpp_file, treat_as_code);
push_parameter(cmd_context, par_wrap_lines, !treat_as_code); push_parameter(cmd_context, par_wrap_lines, !treat_as_code);
push_parameter(cmd_context, par_key_mapid, (treat_as_code)?(my_code_map):(mapid_file)); push_parameter(cmd_context, par_key_mapid, (treat_as_code)?(my_code_map):(mapid_file));
push_parameter(cmd_context, par_end_line_mode, EOL_USE_CRLF);
exec_command(cmd_context, cmdid_set_settings); exec_command(cmd_context, cmdid_set_settings);
} }
@ -228,9 +227,8 @@ extern "C" GET_BINDING_DATA(get_bindings){
bind(context, 'j', MDFR_CTRL, cmdid_to_lowercase); bind(context, 'j', MDFR_CTRL, cmdid_to_lowercase);
bind(context, '?', MDFR_CTRL, cmdid_toggle_show_whitespace); bind(context, '?', MDFR_CTRL, cmdid_toggle_show_whitespace);
// NOTE(allen): These whitespace manipulators are not currently functional
bind(context, '`', MDFR_CTRL, cmdid_clean_line);
bind(context, '~', MDFR_CTRL, cmdid_clean_all_lines); bind(context, '~', MDFR_CTRL, cmdid_clean_all_lines);
// NOTE(allen): These whitespace manipulators are not currently functional
bind(context, '1', MDFR_CTRL, cmdid_eol_dosify); bind(context, '1', MDFR_CTRL, cmdid_eol_dosify);
bind(context, '!', MDFR_CTRL, cmdid_eol_nixify); bind(context, '!', MDFR_CTRL, cmdid_eol_nixify);

View File

@ -92,7 +92,6 @@ enum Command_ID{
cmdid_to_uppercase, cmdid_to_uppercase,
cmdid_to_lowercase, cmdid_to_lowercase,
cmdid_toggle_show_whitespace, cmdid_toggle_show_whitespace,
cmdid_clean_line,
cmdid_clean_all_lines, cmdid_clean_all_lines,
cmdid_eol_dosify, cmdid_eol_dosify,
cmdid_eol_nixify, cmdid_eol_nixify,
@ -124,7 +123,6 @@ enum Param_ID{
par_lex_as_cpp_file, par_lex_as_cpp_file,
par_wrap_lines, par_wrap_lines,
par_key_mapid, par_key_mapid,
par_end_line_mode,
// never below this // never below this
par_type_count par_type_count
}; };
@ -208,12 +206,6 @@ struct Extra_Font{
int size; int size;
}; };
enum EOL_Option{
EOL_USE_CRLF,
EOL_USE_CR_USE_LF,
EOL_SHOW_CR_USE_LF
};
struct Buffer_Summary{ struct Buffer_Summary{
// NOTE(allen): None of these members nor any of the data pointed to // NOTE(allen): None of these members nor any of the data pointed to
// by these members should be modified. // by these members should be modified.
@ -229,7 +221,6 @@ struct Buffer_Summary{
int file_cursor_pos; int file_cursor_pos;
EOL_Option eol_mode;
int is_lexed; int is_lexed;
int map_id; int map_id;

326
4ed.cpp
View File

@ -8,78 +8,6 @@
*/ */
// TOP // TOP
// TODO(allen):
//
// BUGS & PROBLEMS
//
// - line_wrap_ys remeasurement optimization
//
// GENERAL
//
// - eol regularization
//
// - untabification
//
// - mode switching
//
// - auto mode switching
//
// - meta command stuff
// command use frequency
//
// - configuration / GUI for generating configuration
//
// - travel packaging
//
// - multiple live name conflicts
//
// - nav links
//
// - on file reopen diff and try to find place for cursor
//
// TOOLS
//
// - calculator with: dec hex translation
//
// - solver
//
// TEXT MODE
//
// - replace
//
// - select word
//
// - match list
//
// - regular expression
//
// BASIC CODE MODE - general any language
//
// - select token
//
// - reprogrammable lexing / auto indent
//
// - bracket match / mismatch highlighting
//
// - smart line wrapping (using tokens and white space as separation points)
//
// - auto casing rules?
//
// SUPER CODE MODE - C/C++ editing
//
// - identifier rename
//
// - boolean inverse polarity
//
// - compose/explode class/hierarchy
// - virtual to vtable sim/switch
//
// - explode class template?
//
// - enumerate template combinations?
//
// - generate header
//
// App Structs // App Structs
@ -273,15 +201,8 @@ COMMAND_DECL(write_character){
u8 character = (u8)command->key.key.character; u8 character = (u8)command->key.key.character;
char str_space[2]; char str_space[2];
String string = make_string(str_space, 2); String string = make_string(str_space, 2);
if (character == '\n' && file->endline_mode == EOL_USE_CRLF){ str_space[0] = character;
str_space[0] = '\r'; string.size = 1;
str_space[1] = '\n';
string.size = 2;
}
else{
str_space[0] = character;
string.size = 1;
}
i32 pos; i32 pos;
pos = view->cursor.pos; pos = view->cursor.pos;
@ -338,6 +259,7 @@ COMMAND_DECL(seek_whitespace_left){
view_cursor_move(view, pos); view_cursor_move(view, pos);
} }
// TODO(allen): see if this becomes better by using buffer procudures directly
COMMAND_DECL(seek_whitespace_up){ COMMAND_DECL(seek_whitespace_up){
ProfileMomentFunction(); ProfileMomentFunction();
REQ_FILE_VIEW(view); REQ_FILE_VIEW(view);
@ -351,29 +273,17 @@ COMMAND_DECL(seek_whitespace_up){
bool32 no_hard_character = 0; bool32 no_hard_character = 0;
while (pos > 0){ while (pos > 0){
if (starts_new_line(data[pos], file->endline_mode)){ if (starts_new_line(data[pos])){
if (no_hard_character){ if (no_hard_character) break;
break; else no_hard_character = 1;
}
else{
no_hard_character = 1;
}
} }
else{ else if (!char_is_whitespace(data[pos])){
if (!char_is_whitespace(data[pos])){ no_hard_character = 0;
no_hard_character = 0;
}
} }
--pos; --pos;
} }
if (pos != 0){ if (pos != 0) ++pos;
++pos;
}
if (file->endline_mode == EOL_USE_CRLF){
pos = pos_adjust_to_self(pos, (u8*)data, file->buffer.size);
}
view_cursor_move(view, pos); view_cursor_move(view, pos);
} }
@ -393,29 +303,21 @@ COMMAND_DECL(seek_whitespace_down){
bool32 no_hard_character = 0; bool32 no_hard_character = 0;
i32 prev_endline = -1; i32 prev_endline = -1;
while (pos < size){ while (pos < size){
if (starts_new_line(data[pos], file->endline_mode)){ if (starts_new_line(data[pos])){
if (no_hard_character){ if (no_hard_character) break;
break;
}
else{ else{
no_hard_character = 1; no_hard_character = 1;
prev_endline = pos; prev_endline = pos;
} }
} }
else{ else if (!char_is_whitespace(data[pos])){
if (!char_is_whitespace(data[pos])){ no_hard_character = 0;
no_hard_character = 0;
}
} }
++pos; ++pos;
} }
if (prev_endline == -1 || prev_endline+1 >= size){ if (prev_endline == -1 || prev_endline+1 >= size) pos = size;
pos = size; else pos = prev_endline+1;
}
else{
pos = prev_endline+1;
}
view_cursor_move(view, pos); view_cursor_move(view, pos);
} }
@ -665,9 +567,7 @@ COMMAND_DECL(cut){
clipboard_copy(&mem->general, working_set, data, range); clipboard_copy(&mem->general, working_set, data, range);
view_replace_range(mem, view, layout, range.start, range.end, 0, 0, next_cursor_pos); view_replace_range(mem, view, layout, range.start, range.end, 0, 0, next_cursor_pos);
view->mark = pos_universal_fix(range.start, view->mark = range.start;
(u8*)file->buffer.data, file->buffer.size,
file->endline_mode);
view_measure_wraps(&mem->general, view); view_measure_wraps(&mem->general, view);
view_cursor_move(view, next_cursor_pos); view_cursor_move(view, next_cursor_pos);
} }
@ -691,9 +591,7 @@ COMMAND_DECL(paste){
view_replace_range(mem, view, layout, pos_left, pos_left, (u8*)src->str, src->size, next_cursor_pos); view_replace_range(mem, view, layout, pos_left, pos_left, (u8*)src->str, src->size, next_cursor_pos);
view_cursor_move(view, next_cursor_pos); view_cursor_move(view, next_cursor_pos);
view->mark = pos_universal_fix(pos_left, view->mark = pos_left;
(u8*)file->buffer.data, file->buffer.size,
file->endline_mode);
Editing_Layout *layout = command->layout; Editing_Layout *layout = command->layout;
Panel *panels = layout->panels; Panel *panels = layout->panels;
@ -728,9 +626,7 @@ COMMAND_DECL(paste_next){
(u8*)src->str, src->size, next_cursor_pos); (u8*)src->str, src->size, next_cursor_pos);
view_cursor_move(view, next_cursor_pos); view_cursor_move(view, next_cursor_pos);
view->mark = pos_universal_fix(range.start, view->mark = range.start;
(u8*)file->buffer.data, file->buffer.size,
file->endline_mode);
Editing_Layout *layout = command->layout; Editing_Layout *layout = command->layout;
Panel *panels = layout->panels; Panel *panels = layout->panels;
@ -763,9 +659,7 @@ COMMAND_DECL(delete_chunk){
view_replace_range(mem, view, layout, range.start, range.end, 0, 0, next_cursor_pos); view_replace_range(mem, view, layout, range.start, range.end, 0, 0, next_cursor_pos);
view_measure_wraps(&mem->general, view); view_measure_wraps(&mem->general, view);
view_cursor_move(view, next_cursor_pos); view_cursor_move(view, next_cursor_pos);
view->mark = pos_universal_fix(range.start, view->mark = range.start;
(u8*)file->buffer.data, file->buffer.size,
file->endline_mode);
} }
} }
@ -884,8 +778,8 @@ app_open_file(App_Vars *vars, General_Memory *general, Panel *panel,
if (!target_file){ if (!target_file){
Get_File_Result file = working_set_get_available_file(working_set); Get_File_Result file = working_set_get_available_file(working_set);
if (file.file){ if (file.file){
buffer_get_dummy(file.file); file_get_dummy(file.file);
created_file = buffer_create(general, file.file, (u8*)string->str, style->font); created_file = file_create(general, file.file, (u8*)string->str, style->font);
table_add(&working_set->table, file.file->source_path, file.index); table_add(&working_set->table, file.file->source_path, file.index);
if (created_file){ if (created_file){
target_file = file.file; target_file = file.file;
@ -919,7 +813,7 @@ app_open_file(App_Vars *vars, General_Memory *general, Panel *panel,
new_view->map = app_get_map(vars, target_file->base_map_id); new_view->map = app_get_map(vars, target_file->base_map_id);
if (created_file && target_file->tokens_exist) if (created_file && target_file->tokens_exist)
buffer_first_lex_parallel(general, target_file); file_first_lex_parallel(general, target_file);
} }
return result; return result;
@ -987,13 +881,13 @@ COMMAND_DECL(reopen){
USE_VARS(vars); USE_VARS(vars);
Editing_File temp_file; Editing_File temp_file;
if (buffer_create(&mem->general, &temp_file, (u8*)make_c_str(file->source_path), style->font)){ if (file_create(&mem->general, &temp_file, (u8*)make_c_str(file->source_path), style->font)){
buffer_close(&mem->general, file); file_close(&mem->general, file);
*file = temp_file; *file = temp_file;
file->source_path.str = file->source_path_; file->source_path.str = file->source_path_;
file->live_name.str = file->live_name_; file->live_name.str = file->live_name_;
if (file->tokens_exist) if (file->tokens_exist)
buffer_first_lex_parallel(&mem->general, file); file_first_lex_parallel(&mem->general, file);
Partition old_part = command->part; Partition old_part = command->part;
Temp_Memory temp = begin_temp_memory(&vars->mem.part); Temp_Memory temp = begin_temp_memory(&vars->mem.part);
@ -1022,10 +916,11 @@ COMMAND_DECL(save){
ProfileMomentFunction(); ProfileMomentFunction();
REQ_FILE_VIEW(view); REQ_FILE_VIEW(view);
REQ_FILE(file, view); REQ_FILE(file, view);
USE_MEM(mem);
String *file_path = &file->source_path; String *file_path = &file->source_path;
if (file_path->size > 0){ if (file_path->size > 0){
file_save(file, (u8*)file_path->str); file_save(&mem->part, file, (u8*)file_path->str);
} }
} }
@ -1139,29 +1034,10 @@ COMMAND_DECL(toggle_endline_mode){
REQ_FILE_VIEW(view); REQ_FILE_VIEW(view);
REQ_FILE(file, view); REQ_FILE(file, view);
USE_MEM(mem); USE_MEM(mem);
switch (file->endline_mode){ file_measure_starts(&mem->general, view->file);
case EOL_USE_CRLF:
{
file->endline_mode = EOL_USE_CR_USE_LF;
}break;
case EOL_USE_CR_USE_LF:
{
file->endline_mode = EOL_SHOW_CR_USE_LF;
}break;
case EOL_SHOW_CR_USE_LF:
{
view->cursor.pos = pos_adjust_to_self(view->cursor.pos, (u8*)view->file->buffer.data,
view->file->buffer.size);
file->endline_mode = EOL_USE_CRLF;
}break;
}
view->cursor = view->cursor =
view_compute_cursor_from_pos(view, view->cursor.pos); view_compute_cursor_from_pos(view, view->cursor.pos);
file_measure_starts(&mem->general, view->file);
} }
COMMAND_DECL(toggle_show_whitespace){ COMMAND_DECL(toggle_show_whitespace){
@ -1177,10 +1053,10 @@ COMMAND_DECL(toggle_tokens){
USE_MEM(mem); USE_MEM(mem);
if (file->tokens_exist){ if (file->tokens_exist){
buffer_kill_tokens(&mem->general, file); file_kill_tokens(&mem->general, file);
} }
else{ else{
buffer_first_lex_parallel(&mem->general, file); file_first_lex_parallel(&mem->general, file);
} }
} }
@ -1212,7 +1088,7 @@ COMMAND_DECL(to_uppercase){
} }
if (file->token_stack.tokens){ if (file->token_stack.tokens){
buffer_relex_parallel(mem, file, range.start, range.end, 0); file_relex_parallel(mem, file, range.start, range.end, 0);
} }
} }
} }
@ -1245,98 +1121,22 @@ COMMAND_DECL(to_lowercase){
} }
if (file->token_stack.tokens){ if (file->token_stack.tokens){
buffer_relex_parallel(mem, file, range.start, range.end, 0); file_relex_parallel(mem, file, range.start, range.end, 0);
} }
} }
} }
#if 0
internal void
view_clean_line(Mem_Options *mem, File_View *view, Editing_File *file, i32 line_start){
u8 *data = file->data;
i32 pos = line_start;
i32 current_x_bumps = 0;
i32 first_hard = -1;
while (pos < file->size && data[pos] != '\n'){
if (!char_is_whitespace(data[pos])){
first_hard = pos;
break;
}
else{
switch (data[pos]){
case ' ': current_x_bumps += 1; break;
case '\t': current_x_bumps += 4; break;
}
}
++pos;
}
Indent_Definition indent = indent_by_width(current_x_bumps, 4);
buffer_set_indent_whitespace(mem, file, indent,
(u8*)partition_current(&mem->part), line_start);
pos = line_start;
i32 last_hard_start = pos-1;
i32 last_hard = last_hard_start;
while (pos < file->size && data[pos] != '\n'){
if (!char_is_whitespace(data[pos])){
last_hard = pos;
}
++pos;
}
if (last_hard != last_hard_start){
pos = pos_adjust_to_left(pos, data);
if (last_hard + 1 < pos){
buffer_replace_range(mem, file, last_hard+1, pos, 0, 0, REP_WHITESPACE);
if (view->cursor.pos > last_hard){
view->cursor = view_compute_cursor_from_pos(view, last_hard + 1);
}
if (view->mark > last_hard && view->mark <= pos){
view->mark = pos_adjust_to_self(last_hard+1, file->data, file->size);
}
else if (view->mark > pos){
view->mark -= pos - (last_hard + 1);
}
}
}
else{
view_auto_tab(mem, view, pos, pos);
}
}
#endif
COMMAND_DECL(clean_line){
#if 0
ProfileMomentFunction();
REQ_FILE_VIEW(view);
REQ_FILE(file, view);
USE_MEM(mem);
i32 line_start = view_find_beginning_of_line(view, view->cursor.pos);
view_clean_line(mem, view, file, line_start);
view_measure_wraps(&mem->general, view);
#endif
}
COMMAND_DECL(clean_all_lines){ COMMAND_DECL(clean_all_lines){
#if 0
ProfileMomentFunction(); ProfileMomentFunction();
REQ_FILE_VIEW(view); REQ_FILE_VIEW(view);
REQ_FILE(file, view); REQ_FILE(file, view);
USE_LAYOUT(layout);
USE_MEM(mem); USE_MEM(mem);
for (i32 i = 0; i < file->line_count; ++i){ view_clean_whitespace(mem, view, layout);
i32 line_start = file->line_starts[i];
view_clean_line(mem, view, file, line_start);
}
view_measure_wraps(&mem->general, view); view_measure_wraps(&mem->general, view);
view->cursor = view_compute_cursor_from_pos(view, view->cursor.pos); view->cursor = view_compute_cursor_from_pos(view, view->cursor.pos);
#endif
} }
COMMAND_DECL(eol_dosify){ COMMAND_DECL(eol_dosify){
@ -1502,13 +1302,9 @@ COMMAND_DECL(move_left){
REQ_FILE_VIEW(view); REQ_FILE_VIEW(view);
REQ_FILE(file, view); REQ_FILE(file, view);
u8 *data = (u8*)file->buffer.data;
i32 pos = view->cursor.pos; i32 pos = view->cursor.pos;
if (pos > 0){ if (pos > 0){
--pos; --pos;
if (file->endline_mode == EOL_USE_CRLF){
pos = pos_adjust_to_self(pos, data, file->buffer.size);
}
} }
view_cursor_move(view, pos); view_cursor_move(view, pos);
@ -1520,16 +1316,9 @@ COMMAND_DECL(move_right){
REQ_FILE(file, view); REQ_FILE(file, view);
i32 size = file->buffer.size; i32 size = file->buffer.size;
u8* data = (u8*)file->buffer.data;
i32 pos = view->cursor.pos; i32 pos = view->cursor.pos;
if (pos < size){ if (pos < size){
if (file->endline_mode == EOL_USE_CRLF){ ++pos;
pos = pos_adjust_to_right(pos, data, size);
++pos;
}
else{
++pos;
}
} }
view_cursor_move(view, pos); view_cursor_move(view, pos);
@ -1543,17 +1332,10 @@ COMMAND_DECL(delete){
USE_MEM(mem); USE_MEM(mem);
i32 cursor_pos = view->cursor.pos; i32 cursor_pos = view->cursor.pos;
u8 *data = (u8*)file->buffer.data;
if (file->buffer.size > 0 && cursor_pos < file->buffer.size){ if (file->buffer.size > 0 && cursor_pos < file->buffer.size){
i32 start, end; i32 start, end;
start = cursor_pos; start = cursor_pos;
if (file->endline_mode == EOL_USE_CRLF && end = cursor_pos+1;
data[cursor_pos] == '\r' && data[cursor_pos+1] == '\n'){
end = cursor_pos+2;
}
else{
end = cursor_pos+1;
}
i32 shift = (end - start); i32 shift = (end - start);
Assert(shift > 0); Assert(shift > 0);
@ -1573,20 +1355,11 @@ COMMAND_DECL(backspace){
USE_MEM(mem); USE_MEM(mem);
i32 cursor_pos = view->cursor.pos; i32 cursor_pos = view->cursor.pos;
u8 *data = (u8*)file->buffer.data;
if (cursor_pos > 0 && cursor_pos <= (i32)file->buffer.size){ if (cursor_pos > 0 && cursor_pos <= (i32)file->buffer.size){
i32 start, end; i32 start, end;
end = cursor_pos; end = cursor_pos;
if (file->endline_mode == EOL_USE_CRLF && start = cursor_pos-1;
cursor_pos > 1 &&
data[cursor_pos-1] == '\n' &&
data[cursor_pos-2] == '\r'){
start = cursor_pos-2;
}
else{
start = cursor_pos-1;
}
i32 shift = (end - start); i32 shift = (end - start);
Assert(shift > 0); Assert(shift > 0);
@ -1796,10 +1569,10 @@ COMMAND_DECL(set_settings){
{ {
int v = dynamic_to_bool(&param->param.value); int v = dynamic_to_bool(&param->param.value);
if (file->tokens_exist){ if (file->tokens_exist){
if (!v) buffer_kill_tokens(&mem->general, file); if (!v) file_kill_tokens(&mem->general, file);
} }
else{ else{
if (v) buffer_first_lex_parallel(&mem->general, file); if (v) file_first_lex_parallel(&mem->general, file);
} }
}break; }break;
@ -1839,9 +1612,6 @@ COMMAND_DECL(set_settings){
else map = 0; else map = 0;
} }
}break; }break;
case par_end_line_mode:
break;
} }
} }
} }
@ -1920,7 +1690,6 @@ extern "C"{
buffer.file_name = file->source_path.str; buffer.file_name = file->source_path.str;
buffer.buffer_name = file->live_name.str; buffer.buffer_name = file->live_name.str;
buffer.file_cursor_pos = file->cursor_pos; buffer.file_cursor_pos = file->cursor_pos;
buffer.eol_mode = file->endline_mode;
buffer.is_lexed = file->tokens_exist; buffer.is_lexed = file->tokens_exist;
buffer.map_id = file->base_map_id; buffer.map_id = file->base_map_id;
} }
@ -2007,7 +1776,6 @@ setup_file_commands(Command_Map *commands, Partition *part, Key_Codes *codes, Co
map_add(commands, '|', MDFR_CTRL, command_toggle_tokens); map_add(commands, '|', MDFR_CTRL, command_toggle_tokens);
map_add(commands, 'u', MDFR_CTRL, command_to_uppercase); map_add(commands, 'u', MDFR_CTRL, command_to_uppercase);
map_add(commands, 'j', MDFR_CTRL, command_to_lowercase); map_add(commands, 'j', MDFR_CTRL, command_to_lowercase);
map_add(commands, '`', MDFR_CTRL, command_clean_line);
map_add(commands, '~', MDFR_CTRL, command_clean_all_lines); map_add(commands, '~', MDFR_CTRL, command_clean_all_lines);
map_add(commands, '1', MDFR_CTRL, command_eol_dosify); map_add(commands, '1', MDFR_CTRL, command_eol_dosify);
map_add(commands, '!', MDFR_CTRL, command_eol_nixify); map_add(commands, '!', MDFR_CTRL, command_eol_nixify);
@ -2109,7 +1877,6 @@ setup_command_table(){
SET(to_uppercase); SET(to_uppercase);
SET(to_lowercase); SET(to_lowercase);
SET(toggle_show_whitespace); SET(toggle_show_whitespace);
SET(clean_line);
SET(clean_all_lines); SET(clean_all_lines);
SET(eol_dosify); SET(eol_dosify);
SET(eol_nixify); SET(eol_nixify);
@ -2494,7 +2261,6 @@ HOOK_SIG(default_open_file_hook){
app.push_parameter(cmd_context, dynamic_int(par_lex_as_cpp_file), dynamic_int(treat_as_code)); app.push_parameter(cmd_context, dynamic_int(par_lex_as_cpp_file), dynamic_int(treat_as_code));
app.push_parameter(cmd_context, dynamic_int(par_wrap_lines), dynamic_int(!treat_as_code)); app.push_parameter(cmd_context, dynamic_int(par_wrap_lines), dynamic_int(!treat_as_code));
app.push_parameter(cmd_context, dynamic_int(par_key_mapid), dynamic_int(mapid_file)); app.push_parameter(cmd_context, dynamic_int(par_key_mapid), dynamic_int(mapid_file));
app.push_parameter(cmd_context, dynamic_int(par_end_line_mode), dynamic_int(EOL_USE_CRLF));
app.exec_command_keep_stack(cmd_context, cmdid_set_settings); app.exec_command_keep_stack(cmd_context, cmdid_set_settings);
app.clear_parameters(cmd_context); app.clear_parameters(cmd_context);
@ -2756,7 +2522,7 @@ app_init(Thread_Context *thread, Application_Memory *memory,
vars->working_set.files = vars->working_set.files =
push_array(partition, Editing_File, vars->working_set.file_max_count); push_array(partition, Editing_File, vars->working_set.file_max_count);
buffer_get_dummy(&vars->working_set.files[0]); file_get_dummy(&vars->working_set.files[0]);
vars->working_set.table.max = vars->working_set.file_max_count * 3 / 2; vars->working_set.table.max = vars->working_set.file_max_count * 3 / 2;
vars->working_set.table.count = 0; vars->working_set.table.count = 0;
@ -3250,7 +3016,7 @@ app_step(Thread_Context *thread, Key_Codes *codes,
if (fview){ if (fview){
Editing_File *file = fview->file; Editing_File *file = fview->file;
if (file && !file->is_dummy){ if (file && !file->is_dummy){
file_save_and_set_names(file, (u8*)string->str); file_save_and_set_names(&vars->mem.part, file, (u8*)string->str);
} }
} }
}break; }break;
@ -3259,14 +3025,14 @@ app_step(Thread_Context *thread, Key_Codes *codes,
{ {
Editing_File *file = working_set_lookup_file(working_set, *string); Editing_File *file = working_set_lookup_file(working_set, *string);
if (file && !file->is_dummy){ if (file && !file->is_dummy){
file_save(file, (u8*)file->source_path.str); file_save(&vars->mem.part, file, (u8*)file->source_path.str);
} }
}break; }break;
case DACT_NEW: case DACT_NEW:
{ {
Get_File_Result file = working_set_get_available_file(working_set); Get_File_Result file = working_set_get_available_file(working_set);
buffer_create_empty(general, file.file, (u8*)string->str, style->font); file_create_empty(general, file.file, (u8*)string->str, style->font);
table_add(&working_set->table, file.file->source_path, file.index); table_add(&working_set->table, file.file->source_path, file.index);
View *new_view = live_set_alloc_view(live_set, mem); View *new_view = live_set_alloc_view(live_set, mem);
@ -3277,7 +3043,7 @@ app_step(Thread_Context *thread, Key_Codes *codes,
view_set_file(file_view, file.file, style, view_set_file(file_view, file.file, style,
vars->hooks[hook_open_file], &command_data, app_links); vars->hooks[hook_open_file], &command_data, app_links);
new_view->map = app_get_map(vars, file.file->base_map_id); new_view->map = app_get_map(vars, file.file->base_map_id);
if (file.file->tokens_exist) buffer_first_lex_parallel(general, file.file); if (file.file->tokens_exist) file_first_lex_parallel(general, file.file);
}break; }break;
case DACT_SWITCH: case DACT_SWITCH:
@ -3391,7 +3157,7 @@ app_step(Thread_Context *thread, Key_Codes *codes,
Editing_File *file = vars->working_set.files; Editing_File *file = vars->working_set.files;
for (i32 i = vars->working_set.file_index_count; i > 0; --i, ++file){ for (i32 i = vars->working_set.file_index_count; i > 0; --i, ++file){
if (file->buffer.data && !file->is_dummy){ if (file->buffer.data && !file->is_dummy){
buffer_measure_widths(&vars->mem.general, file, vars->style.font); file_measure_widths(&vars->mem.general, file, vars->style.font);
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -84,6 +84,8 @@ _OutDbgStr(u8*);
#define AllowLocal(name) (void)name #define AllowLocal(name) (void)name
#define ArrayCount(array) (sizeof(array)/sizeof(array[0])) #define ArrayCount(array) (sizeof(array)/sizeof(array[0]))
#define OffsetOfStruct(S,c) ((i64)(& ((S*)0)->c ))
#define OffsetOfPtr(s,c) ((i64)((char*)(&(s)->c) - (char*)(s)))
#define Swap(a,b) {auto t = a; a = b; b = t;} #define Swap(a,b) {auto t = a; a = b; b = t;}

View File

@ -2,6 +2,8 @@
* Mr. 4th Dimention - Allen Webster * Mr. 4th Dimention - Allen Webster
* Four Tech * Four Tech
* *
* public domain -- no warranty is offered or implied; use this code at your own risk
*
* 16.10.2015 * 16.10.2015
* *
* Buffer data object * Buffer data object
@ -9,61 +11,92 @@
* *
*/ */
// TOP
#ifndef defines_4tech
#define inline_4tech inline #define inline_4tech inline
#define internal_4tech static #define internal_4tech static
#define memset_4tech memset
#define memcpy_4tech memcpy
#define memmove_4tech memmove
#define defines_4tech 1
#define debug_4tech(x) x
#endif
#ifndef Assert
#define Assert
#endif
typedef struct{ typedef struct{
char *data; char *data;
int size, max; int size, max;
float *line_widths;
int *line_starts;
int line_count;
int line_max;
int widths_max;
} Buffer; } Buffer;
typedef struct{ typedef struct{
Buffer *buffer; Buffer *buffer;
char *data; char *data;
int size; int size;
} Buffer_Save_Loop; } Buffer_Stringify_Loop;
inline_4tech Buffer_Save_Loop inline_4tech Buffer_Stringify_Loop
buffer_save_loop(Buffer *buffer){ buffer_stringify_loop(Buffer *buffer, int start, int end){
Buffer_Save_Loop result; Buffer_Stringify_Loop result;
result.buffer = buffer; if (0 <= start && start <= end && end < buffer->size){
result.data = buffer->data; result.buffer = buffer;
result.size = buffer->size; result.data = buffer->data + start;
result.size = end - start;
}
else result.buffer = 0;
return(result); return(result);
} }
inline_4tech int inline_4tech int
buffer_save_good(Buffer_Save_Loop *loop){ buffer_stringify_good(Buffer_Stringify_Loop *loop){
int result; int result;
result = (loop->buffer != 0); result = (loop->buffer != 0);
return(result); return(result);
} }
inline_4tech void inline_4tech void
buffer_save_next(Buffer_Save_Loop *loop){ buffer_stringify_next(Buffer_Stringify_Loop *loop){
loop->buffer = 0; loop->buffer = 0;
} }
inline_4tech void
buffer_stringify(Buffer *buffer, int start, int end, char *out){
for (Buffer_Stringify_Loop loop = buffer_stringify_loop(buffer, start, end);
buffer_stringify_good(&loop);
buffer_stringify_next(&loop)){
memcpy_4tech(out, loop.data, loop.size);
out += loop.size;
}
}
internal_4tech int internal_4tech int
buffer_count_newlines(Buffer *buffer, int start, int end, int CR, int LF){ buffer_count_newlines(Buffer *buffer, int start, int end){
int new_line, count; int new_line, count;
char *data; char *data;
int i; int i;
Assert(0 <= start);
Assert(start <= end);
Assert(end < buffer->size);
data = buffer->data; data = buffer->data;
new_line = 0;
count = 0; count = 0;
for (i = start; i < end; ++i){ for (i = start; i < end; ++i){
switch(data[i]){ new_line = (data[i] == '\n');
case '\n': new_line = LF; break;
case '\r': new_line = CR; break;
default: new_line = 0; break;
}
count += new_line; count += new_line;
} }
return (count); return(count);
} }
typedef struct{ typedef struct{
@ -73,27 +106,28 @@ typedef struct{
} Buffer_Measure_Starts; } Buffer_Measure_Starts;
internal_4tech int internal_4tech int
buffer_measure_starts_(Buffer_Measure_Starts *state, Buffer *buffer, int *starts, int max, int CR, int LF){ buffer_measure_starts(Buffer_Measure_Starts *state, Buffer *buffer){
int *starts;
int max;
char *data; char *data;
int size; int size;
int start, count, i, new_line; int start, count, i;
int result; int result;
starts = buffer->line_starts;
max = buffer->line_max;
data = buffer->data; data = buffer->data;
size = buffer->size; size = buffer->size;
result = 0; result = 0;
start = state->start;
count = state->count;
for (i = state->i; i < size; ++i){ i = state->i;
switch (data[i]){ count = state->count;
case '\n': new_line = LF; break; start = state->start;
case '\r': new_line = CR; break;
default: new_line = 0; break; for (; i < size; ++i){
} if (data[i] == '\n'){
if (new_line){
if (count == max){ if (count == max){
result = 1; result = 1;
break; break;
@ -113,6 +147,632 @@ buffer_measure_starts_(Buffer_Measure_Starts *state, Buffer *buffer, int *starts
state->count = count; state->count = count;
state->start = start; state->start = start;
return (result); return(result);
} }
inline_4tech float
measure_character(void *advance_data, int offset, int stride, int *new_line, char character){
char *advances;
float width;
advances = (char*)advance_data + offset;
switch (character){
case 0: width = 0; *new_line = 1; break;
case '\n': width = *(float*)(advances + stride * '\n'); *new_line = 1; break;
case '\r': width = *(float*)(advances + stride * '\\') + *(float*)(advances + stride * '\r'); break;
default: width = *(float*)(advances + stride * character);
}
return(width);
}
internal_4tech void
buffer_measure_widths(Buffer *buffer, void *advance_data, int offset, int stride){
float *widths;
debug_4tech(int *starts);
int line_count;
char *data;
int size;
int i, j, new_line;
float width;
char ch, next;
Assert(buffer->widths_max >= buffer->line_count);
widths = buffer->line_widths;
debug_4tech(starts = buffer->line_starts);
line_count = buffer->line_count;
data = buffer->data;
size = buffer->size;
Assert(size < buffer->max);
data[size] = 0;
for (i = 0, j = 0; i < line_count; ++i){
Assert(j == starts[i]);
new_line = 0;
width = 0;
ch = data[j];
next = data[++j];
while (new_line == 0){
width += measure_character(advance_data, offset, stride, &new_line, ch);
ch = next;
next = data[++j];
}
--j;
widths[i] = width;
}
}
internal_4tech void
buffer_remeasure_starts(Buffer *buffer, int line_start, int line_end, int line_shift, int text_shift){
int *lines;
int line_count;
char *data;
int size;
int line_i, char_i, start;
char character;
lines = buffer->line_starts;
line_count = buffer->line_count;
if (line_shift != 0){
memmove_4tech(lines + line_end + line_shift + 1, lines + line_end + 1,
sizeof(int)*(line_count - line_end - 1));
line_count += line_shift;
}
if (text_shift != 0){
line_i = line_end + 1;
lines = lines + line_i;
for (; line_i < line_count; ++line_i, ++lines){
*lines += text_shift;
}
lines = buffer->line_starts;
}
size = buffer->size;
data = buffer->data;
char_i = lines[line_start];
line_i = line_start;
Assert(size < buffer->max);
data[size] = '\n';
start = char_i;
for (; char_i <= size; ++char_i){
character = data[char_i];
if (character == '\n'){
if (line_i > line_end && start == lines[line_i]) break;
lines[line_i++] = start;
start = char_i + 1;
}
}
buffer->line_count = line_count;
}
internal_4tech int
buffer_get_line_index(Buffer *buffer, int pos, int l_bound, int u_bound){
int *lines;
int start, end;
int i;
Assert(0 <= l_bound);
Assert(l_bound <= u_bound);
Assert(u_bound <= buffer->line_count);
start = l_bound;
end = u_bound;
lines = buffer->line_starts;
for (;;){
i = (start + end) >> 1;
if (lines[i] < pos) start = i;
else if (lines[i] > pos) end = i;
else{
start = i;
break;
}
Assert(start < end);
if (start == end - 1) break;
}
return(start);
}
internal_4tech int
buffer_replace_range(Buffer *buffer, int start, int end, char *str, int len, int *shift_amount){
char *data;
int result;
*shift_amount = (len - (end - start));
if (*shift_amount + buffer->size + 1 <= buffer->max){
data = (char*)buffer->data;
memmove_4tech(data + end + *shift_amount, data + end, buffer->size - end);
buffer->size += *shift_amount;
data[buffer->size] = 0;
if (len != 0) memcpy_4tech(data + start, str, len);
result = 0;
}
else{
result = *shift_amount + buffer->size + 1;
}
return(result);
}
typedef struct{
int pos, index;
} Cursor_With_Index;
inline_4tech void
write_cursor_with_index(Cursor_With_Index *positions, int *count, int pos){
positions[*count].index = *count;
positions[*count].pos = pos;
++*count;
}
internal_4tech int
buffer_quick_partition_cursors(Cursor_With_Index *positions, int start, int pivot){
int i;
int pivot_pos;
pivot_pos = positions[pivot].pos;
for (i = start; i < pivot; ++i){
if (positions[i].pos < pivot_pos){
Swap(positions[start], positions[i]);
++start;
}
}
Swap(positions[start], positions[pivot]);
return start;
}
internal_4tech void
buffer_quick_sort_cursors(Cursor_With_Index *positions, int start, int pivot){
int mid;
mid = buffer_quick_partition_cursors(positions, start, pivot);
if (start < mid - 1) buffer_quick_sort_cursors(positions, start, mid - 1);
if (mid + 1 < pivot) buffer_quick_sort_cursors(positions, mid + 1, pivot);
}
inline_4tech void
buffer_sort_cursors(Cursor_With_Index *positions, int count){
Assert(count > 0);
buffer_quick_sort_cursors(positions, 0, count-1);
}
internal_4tech int
buffer_quick_unpartition_cursors(Cursor_With_Index *positions, int start, int pivot){
int i;
int pivot_index;
pivot_index = positions[pivot].index;
for (i = start; i < pivot; ++i){
if (positions[i].index < pivot_index){
Swap(positions[start], positions[i]);
++start;
}
}
Swap(positions[start], positions[pivot]);
return start;
}
internal_4tech void
buffer_quick_unsort_cursors(Cursor_With_Index *positions, int start, int pivot){
int mid;
mid = buffer_quick_unpartition_cursors(positions, start, pivot);
if (start < mid - 1) buffer_quick_unsort_cursors(positions, start, mid - 1);
if (mid + 1 < pivot) buffer_quick_unsort_cursors(positions, mid + 1, pivot);
}
inline_4tech void
buffer_unsort_cursors(Cursor_With_Index *positions, int count){
Assert(count > 0);
buffer_quick_unsort_cursors(positions, 0, count-1);
}
internal_4tech void
buffer_update_cursors(Cursor_With_Index *sorted_positions, int count, int start, int end, int len){
Cursor_With_Index *position, *end_position;
int shift_amount;
shift_amount = (len - (end - start));
position = sorted_positions;
end_position = sorted_positions + count;
for (; position < end_position && position->pos < start; ++position);
for (; position < end_position && position->pos < end; ++position) position->pos = start;
for (; position < end_position; ++position) position->pos += shift_amount;
}
typedef enum{
buffer_seek_pos,
buffer_seek_wrapped_xy,
buffer_seek_unwrapped_xy,
buffer_seek_line_char
} Buffer_Seek_Type;
typedef struct{
Buffer_Seek_Type type;
union{
struct { int pos; };
struct { int round_down; float x, y; };
struct { int line, character; };
};
} Buffer_Seek;
inline_4tech Buffer_Seek
seek_pos(int pos){
Buffer_Seek result;
result.type = buffer_seek_pos;
result.pos = pos;
return(result);
}
inline_4tech Buffer_Seek
seek_wrapped_xy(float x, float y, int round_down){
Buffer_Seek result;
result.type = buffer_seek_wrapped_xy;
result.x = x;
result.y = y;
result.round_down = round_down;
return(result);
}
inline_4tech Buffer_Seek
seek_unwrapped_xy(float x, float y, int round_down){
Buffer_Seek result;
result.type = buffer_seek_unwrapped_xy;
result.x = x;
result.y = y;
result.round_down = round_down;
return(result);
}
inline_4tech Buffer_Seek
seek_line_char(int line, int character){
Buffer_Seek result;
result.type = buffer_seek_line_char;
result.line = line;
result.character = character;
return(result);
}
typedef struct{
int pos;
int line, character;
float unwrapped_x, unwrapped_y;
float wrapped_x, wrapped_y;
} Full_Cursor;
internal_4tech Full_Cursor
buffer_cursor_seek(Buffer *buffer, Buffer_Seek seek,
float max_width, float font_height, void *width_data, int offset, int stride, Full_Cursor cursor){
Full_Cursor prev_cursor;
char *data, *data_;
int size;
int do_newline, do_slashr;
char ch, next;
float ch_width;
int get_out;
int xy_seek;
float x, y, px;
data = buffer->data;
size = buffer->size;
Assert(size < buffer->max);
data[size] = 0;
data_ = (char*)width_data + offset;
xy_seek = (seek.type == buffer_seek_wrapped_xy || seek.type == buffer_seek_unwrapped_xy);
for (;;){
prev_cursor = cursor;
ch_width = 0;
ch = data[cursor.pos];
next = data[cursor.pos+1];
switch (ch){
case '\r': do_newline = 0; do_slashr = 1; break;
case '\n': do_newline = 1; do_slashr = 0; break;
default:
do_newline = 0; do_slashr = 0;
++cursor.character;
ch_width = *(float*)(data_ + stride * ch);
break;
}
if (do_slashr){
++cursor.character;
ch_width = *(float*)(data_ + stride * '\\') + *(float*)(data_ + stride * 'r');
}
if (cursor.wrapped_x + ch_width >= max_width){
cursor.wrapped_y += font_height;
cursor.wrapped_x = 0;
prev_cursor = cursor;
}
cursor.unwrapped_x += ch_width;
cursor.wrapped_x += ch_width;
if (do_newline){
++cursor.line;
cursor.unwrapped_y += font_height;
cursor.wrapped_y += font_height;
cursor.character = 0;
cursor.unwrapped_x = 0;
cursor.wrapped_x = 0;
}
++cursor.pos;
if (cursor.pos > size){
cursor = prev_cursor;
break;
}
get_out = 0;
x = 0;
y = 0;
px = 0;
switch (seek.type){
case buffer_seek_pos:
if (cursor.pos > seek.pos){
cursor = prev_cursor;
get_out = 1;
}break;
case buffer_seek_wrapped_xy:
x = cursor.wrapped_x; px = prev_cursor.wrapped_x;
y = cursor.wrapped_y; break;
case buffer_seek_unwrapped_xy:
x = cursor.unwrapped_x; px = prev_cursor.unwrapped_x;
y = cursor.unwrapped_y; break;
case buffer_seek_line_char:
if (cursor.line == seek.line && cursor.character >= seek.character){
get_out = 1;
}
else if (cursor.line > seek.line){
cursor = prev_cursor;
get_out = 1;
}break;
}
if (get_out) break;
if (xy_seek){
if (y > seek.y){
cursor = prev_cursor;
break;
}
if (seek.round_down){
if (y > seek.y - font_height && x > seek.x){
cursor = prev_cursor;
break;
}
}
else{
if (y > seek.y - font_height && x >= seek.x){
if ((seek.x - px) < (x - seek.x)) cursor = prev_cursor;
break;
}
}
}
}
return(cursor);
}
typedef struct{
char *str;
int len;
int start, end;
} Buffer_Edit;
internal_4tech int
buffer_batch_debug_sort_check(Buffer_Edit *sorted_edits, int edit_count){
Buffer_Edit *edit;
int i, result, start_point;
result = 1;
start_point = 0;
edit = sorted_edits;
for (i = 0; i < edit_count; ++i, ++edit){
if (start_point > edit->start){
result = 0; break;
}
start_point = Max(edit->end, edit->start + 1);
}
return(result);
}
internal_4tech int
buffer_batch_edit_max_shift(Buffer_Edit *sorted_edits, int edit_count){
Buffer_Edit *edit;
int i, result;
int shift_total, shift_max;
result = 0;
shift_total = 0;
shift_max = 0;
edit = sorted_edits;
for (i = 0; i < edit_count; ++i, ++edit){
shift_total += (edit->len - (edit->end - edit->start));
if (shift_total > shift_max) shift_max = shift_total;
}
return shift_max;
}
typedef struct{
int i;
int shift_total;
} Buffer_Batch_State;
internal_4tech int
buffer_batch_edit_step(Buffer_Batch_State *state, Buffer *buffer, Buffer_Edit *sorted_edits, int edit_count){
Buffer_Edit *edit;
int i, result;
int shift_total, shift_amount;
result = 0;
shift_total = state->shift_total;
i = state->i;
edit = sorted_edits + i;
for (; i < edit_count; ++i, ++edit){
result = buffer_replace_range(buffer, edit->start + shift_total, edit->end + shift_total,
edit->str, edit->len, &shift_amount);
if (result) break;
shift_total += shift_amount;
}
state->shift_total = shift_total;
state->i = i;
return result;
}
internal_4tech void
buffer_batch_edit(Buffer *buffer, Buffer_Edit *sorted_edits, int edit_count){
Buffer_Batch_State state;
int result;
state.i = 0;
state.shift_total = 0;
result = buffer_batch_edit_step(&state, buffer, sorted_edits, edit_count);
Assert(result == 0);
}
internal_4tech void
buffer_batch_edit_update_cursors(Cursor_With_Index *sorted_positions, int count, Buffer_Edit *sorted_edits, int edit_count){
Cursor_With_Index *position, *end_position;
Buffer_Edit *edit, *end_edit;
int start, end;
int shift_amount;
position = sorted_positions;
end_position = sorted_positions + count;
edit = sorted_edits;
end_edit = sorted_edits + edit_count;
shift_amount = 0;
for (; edit < end_edit && position < end_position; ++edit){
start = edit->start;
end = edit->end;
for (; position->pos < start && position < end_position; ++position){
position->pos += shift_amount;
}
for (; position->pos < end && position < end_position; ++position){
position->pos = start + shift_amount;
}
shift_amount += (edit->len - (end - start));
}
for (; position < end_position; ++position){
position->pos += shift_amount;
}
}
internal_4tech int
buffer_find_hard_start(Buffer *buffer, int line_start, int *all_whitespace, int *all_space,
int *preferred_indent, int tab_width){
char *data;
int size;
int result;
char c;
*all_space = 0;
*preferred_indent = 0;
data = buffer->data;
size = buffer->size;
tab_width -= 1;
for (result = line_start; result < size; ++result){
c = data[result];
if (c == '\n' || c == 0){
*all_whitespace = 1;
break;
}
if (c >= '!' && c <= '~') break;
if (c == '\t') *preferred_indent += tab_width;
if (c != ' ') *all_space = 0;
*preferred_indent += 1;
}
return(result);
}
internal_4tech void
buffer_eol_convert_in(Buffer *buffer){
char *data;
int size;
int i;
data = buffer->data;
size = buffer->size;
Assert(size < buffer->max);
data[size] = 0;
for (i = 0; i < size; ++i){
if (data[i] == '\r' && data[i+1] == '\n'){
memmove_4tech(data + i, data + i + 1, size - i);
size -= 1;
}
}
buffer->size = size;
}
inline_4tech int
buffer_eol_convert_out_size(Buffer *buffer){
int size;
size = buffer->size + buffer->line_count;
return(size);
}
internal_4tech void
buffer_eol_convert_out(Buffer *buffer){
char *data;
int size;
int i;
data = buffer->data;
size = buffer->size;
Assert(buffer_eol_convert_out_size(buffer) < buffer->max);
data[size] = 0;
for (i = 0; i < size; ++i){
if (data[i] == '\n'){
memmove_4tech(data + i, data + i + 1, size - i);
data[i] = '\r';
++i;
}
}
buffer->size = size;
}
// BOTTOM