lua_api.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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& 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. KeyResult process_key(const std::string& key);
  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. // Legacy methods for backward compatibility
  55. [[nodiscard]] bool has_key_binding(const std::string& key) const;
  56. bool execute_key_binding(const std::string& key);
  57. private:
  58. sol::state lua_;
  59. EditorCore* core_ = nullptr;
  60. std::map<std::string, sol::function> legacy_key_bindings_; // For backward compatibility
  61. /// Initialize Lua state and libraries.
  62. void setup_api();
  63. /// Register C++ user types (usertypes) with Sol2.
  64. void register_types();
  65. /// Register global C++ functions in Lua.
  66. void register_functions();
  67. };
  68. } // namespace lumacs