lua_api.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "lumacs/editor_core.hpp"
  3. #include "lumacs/theme.hpp"
  4. #include "lumacs/keybinding.hpp"
  5. #include <sol/sol.hpp>
  6. #include <functional>
  7. #include <map>
  8. #include <string>
  9. namespace lumacs {
  10. class EditorCore; // Forward declaration
  11. /// @brief Bridge between C++ Core and Lua 5.4 environment.
  12. ///
  13. /// The LuaApi class is responsible for:
  14. /// - Initializing the Lua virtual machine.
  15. /// - Exposing C++ types (Buffer, Window, etc.) to Lua.
  16. /// - Exposing C++ functions/commands to Lua.
  17. /// - Loading and executing user configuration scripts (init.lua).
  18. /// - Managing the interaction between Lua scripts and the EditorCore.
  19. class LuaApi {
  20. public:
  21. /// @brief Construct the Lua API bridge.
  22. explicit LuaApi();
  23. ~LuaApi() = default;
  24. /// @brief Connect the API to the EditorCore instance.
  25. /// @param core The editor core instance.
  26. void set_core(EditorCore& core);
  27. /// @brief Get the underlying Lua state object (sol2).
  28. [[nodiscard]] sol::state_view& get_lua_state() { return lua_; }
  29. /// @brief Load and execute a Lua file from disk.
  30. /// @param path The path to the Lua file.
  31. /// @return true if loaded and executed successfully.
  32. bool load_file(const std::filesystem::path& path);
  33. /// @brief Execute a string of Lua code.
  34. /// @param code The Lua code to execute.
  35. /// @return true if executed successfully.
  36. bool execute(std::string_view code);
  37. /// @brief Attempt to find and load the user's 'init.lua'.
  38. /// Checks standard locations (e.g., ~/.config/lumacs/init.lua).
  39. /// @return true if found and loaded.
  40. bool load_init_file();
  41. /// @brief Bind a key sequence to a Lua function.
  42. /// @param key The key sequence (e.g., "C-x C-f").
  43. /// @param callback The Lua function to call.
  44. /// @param description Optional description for the binding.
  45. void bind_key(std::string key, sol::function callback, std::string description = "");
  46. /// @brief Process a key press via the Lua layer/KeyBindingManager.
  47. /// @param key The key name/sequence.
  48. /// @return The result of processing (Unbound, Executed, etc.).
  49. KeyProcessingResult process_key(const std::string& key); // Changed return type
  50. /// @brief Get all registered Lua key bindings (Legacy).
  51. [[nodiscard]] std::map<std::string, sol::function> key_bindings() const {
  52. return legacy_key_bindings_;
  53. }
  54. bool execute_key_binding(const std::string& key);
  55. // New manual functions for core interactions
  56. int get_active_buffer_line_count_lua();
  57. void lua_editor_move_right();
  58. void lua_config_set_string(const std::string& key, const std::string& value);
  59. std::string lua_config_get_string(const std::string& key, const std::string& default_val);
  60. // Legacy methods for backward compatibility
  61. [[nodiscard]] bool has_key_binding(const std::string& key) const;
  62. private:
  63. sol::state lua_;
  64. EditorCore* core_ = nullptr;
  65. std::map<std::string, sol::function> legacy_key_bindings_; // For backward compatibility
  66. /// Initialize Lua state and libraries.
  67. void setup_api();
  68. /// Register C++ user types (usertypes) with Sol2.
  69. void register_types();
  70. /// Register global C++ functions in Lua.
  71. void register_functions();
  72. };
  73. } // namespace lumacs