| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- #pragma once
- #include "lumacs/buffer.hpp"
- #include "lumacs/window.hpp"
- #include "lumacs/kill_ring.hpp"
- #include "lumacs/theme.hpp"
- #include "lumacs/config.hpp"
- #include "lumacs/keybinding.hpp"
- #include "lumacs/modeline.hpp"
- #include "lumacs/minibuffer_manager.hpp" // Added include
- #include "lumacs/completion_system.hpp" // Added include
- #include "lumacs/ui_interface.hpp" // Include for EditorEvent
- #include "lumacs/i_command_target.hpp" // New include for ICommandTarget
- #include <memory>
- #include <functional>
- #include <vector>
- #include <list>
- #include <unordered_map>
- #include <chrono> // For std::chrono::steady_clock
- #include <optional> // For std::optional
- namespace lumacs {
- class LuaApi; // Forward declaration
- class CommandSystem; // Forward declaration
- /// @brief Represents a node in the window layout tree.
- struct LayoutNode {
- enum class Type { Leaf, HorizontalSplit, VerticalSplit };
- Type type;
-
- // If Leaf
- std::shared_ptr<Window> window;
-
- // If Split
- std::shared_ptr<LayoutNode> child1;
- std::shared_ptr<LayoutNode> child2;
- float ratio = 0.5f; // Split ratio (0.0 to 1.0)
-
- LayoutNode(std::shared_ptr<Window> w) : type(Type::Leaf), window(w) {}
- LayoutNode(Type t, std::shared_ptr<LayoutNode> c1, std::shared_ptr<LayoutNode> c2)
- : type(t), child1(c1), child2(c2) {}
- };
- /// @brief Core logic of the Lumacs editor, independent of the UI framework.
- ///
- /// This class acts as the central controller/facade for the editor's logic.
- /// It manages buffers, windows, the kill ring, registers, macros, configuration,
- /// and subsystems like the command system and Lua API.
- /// It emits events to notify the UI (IEditorView) of state changes.
- class EditorCore : public ICommandTarget { // Inherit from ICommandTarget
- public:
- EditorCore();
- ~EditorCore();
- // Disable copy, allow move
- EditorCore(const EditorCore&) = delete;
- EditorCore& operator=(const EditorCore&) = delete;
- EditorCore(EditorCore&&) noexcept = default; // Should now be fine due to unique_ptr members
- EditorCore& operator=(EditorCore&&) noexcept = default; // Should now be fine due to unique_ptr members
- // === ICommandTarget Implementation ===
- // These methods implement the ICommandTarget interface.
- // Buffer Management
- [[nodiscard]] const Buffer& buffer() const noexcept override;
- [[nodiscard]] Buffer& buffer() noexcept override;
- bool load_file(const std::filesystem::path& path) override;
- void new_buffer(std::string name = "*scratch*") override;
- [[nodiscard]] std::vector<std::string> get_buffer_names() const override;
- [[nodiscard]] std::shared_ptr<Buffer> get_buffer_by_name(const std::string& name) override;
- bool switch_buffer_in_window(const std::string& name) override;
- bool close_buffer(const std::string& name) override;
-
- // Window Management
- [[nodiscard]] std::shared_ptr<Window> active_window() const override { return active_window_; }
- bool set_active_window(std::shared_ptr<Window> window) override;
- void split_horizontally() override;
- void split_vertically() override;
- void close_active_window() override;
- void next_window() override;
-
- // Cursor Management
- [[nodiscard]] Position cursor() const noexcept override;
- void set_cursor(Position pos) override;
- // Basic Editing
- void move_up() override;
- void move_down() override;
- void move_left() override;
- void move_right() override;
- void move_to_line_start() override;
- void move_to_line_end() override;
- void move_forward_word() override;
- void move_backward_word() override;
- void page_up() override;
- void page_down() override;
- void goto_beginning() override;
- void goto_end() override;
- void goto_line(size_t line) override;
- void kill_line() override;
- void kill_region() override;
- void copy_region_as_kill() override;
- void yank() override;
- void yank_pop() override;
- void kill_word() override;
- void backward_kill_word() override;
- // Message Display
- void set_message(std::string msg) override;
- // Quit
- void request_quit() override;
- // Configuration
- Config& config() override { return config_; }
- const Config& config() const override { return config_; }
- // Theme Management
- ThemeManager& theme_manager() override { return theme_manager_; }
- const ThemeManager& theme_manager() const override { return theme_manager_; }
- // === Original EditorCore methods not part of ICommandTarget ===
- /// @brief Get the last set message.
- const std::string& last_message() const { return last_message_; }
- /// @brief Check if a message is set and if its display time has expired.
- /// Clears the message if expired.
- void check_and_clear_message();
- // === Actions ===
- // These methods trigger specific input modes in the UI.
- void enter_command_mode();
- void enter_buffer_switch_mode();
- void enter_kill_buffer_mode();
- void enter_find_file_mode();
- void enter_theme_selection_mode();
- void enter_isearch_mode();
- void enter_isearch_backward_mode();
- /// @brief Structure containing summary information about a buffer.
- struct BufferInfo {
- std::string name;
- size_t size;
- bool modified;
- std::string mode;
- std::optional<std::filesystem::path> filepath;
- };
- /// @brief Get detailed information about all buffers.
- [[nodiscard]] std::vector<BufferInfo> get_all_buffer_info() const;
- /// @brief Get the root node of the window layout tree.
- std::shared_ptr<LayoutNode> root_layout() const { return root_node_; }
- // === Viewport Management (Proxies to active window) ===
-
- const Viewport& viewport() const noexcept;
- void set_viewport_size(int width, int height);
- void adjust_scroll();
- std::pair<size_t, size_t> visible_line_range() const;
- // === Event Callbacks ===
- using EventCallback = std::function<void(EditorEvent)>;
- /// @brief Register a callback to be notified of editor events.
- void on_event(EventCallback callback) {
- event_callbacks_.push_back(std::move(callback));
- }
- /// @brief Clear all registered event callbacks.
- void clear_event_callbacks() {
- event_callbacks_.clear();
- }
- // === Undo/Redo ===
- bool undo();
- bool redo();
- bool can_undo() const;
- bool can_redo() const;
- // === Kill Ring ===
- /// @brief Access the global Kill Ring (clipboard history).
- [[nodiscard]] KillRing& kill_ring() noexcept { return kill_ring_; }
- [[nodiscard]] const KillRing& kill_ring() const noexcept { return kill_ring_; }
- // === Registers ===
- /// @brief Save text to a named register.
- void copy_to_register(char register_name, const std::string& text);
-
- /// @brief Insert text from a named register.
- bool insert_register(char register_name);
-
- /// @brief Copy the active region to a register (C-x r s).
- void copy_region_to_register(char register_name);
-
- /// @brief Insert text from a register (C-x r i).
- bool yank_from_register(char register_name);
- // === Keyboard Macros ===
- /// @brief Start recording a keyboard macro (F3).
- void start_kbd_macro();
- /// @brief Stop recording or execute the last macro (F4).
- void end_kbd_macro_or_call();
- /// @brief Record a key sequence if macro recording is active.
- void record_key_sequence(const std::string& key_sequence);
- /// @brief Check if macro recording is currently active.
- [[nodiscard]] bool is_recording_macro() const noexcept { return recording_macro_; }
- // === Rectangles ===
- /// @brief Kill (cut) a rectangular region (C-x r k).
- void kill_rectangle();
- /// @brief Yank (paste) the last killed rectangle (C-x r y).
- void yank_rectangle();
- /// @brief Replace a rectangular region with a string (C-x r t).
- void string_rectangle(const std::string& text);
- // === Key Binding System ===
- [[nodiscard]] KeyBindingManager& keybinding_manager() noexcept { return *keybinding_manager_; }
- [[nodiscard]] const KeyBindingManager& keybinding_manager() const noexcept { return *keybinding_manager_; }
-
- // === Lua API ===
- [[nodiscard]] LuaApi* lua_api() const { return lua_api_.get(); }
-
- // === Command System ===
- [[nodiscard]] CommandSystem& command_system() noexcept { return *command_system_; }
- [[nodiscard]] const CommandSystem& command_system() const noexcept { return *command_system_; }
- // === Modeline Manager ===
- [[nodiscard]] ModelineManager& modeline_manager() noexcept { return modeline_manager_; }
- [[nodiscard]] const ModelineManager& modeline_manager() const noexcept { return modeline_manager_; }
- // === Minibuffer Manager ===
- [[nodiscard]] MinibufferManager& minibuffer_manager() noexcept { return *minibuffer_manager_; }
- [[nodiscard]] const MinibufferManager& minibuffer_manager() const noexcept { return *minibuffer_manager_; }
- // === Completion System ===
- [[nodiscard]] CompletionSystem& completion_system() noexcept { return *completion_system_; }
- [[nodiscard]] const CompletionSystem& completion_system() const noexcept { return *completion_system_; }
- private:
- std::list<std::shared_ptr<Buffer>> buffers_;
- // Word movement helpers
- Position calculate_forward_word_pos(Position start_pos);
- Position calculate_backward_word_pos(Position start_pos);
- // Window layout
- std::shared_ptr<LayoutNode> root_node_;
- std::shared_ptr<Window> active_window_;
- std::string last_message_;
- std::optional<std::chrono::steady_clock::time_point> message_clear_time_;
- std::vector<EventCallback> event_callbacks_;
- // Kill ring for cut/copy/paste
- KillRing kill_ring_;
- // Last yank position (for yank-pop)
- std::optional<Position> last_yank_start_;
- std::optional<Position> last_yank_end_;
- // Registers for storing text (a-z, A-Z, 0-9)
- std::unordered_map<char, std::string> registers_;
- // Keyboard macros
- std::vector<std::string> current_macro_;
- std::vector<std::string> last_macro_;
- bool recording_macro_ = false;
- // Rectangle storage
- std::vector<std::string> rectangle_kill_ring_;
- // Subsystems
- ThemeManager theme_manager_;
- Config config_;
- std::unique_ptr<CommandSystem> command_system_; // Must be declared before KeyBindingManager
- std::unique_ptr<KeyBindingManager> keybinding_manager_; // Changed to unique_ptr
- std::unique_ptr<LuaApi> lua_api_;
- ModelineManager modeline_manager_;
- std::unique_ptr<CompletionSystem> completion_system_; // Added missing member
- std::unique_ptr<MinibufferManager> minibuffer_manager_; // Added missing member
- void emit_event(EditorEvent event);
-
- // Helper to find a node containing the active window
- LayoutNode* find_parent_node(LayoutNode* root, std::shared_ptr<Window> target);
-
- // Helper to collect all windows in traversal order
- void collect_windows(LayoutNode* node, std::vector<std::shared_ptr<Window>>& windows);
- };
- } // namespace lumacs
|