4coder/code/custom/4coder_default_bindings.cpp

551 lines
17 KiB
C++

/*
4coder_default_bidings.cpp - Supplies the default bindings used for default 4coder behavior.
*/
// TOP
#if !defined(FCODER_DEFAULT_BINDINGS_CPP)
#define FCODER_DEFAULT_BINDINGS_CPP
#include "4coder_default_include.cpp"
// NOTE(allen): Users can declare their own managed IDs here.
#if !defined(META_PASS)
#include "generated/managed_id_metadata.cpp"
#endif
#define EXTERNAL_KEYBOARD 0
#if OS_MAC && !EXTERNAL_KEYBOARD
global u32 key_alt = KeyCode_Command;
#else
global u32 key_alt = KeyCode_Alt;
#endif
function String_Const_u8
get_lexeme_under_cursor(Application_Links* app, View_ID view, Buffer_ID buffer, Arena* arena)
{
String_Const_u8 lexeme = {0};
i64 pos = view_get_cursor_pos(app, view);
Token* token = get_token_from_pos(app, buffer, pos);
if (token != 0) {
lexeme = push_token_lexeme(app, arena, buffer, token);
}
return lexeme;
}
function void
go_to_definition(Application_Links* app, String_Const_u8 lexeme, View_ID view)
{
Code_Index_Note* note = 0;
// if we're trying to go to the definition of the same lexeme as last time
// then there are probably a typedef + declaration in different locations so
// we want to advance to the next code index note that matches this lexeme
// and then loop
if (string_match(go_to_definition_last_lexeme, lexeme))
{
Code_Index_Note_List* list = code_index__list_from_string(lexeme);
u64 i = 0;
for (Code_Index_Note *it = list->first;
it != 0;
it = it->next_in_hash, i++){
if (string_match(lexeme, it->text) && i > go_to_definition_last_lexeme_index){
note = it;
go_to_definition_last_lexeme_index = i;
break;
}
}
}
if (!note)
{
note = code_index_note_from_string(lexeme);
go_to_definition_last_lexeme = lexeme;
go_to_definition_last_lexeme_index = 0;
}
if (note == 0) return;
Buffer_ID buffer = note->file->buffer;
view_set_buffer(app, view, buffer, 0);
switch (note->note_kind)
{
case CodeIndexNote_Type:
case CodeIndexNote_Function:
case CodeIndexNote_Macro:
{
jump_to_location(app, view, note->file->buffer, note->pos.start);
} break;
default: {} break;
}
}
CUSTOM_COMMAND_SIG(cmd_enter_behavior)
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
if (buffer == 0){
buffer = view_get_buffer(app, view, Access_ReadVisible);
if (buffer != 0){
goto_jump_at_cursor(app);
lock_jump_buffer(app, buffer);
}
}
else{
Scratch_Block scratch(app);
String_Const_u8 lexeme = get_lexeme_under_cursor(app, view, buffer, scratch);
if (lexeme.size > 0) {
go_to_definition(app, lexeme, view);
}
}
}
CUSTOM_COMMAND_SIG(cmd_alt_enter_behavior)
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
if (buffer == 0){
buffer = view_get_buffer(app, view, Access_ReadVisible);
if (buffer != 0){
goto_jump_at_cursor_same_panel(app);
lock_jump_buffer(app, buffer);
}
}
else{
Scratch_Block scratch(app);
String_Const_u8 lexeme = get_lexeme_under_cursor(app, view, buffer, scratch);
if (lexeme.size > 0) {
view = get_next_view_looped_primary_panels(app, view, Access_Always);
go_to_definition(app, lexeme, view);
}
}
}
//////////////////////////////////////////////////////////////////
// Column Alignment
function i64
qol_seek_char(Application_Links *app, Buffer_ID buffer, Scan_Direction direction, i64 start_pos, u8 target_char){
Scratch_Block scratch(app);
i64 line = get_line_number_from_pos(app, buffer, start_pos);
Range_i64 range = get_line_pos_range(app, buffer, line);
range.max += 1;
String_Const_u8 string = push_buffer_range(app, scratch, buffer, range);
i64 pos = start_pos;
while(range_contains(range, pos)){
pos += direction;
u8 current_char = string.str[pos - range.min];
if (current_char == target_char){ return pos; }
}
return start_pos;
}
CUSTOM_COMMAND_SIG(qol_char_forward)
CUSTOM_DOC("[QOL] Seeks forward in current line to the selected char")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
i64 cursor_pos = view_get_cursor_pos(app, view);
i64 pos = qol_seek_char(app, buffer, Scan_Forward, cursor_pos, qol_target_char);
view_set_cursor_and_preferred_x(app, view, seek_pos(pos));
}
CUSTOM_COMMAND_SIG(qol_char_backward)
CUSTOM_DOC("[QOL] Seeks back in current line to the selected char")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
i64 cursor_pos = view_get_cursor_pos(app, view);
i64 pos = qol_seek_char(app, buffer, Scan_Backward, cursor_pos, qol_target_char);
view_set_cursor_and_preferred_x(app, view, seek_pos(pos));
}
CUSTOM_COMMAND_SIG(qol_column_toggle)
CUSTOM_DOC("[QOL] Toggles the column for bumping and selects hovered char")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
if (qol_col_cursor.pos < 0){
i64 pos = view_get_cursor_pos(app, view);
qol_target_char = buffer_get_char(app, buffer, pos);
qol_col_cursor = buffer_compute_cursor(app, buffer, seek_pos(pos));
}
else{
qol_col_cursor.pos = -1;
}
}
CUSTOM_COMMAND_SIG(qol_write_space)
CUSTOM_DOC("[QOL] Writes as many spaces needed for bumping to column")
{
Scratch_Block scratch(app);
if (qol_col_cursor.pos > 0){
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
qol_col_cursor = buffer_compute_cursor(app, buffer, seek_line_col(qol_col_cursor.line, qol_col_cursor.col));
i64 pos = view_get_cursor_pos(app, view);
i64 line = get_line_number_from_pos(app, buffer, pos);
f32 col_x = view_relative_xy_of_pos(app, view, line, qol_col_cursor.pos).x;
f32 cur_x = view_relative_xy_of_pos(app, view, line, pos).x;
Face_ID face = get_face_id(app, buffer);
f32 space_advance = get_face_metrics(app, face).space_advance;
i64 N = i64((col_x - cur_x) / space_advance);
if (N < 0){ N = 1; }
String_Const_u8 spaces = string_const_u8_push(scratch, N);
block_fill_u8(spaces.str, N, ' ');
write_text(app, spaces);
}
else{
write_space(app);
}
}
//////////////////////////////////////////////////////////////////
// Jumping
struct QOL_Point
{
Buffer_ID buffer;
i64 pos;
};
struct QOL_View_Jumps
{
b32 check;
QOL_Point point;
i64 bot, pos, top;
Point_Stack_Slot ring[128];
};
function QOL_View_Jumps*
qol_jumps(Application_Links *app, View_ID view)
{
Managed_Scope scope = view_get_managed_scope(app, view);
return scope_attachment(app, scope, qol_view_jumps, QOL_View_Jumps);
}
function QOL_Point
qol_current_point(Application_Links *app)
{
QOL_Point point = {};
View_ID view = get_active_view(app, Access_ReadVisible);
point.buffer = view_get_buffer(app, view, Access_ReadVisible);
point.pos = view_get_cursor_pos(app, view);
return point;
}
function QOL_Point
qol_point_from_slot(Application_Links *app, Point_Stack_Slot slot)
{
Marker *marker = (Marker*)managed_object_get_pointer(app, slot.object);
return marker == 0 ? QOL_Point{-1,-1} : QOL_Point{slot.buffer, marker->pos};
}
function Point_Stack_Slot
qol_alloc_slot_from_point(Application_Links *app, QOL_Point point)
{
Point_Stack_Slot slot = {};
Managed_Object object = alloc_buffer_markers_on_buffer(app, point.buffer, 1, 0);
Marker *marker = (Marker*)managed_object_get_pointer(app, object);
marker->pos = point.pos;
marker->lean_right = false;
slot.buffer = point.buffer;
slot.object = object;
return slot;
}
function void
qol_free_slot(Application_Links *app, Point_Stack_Slot slot)
{
managed_object_free(app, slot.object);
}
function b32
qol_is_jump(Application_Links *app, QOL_Point a, QOL_Point b)
{
if (!buffer_exists(app, a.buffer)){ return false; }
return a.buffer != b.buffer || 1 < range_size(get_line_range_from_pos_range(app, a.buffer, Ii64(a.pos, b.pos)));
}
function void
qol_pre_command_inner(Application_Links *app, Managed_Scope scope)
{
QOL_View_Jumps *jumps = scope_attachment(app, scope, qol_view_jumps, QOL_View_Jumps);
jumps->check = true;
jumps->point = qol_current_point(app);
}
function void
qol_post_command_inner(Application_Links *app, Managed_Scope scope)
{
QOL_View_Jumps *jumps = scope_attachment(app, scope, qol_view_jumps, QOL_View_Jumps);
QOL_Point point = qol_current_point(app);
if (jumps != 0 && jumps->check && qol_is_jump(app, jumps->point, point))
{
i64 cap = ArrayCount(jumps->ring);
if (jumps->pos - jumps->bot == cap-1)
{
qol_free_slot(app, jumps->ring[jumps->bot++ % cap]);
}
if (2*cap <= jumps->pos)
{
jumps->top -= cap;
jumps->bot -= cap;
}
jumps->ring[jumps->pos++ % cap] = qol_alloc_slot_from_point(app, jumps->point);
jumps->top = jumps->pos;
}
}
function void
qol_jump_to_point(Application_Links *app, View_ID view, QOL_Point point)
{
if (point.buffer != view_get_buffer(app, view, Access_Always))
{
view_set_buffer(app, view, point.buffer, 0);
view_set_cursor(app, view, seek_pos(point.pos));
center_view(app);
}
else
{
Range_i64 range = Ii64(view_get_cursor_pos(app, view), point.pos);
Range_i64 lines = get_line_range_from_pos_range(app, point.buffer, range);
f32 y_diff = view_line_y_difference(app, view, lines.max, lines.min);
view_set_cursor(app, view, seek_pos(point.pos));
if (rect_height(view_get_buffer_region(app, view)) < y_diff)
{
center_view(app);
}
}
}
CUSTOM_COMMAND_SIG(qol_jump_down)
CUSTOM_DOC("[QOL] Jump down the view's jump stack")
{
View_ID view = get_active_view(app, Access_ReadVisible);
QOL_View_Jumps *jumps = qol_jumps(app, view);
jumps->check = false;
if (jumps->pos == jumps->bot){ return; }
QOL_Point point = qol_point_from_slot(app, ArraySafe(jumps->ring, --jumps->pos));
if (!buffer_exists(app, point.buffer)){ return; }
if (jumps->pos+1 == jumps->top){
ArraySafe(jumps->ring, jumps->top++) = qol_alloc_slot_from_point(app, qol_current_point(app));
}
qol_jump_to_point(app, view, point);
}
CUSTOM_COMMAND_SIG(qol_jump_up)
CUSTOM_DOC("[QOL] Jump back up the view's jump stack")
{
View_ID view = get_active_view(app, Access_ReadVisible);
QOL_View_Jumps *jumps = qol_jumps(app, view);
jumps->check = false;
if (jumps->top <= jumps->pos+1){ return; }
QOL_Point point = qol_point_from_slot(app, ArraySafe(jumps->ring, ++jumps->pos));
if (!buffer_exists(app, point.buffer)){ return; }
qol_jump_to_point(app, view, point);
}
//////////////////////////////////////////////////////////////////
// Bindings
function void
bindings_cmd_misc(Mapping* m, Command_Map* map)
{
Bind(command_lister, KeyCode_W);
Bind(change_active_panel, KeyCode_E);
Bind(toggle_compilation_view, KeyCode_Minus);
}
function void
bindings_cmd_file_ops(Mapping* m, Command_Map* map)
{
Bind(set_mark, KeyCode_Space);
Bind(interactive_open_or_new, KeyCode_Comma);
Bind(interactive_switch_buffer, KeyCode_Period);
Bind(save, KeyCode_Semicolon);
}
function void
bindings_cmd_search(Mapping* m, Command_Map* map)
{
Bind(query_replace, KeyCode_S);
Bind(query_replace_identifier, KeyCode_S, key_alt);
Bind(query_replace_selection, KeyCode_S, key_alt, KeyCode_Shift);
Bind(search, KeyCode_F);
Bind(list_all_locations_of_identifier, KeyCode_D);
Bind(list_all_substring_locations_case_insensitive, KeyCode_D, key_alt);
Bind(goto_next_jump, KeyCode_T);
Bind(goto_prev_jump, KeyCode_R);
// Listers
Bind(lister_search_all, KeyCode_1);
}
function void
bindings_cmd_nav(Mapping* m, Command_Map* map)
{
Bind(seek_beginning_of_line, KeyCode_Y);
Bind(seek_end_of_line, KeyCode_P);
Bind(move_left_token_boundary, KeyCode_U);
Bind(move_right_token_boundary, KeyCode_O);
Bind(move_up, KeyCode_I);
Bind(move_left, KeyCode_J);
Bind(move_down, KeyCode_K);
Bind(move_right, KeyCode_L);
Bind(move_up_to_blank_line_end, KeyCode_H);
Bind(move_down_to_blank_line_end, KeyCode_N);
Bind(cmd_enter_behavior, KeyCode_Return);
Bind(cmd_alt_enter_behavior, KeyCode_Return, key_alt);
Bind(jump_to_last_point, KeyCode_Semicolon, KeyCode_Control);
}
function void
custom_keyboard_bindings()
{
modal_set_cursor_color_u32(modal_mode_input, 0xFF00FF00);
modal_set_cursor_color_u32(modal_mode_cmd, 0xFFFF0000);
modal_set_cursor_color_u32(modal_mode_debug, 0xFF00F0FF);
MappingScope();
// Global commands
modal_bind_all(modal_map_id_global, modal_set_mode_toggle, KeyCode_F, key_alt, 0);
modal_bind_all(modal_map_id_global, modal_set_mode_next, KeyCode_F, KeyCode_Control, 0);
modal_bind_all(modal_map_id_global, exit_4coder, KeyCode_F4, key_alt, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F1, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F2, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F3, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F4, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F5, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F6, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F7, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F8, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F9, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F10, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F11, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F12, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F13, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F14, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F15, 0, 0);
modal_bind_all(modal_map_id_global, project_fkey_command, KeyCode_F16, 0, 0);
SelectMapping(&modal_get_mode(modal_mode_cmd)->map);
SelectMap(modal_map_id_global);
{
bindings_cmd_file_ops(m, map);
bindings_cmd_misc(m, map);
bindings_cmd_search(m, map);
bindings_cmd_nav(m, map);
// Text Editing
Bind(delete_to_end_of_line, KeyCode_A);
Bind(undo, KeyCode_Z);
Bind(redo, KeyCode_B);
Bind(copy, KeyCode_C);
Bind(paste, KeyCode_V);
Bind(cut, KeyCode_X);
Bind(backspace_char, KeyCode_Backspace);
Bind(backspace_alpha_numeric_or_camel_boundary, KeyCode_Backspace, key_alt);
Bind(backspace_token_boundary, KeyCode_Backspace, KeyCode_Control);
Bind(unindent_range, KeyCode_Tab, KeyCode_Shift);
Bind(indent_range, KeyCode_Tab);
// Column Alignment
Bind(qol_column_toggle, KeyCode_BackwardSlash, key_alt);
Bind(qol_write_space, KeyCode_Space, key_alt, KeyCode_Shift);
Bind(qol_char_forward, KeyCode_L, key_alt);
Bind(qol_char_backward, KeyCode_J, key_alt);
// Jumping
Bind(qol_jump_down, KeyCode_LeftBracket);
Bind(qol_jump_up, KeyCode_RightBracket);
// Macros
Bind(keyboard_macro_start_recording, KeyCode_1, key_alt);
Bind(keyboard_macro_finish_recording, KeyCode_2, key_alt);
Bind(keyboard_macro_replay, KeyCode_3, key_alt);
// Yeet Sheet
Bind(loco_yeet_selected_range_or_jump, KeyCode_G);
}
SelectMapping(&modal_get_mode(modal_mode_input)->map);
SelectMap(modal_map_id_global);
{
BindMouse(click_set_cursor_and_mark, MouseCode_Left);
BindMouseRelease(click_set_cursor, MouseCode_Left);
BindCore(click_set_cursor_and_mark, CoreCode_ClickActivateView);
BindMouseMove(click_set_cursor_if_lbutton);
Bind(delete_char, KeyCode_Delete);
Bind(backspace_char, KeyCode_Backspace);
Bind(backspace_alpha_numeric_or_camel_boundary, KeyCode_Backspace, key_alt);
Bind(backspace_token_boundary, KeyCode_Backspace, KeyCode_Control);
Bind(move_up, KeyCode_I, key_alt);
Bind(move_down, KeyCode_K, key_alt);
BindTextInput(write_text_and_auto_indent);
Bind(indent_or_autocomplete, KeyCode_Tab);
Bind(unindent_line, KeyCode_Tab, KeyCode_Shift);
Bind(write_todo, KeyCode_T, key_alt);
Bind(write_note, KeyCode_G, key_alt);
Bind(input_enter_behavior, KeyCode_Return);
Bind(input_alt_enter_behavior, KeyCode_Return, key_alt);
}
SelectMapping(&modal_get_mode(modal_mode_debug)->map);
SelectMap(modal_map_id_global);
{
bindings_cmd_file_ops(m, map);
bindings_cmd_misc(m, map);
bindings_cmd_search(m, map);
bindings_cmd_nav(m, map);
}
}
void
custom_layer_init(Application_Links *app){
Thread_Context *tctx = get_thread_context(app);
default_framework_init(app);
set_all_default_hooks(app);
modal_init(3, tctx);
custom_keyboard_bindings();
#if 0
mapping_init(tctx, &framework_mapping);
String_ID global_map_id = vars_save_string_lit("keys_global");
String_ID file_map_id = vars_save_string_lit("keys_file");
String_ID code_map_id = vars_save_string_lit("keys_code");
#if OS_MAC
setup_mac_mapping(&framework_mapping, global_map_id, file_map_id, code_map_id);
#else
setup_default_mapping(&framework_mapping, global_map_id, file_map_id, code_map_id);
#endif
setup_essential_mapping(&framework_mapping, global_map_id, file_map_id, code_map_id);
#endif
}
#endif //FCODER_DEFAULT_BINDINGS
// BOTTOM