lua_api.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_editor_new_buffer(const std::string& name);
  59. sol::optional<std::string> lua_editor_get_buffer_by_name(const std::string& name);
  60. void lua_editor_set_message(const std::string& message);
  61. void lua_config_set_string(const std::string& key, const std::string& value);
  62. std::string lua_config_get_string(const std::string& key, const std::string& default_val);
  63. void lua_config_set_bool(const std::string& key, bool value);
  64. bool lua_config_get_bool(const std::string& key, bool default_val);
  65. void lua_config_set_int(const std::string& key, int value);
  66. int lua_config_get_int(const std::string& key, int default_val);
  67. // New: Create and register a theme from Lua
  68. sol::object lua_create_and_register_theme(const std::string& name);
  69. // Legacy methods for backward compatibility
  70. [[nodiscard]] bool has_key_binding(const std::string& key) const;
  71. private:
  72. sol::state lua_;
  73. EditorCore* core_ = nullptr;
  74. std::map<std::string, sol::function> legacy_key_bindings_; // For backward compatibility
  75. /// Initialize Lua state and libraries.
  76. void setup_api();
  77. /// Register C++ user types (usertypes) with Sol2.
  78. void register_types();
  79. /// Register global C++ functions in Lua.
  80. void register_functions();
  81. /// @brief Create a CommandFunction that wraps a Lua callback.
  82. /// This eliminates code duplication across bind_key and register_command.
  83. /// @param callback The Lua function to wrap.
  84. /// @param error_context Description of the context for error messages.
  85. /// @return A CommandFunction suitable for CommandSystem::register_command.
  86. CommandFunction wrap_lua_callback(sol::function callback, const std::string& error_context);
  87. };
  88. } // namespace lumacs