4coder/custom/4coder_keyboard_macro.cpp

81 lines
2.6 KiB
C++

/*
4coder_keyboard_macro.cpp - Keyboard macro recording and replaying commands.
*/
// TOP
global b32 global_keyboard_macro_is_recording = false;
global Range_i64 global_keyboard_macro_range = {};
function Buffer_ID
get_keyboard_log_buffer(Application_Links *app){
return(get_buffer_by_name(app, string_u8_litexpr("*keyboard*"), Access_Always));
}
function void
keyboard_macro_play_single_line(Application_Links *app, String_Const_u8 macro_line){
Scratch_Block scratch(app);
Input_Event event = parse_keyboard_event(scratch, macro_line);
if (event.kind != InputEventKind_None){
enqueue_virtual_event(app, &event);
}
}
function void
keyboard_macro_play(Application_Links *app, String_Const_u8 macro){
Scratch_Block scratch(app, Scratch_Share);
List_String_Const_u8 lines = string_split(scratch, macro, (u8*)"\n", 1);
for (Node_String_Const_u8 *node = lines.first;
node != 0;
node = node->next){
String_Const_u8 line = string_skip_chop_whitespace(node->string);
keyboard_macro_play_single_line(app, line);
}
}
////////////////////////////////
CUSTOM_COMMAND_SIG(keyboard_macro_record)
CUSTOM_DOC("Start macro recording or end macro recording if recording is in progress")
{
Buffer_ID buffer = get_keyboard_log_buffer(app);
if (!global_keyboard_macro_is_recording){
global_keyboard_macro_is_recording = true;
global_keyboard_macro_range.first = buffer_get_size(app, buffer);
}
else{
global_keyboard_macro_is_recording = false;
i64 end = buffer_get_size(app, buffer);
Buffer_Cursor cursor = buffer_compute_cursor(app, buffer, seek_pos(end));
Buffer_Cursor back_cursor = buffer_compute_cursor(app, buffer, seek_line_col(cursor.line - 1, 1));
global_keyboard_macro_range.one_past_last = back_cursor.pos;
#if 0
Scratch_Block scratch(app);
String_Const_u8 macro = push_buffer_range(app, scratch, buffer, global_keyboard_macro_range);
print_message(app, string_u8_litexpr("recorded:\n"));
print_message(app, macro);
#endif
}
}
CUSTOM_COMMAND_SIG(keyboard_macro_replay)
CUSTOM_DOC("Replay the most recently recorded keyboard macro")
{
if (global_keyboard_macro_is_recording){
return;
}
Buffer_ID buffer = get_keyboard_log_buffer(app);
Scratch_Block scratch(app, Scratch_Share);
String_Const_u8 macro = push_buffer_range(app, scratch, buffer, global_keyboard_macro_range);
#if 0
print_message(app, string_u8_litexpr("replaying:\n"));
print_message(app, macro);
#endif
keyboard_macro_play(app, macro);
}
// BOTTOM