4coder/code/custom/4coder_default_bindings.cpp

392 lines
13 KiB
C++
Raw Normal View History

/*
4coder_default_bidings.cpp - Supplies the default bindings used for default 4coder behavior.
*/
2016-03-21 22:27:27 +00:00
// TOP
#if !defined(FCODER_DEFAULT_BINDINGS_CPP)
#define FCODER_DEFAULT_BINDINGS_CPP
2016-06-28 22:58:50 +00:00
2016-05-06 16:45:50 +00:00
#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
2019-10-14 22:57:47 +00:00
2024-02-10 23:38:37 +00:00
#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;
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
// 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;
}
}
}
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
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;
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
Buffer_ID buffer = note->file->buffer;
view_set_buffer(app, view, buffer, 0);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
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;
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
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);
}
}
}
2025-07-07 21:31:41 +00:00
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);
}
}
2024-02-10 23:38:37 +00:00
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);
2024-02-10 23:38:37 +00:00
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);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
// 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);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
Bind(move_up, KeyCode_I);
Bind(move_left, KeyCode_J);
Bind(move_down, KeyCode_K);
Bind(move_right, KeyCode_L);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
Bind(move_up_to_blank_line_end, KeyCode_H);
Bind(move_down_to_blank_line_end, KeyCode_N);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
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);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
MappingScope();
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
// 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);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
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);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
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);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
// 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);
2024-05-14 02:29:16 +00:00
2025-07-07 21:31:41 +00:00
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);
2024-02-10 23:38:37 +00:00
// 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);
2024-05-14 02:29:16 +00:00
// Yeet Sheet
Bind(loco_yeet_selected_range_or_jump, KeyCode_G);
2024-02-10 23:38:37 +00:00
}
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
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);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
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);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
Bind(move_up, KeyCode_I, key_alt);
Bind(move_down, KeyCode_K, key_alt);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
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);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
Bind(input_enter_behavior, KeyCode_Return);
Bind(input_alt_enter_behavior, KeyCode_Return, key_alt);
}
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
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);
}
}
2019-10-10 22:57:02 +00:00
void
custom_layer_init(Application_Links *app){
Thread_Context *tctx = get_thread_context(app);
default_framework_init(app);
2024-05-14 02:29:16 +00:00
2019-12-03 19:35:41 +00:00
set_all_default_hooks(app);
2024-02-10 23:38:37 +00:00
modal_init(3, tctx);
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
custom_keyboard_bindings();
2024-05-14 02:29:16 +00:00
2024-02-10 23:38:37 +00:00
#if 0
2019-12-03 19:35:41 +00:00
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);
2024-02-10 23:38:37 +00:00
#endif
2024-05-14 02:29:16 +00:00
}
2019-10-14 22:57:47 +00:00
2016-06-28 22:58:50 +00:00
#endif //FCODER_DEFAULT_BINDINGS
2016-03-21 22:27:27 +00:00
// BOTTOM