Ver Fonte

feat: Enable keyboard macro recording

- Enable macro recording in KeyBindingManager::process_key for bound commands.
- Expose EditorCore via CommandSystem to allow KeyBindingManager to check recording status.
- Implement self-insert recording in GtkEditor and TuiEditor fallbacks.
- Fix logic to prevent recording the macro stop key.
Bernardo Magri há 1 mês atrás
pai
commit
6c4359b248
4 ficheiros alterados com 34 adições e 0 exclusões
  1. 3 0
      include/lumacs/command_system.hpp
  2. 7 0
      src/gtk_editor.cpp
  3. 17 0
      src/keybinding.cpp
  4. 7 0
      src/tui_editor.cpp

+ 3 - 0
include/lumacs/command_system.hpp

@@ -112,6 +112,9 @@ public:
     /// @return CommandResult indicating success/failure and a message, or a pending state.
     CommandResult execute_interactive(const std::string& name);
 
+    /// @brief Access the EditorCore instance.
+    EditorCore& core() const { return core_; }
+
 private:
     EditorCore& core_; // Reference to EditorCore
     MinibufferManager& minibuffer_manager_; // Reference to MinibufferManager

+ 7 - 0
src/gtk_editor.cpp

@@ -337,6 +337,13 @@ bool GtkEditor::on_global_key_pressed(guint keyval, guint keycode, Gdk::Modifier
             if (!has_ctrl && !has_meta && lumacs_key_name.length() == 1) {
                  // Execute self-insert-command
                  core_->command_system().execute("self-insert-command", {lumacs_key_name});
+                 
+                 // --- Macro Recording Logic for Self-Insert ---
+                 if (core_->is_recording_macro()) {
+                     core_->record_key_sequence(lumacs_key_name);
+                 }
+                 // --------------------------------------------
+
                  queue_redraw_all_windows(content_widget_);
                  return true;
             }

+ 17 - 0
src/keybinding.cpp

@@ -1,5 +1,6 @@
 #include "lumacs/keybinding.hpp"
 #include "lumacs/command_system.hpp" // Include for CommandSystem
+#include "lumacs/editor_core.hpp" // Needed for EditorCore definition
 #include <sstream>
 #include <algorithm>
 #include <cctype>
@@ -11,6 +12,7 @@ namespace lumacs {
 // ============================================================================ 
 
 namespace {
+// ... (keep anonymous namespace as is) ...
     // Helper to convert string to BaseKey
     BaseKey string_to_base_key(const std::string& s) {
         if (s.length() == 1) {
@@ -359,12 +361,27 @@ KeyProcessingResult KeyBindingManager::process_key(const Key& key) {
     if (node && node->binding.has_value()) {
         // Found exact match
         KeyBinding bound_command = node->binding.value();
+        
+        // Capture the raw key string of the full sequence before clearing
+        std::string sequence_str = current_sequence_.to_string(); 
         clear_partial_sequence();
         
         try {
             // Execute the command via the CommandSystem
             if (command_system_) {
+                // --- Macro Recording Logic ---
+                bool was_recording = command_system_->core().is_recording_macro();
+
                 CommandResult cmd_result = command_system_->execute(bound_command.command_name, {}); // No args for now
+
+                bool is_recording = command_system_->core().is_recording_macro();
+
+                // Only record if we were recording BEFORE execution and are STILL recording AFTER execution.
+                if (was_recording && is_recording) {
+                    command_system_->core().record_key_sequence(sequence_str);
+                }
+                // -----------------------------
+
                 return KeyProcessingResult(cmd_result.status == CommandStatus::Success ? KeyResult::Executed : KeyResult::Failed, cmd_result);
             }
             return KeyProcessingResult(KeyResult::Failed, CommandResult{CommandStatus::Failure, "No CommandSystem available"});

+ 7 - 0
src/tui_editor.cpp

@@ -287,6 +287,13 @@ bool TuiEditor::handle_input(int ch) {
             // Actually, resolve_key might return special single chars? No, most are named.
             // But let's be safe.
             core_->command_system().execute("self-insert-command", {key_name});
+            
+            // --- Macro Recording Logic for Self-Insert ---
+            if (core_->is_recording_macro()) {
+                core_->record_key_sequence(key_name);
+            }
+            // --------------------------------------------
+
             return true;
         }
     }