editor_core.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #pragma once
  2. #include "lumacs/buffer.hpp"
  3. #include "lumacs/window.hpp"
  4. #include "lumacs/kill_ring.hpp"
  5. #include "lumacs/theme.hpp"
  6. #include "lumacs/config.hpp"
  7. #include "lumacs/keybinding.hpp"
  8. #include "lumacs/modeline.hpp"
  9. // #include "lumacs/minibuffer_manager.hpp" // Changed to forward declaration below
  10. #include "lumacs/ui_interface.hpp" // Include for EditorEvent
  11. #include <memory>
  12. #include <functional>
  13. #include <vector>
  14. #include <list>
  15. #include <unordered_map>
  16. #include <chrono> // For std::chrono::steady_clock
  17. #include <optional> // For std::optional
  18. namespace lumacs {
  19. class LuaApi; // Forward declaration
  20. class CommandSystem; // Forward declaration
  21. class CompletionSystem; // Forward declaration
  22. class MinibufferManager; // Forward declaration
  23. /// @brief Represents a node in the window layout tree.
  24. struct LayoutNode {
  25. enum class Type { Leaf, HorizontalSplit, VerticalSplit };
  26. Type type;
  27. // If Leaf
  28. std::shared_ptr<Window> window;
  29. // If Split
  30. std::shared_ptr<LayoutNode> child1;
  31. std::shared_ptr<LayoutNode> child2;
  32. float ratio = 0.5f; // Split ratio (0.0 to 1.0)
  33. LayoutNode(std::shared_ptr<Window> w) : type(Type::Leaf), window(w) {}
  34. LayoutNode(Type t, std::shared_ptr<LayoutNode> c1, std::shared_ptr<LayoutNode> c2)
  35. : type(t), child1(c1), child2(c2) {}
  36. };
  37. /// @brief Core logic of the Lumacs editor, independent of the UI framework.
  38. ///
  39. /// This class acts as the central controller/facade for the editor's logic.
  40. /// It manages buffers, windows, the kill ring, registers, macros, configuration,
  41. /// and subsystems like the command system and Lua API.
  42. /// It emits events to notify the UI (IEditorView) of state changes.
  43. class EditorCore {
  44. public:
  45. EditorCore();
  46. ~EditorCore();
  47. // Disable copy, allow move
  48. EditorCore(const EditorCore&) = delete;
  49. EditorCore& operator=(const EditorCore&) = delete;
  50. EditorCore(EditorCore&&) noexcept = default;
  51. EditorCore& operator=(EditorCore&&) noexcept = default;
  52. // === Message System ===
  53. /// @brief Display a message to the user (usually in the minibuffer).
  54. /// @param msg The message string to display.
  55. void set_message(std::string msg) {
  56. last_message_ = std::move(msg);
  57. message_clear_time_ = std::chrono::steady_clock::now() + std::chrono::seconds(5); // Clear after 5 seconds
  58. emit_event(EditorEvent::Message); // Use this for transient messages
  59. }
  60. /// @brief Get the last set message.
  61. const std::string& last_message() const { return last_message_; }
  62. /// @brief Check if a message is set and if its display time has expired.
  63. /// Clears the message if expired.
  64. void check_and_clear_message() {
  65. if (message_clear_time_.has_value() && std::chrono::steady_clock::now() > message_clear_time_.value()) {
  66. last_message_.clear();
  67. message_clear_time_.reset();
  68. emit_event(EditorEvent::TransientMessageCleared); // Notify UI
  69. }
  70. }
  71. // === Actions ===
  72. // These methods trigger specific input modes in the UI.
  73. void enter_command_mode() { emit_event(EditorEvent::CommandMode); }
  74. void enter_buffer_switch_mode() { emit_event(EditorEvent::BufferSwitchMode); }
  75. void enter_kill_buffer_mode() { emit_event(EditorEvent::KillBufferMode); }
  76. void enter_find_file_mode() { emit_event(EditorEvent::FindFileMode); }
  77. void enter_theme_selection_mode() { emit_event(EditorEvent::ThemeSelectionMode); }
  78. void enter_isearch_mode() { emit_event(EditorEvent::ISearchMode); }
  79. void enter_isearch_backward_mode() { emit_event(EditorEvent::ISearchBackwardMode); }
  80. // === Buffer Management ===
  81. /// @brief Get the active buffer (associated with the active window).
  82. [[nodiscard]] const Buffer& buffer() const noexcept;
  83. [[nodiscard]] Buffer& buffer() noexcept;
  84. /// @brief Load a file into a buffer and display it in the active window.
  85. /// @param path Filesystem path to load.
  86. /// @return true if successful, false otherwise.
  87. bool load_file(const std::filesystem::path& path);
  88. /// @brief Create a new empty buffer (e.g., *scratch*) and display it.
  89. /// @param name The name of the new buffer.
  90. void new_buffer(std::string name = "*scratch*");
  91. /// @brief Get a list of names of all open buffers.
  92. [[nodiscard]] std::vector<std::string> get_buffer_names() const;
  93. /// @brief Find a buffer by its name.
  94. /// @return Shared pointer to the buffer, or nullptr if not found.
  95. [[nodiscard]] std::shared_ptr<Buffer> get_buffer_by_name(const std::string& name);
  96. /// @brief Switch the active window to display the specified buffer.
  97. /// @param name The name of the buffer to switch to.
  98. /// @return true if successful.
  99. bool switch_buffer_in_window(const std::string& name);
  100. /// @brief Close (kill) a buffer.
  101. /// @param name The name of the buffer to close.
  102. /// @return true if closed, false if it doesn't exist or is the last buffer.
  103. bool close_buffer(const std::string& name);
  104. /// @brief Structure containing summary information about a buffer.
  105. struct BufferInfo {
  106. std::string name;
  107. size_t size;
  108. bool modified;
  109. std::string mode;
  110. std::optional<std::filesystem::path> filepath;
  111. };
  112. /// @brief Get detailed information about all buffers.
  113. [[nodiscard]] std::vector<BufferInfo> get_all_buffer_info() const;
  114. // === Window Management ===
  115. /// @brief Split the active window horizontally (active becomes top).
  116. void split_horizontally();
  117. /// @brief Split the active window vertically (active becomes left).
  118. void split_vertically();
  119. /// @brief Close the active window and remove it from the layout tree.
  120. void close_active_window();
  121. /// @brief Move focus to the next window in the tree traversal order.
  122. void next_window();
  123. /// @brief Safe version of next_window (deprecated, alias for next_window).
  124. void next_window_safe();
  125. /// @brief Get the currently focused window.
  126. std::shared_ptr<Window> active_window() const { return active_window_; }
  127. /// @brief Set the active window explicitly (must exist in the tree).
  128. bool set_active_window(std::shared_ptr<Window> window);
  129. /// @brief Get the root node of the window layout tree.
  130. std::shared_ptr<LayoutNode> root_layout() const { return root_node_; }
  131. // === Cursor Management (Proxies to active window) ===
  132. [[nodiscard]] Position cursor() const noexcept;
  133. void set_cursor(Position pos);
  134. // === Cursor Movement (Proxies to active window) ===
  135. void move_up();
  136. void move_down();
  137. void move_left();
  138. void move_right();
  139. void move_to_line_start();
  140. void move_to_line_end();
  141. void move_forward_word();
  142. void move_backward_word();
  143. void page_up();
  144. void page_down();
  145. void goto_beginning();
  146. void goto_end();
  147. void goto_line(size_t line);
  148. // === Viewport Management (Proxies to active window) ===
  149. const Viewport& viewport() const noexcept;
  150. void set_viewport_size(int width, int height);
  151. void adjust_scroll();
  152. std::pair<size_t, size_t> visible_line_range() const;
  153. // === Event Callbacks ===
  154. using EventCallback = std::function<void(EditorEvent)>;
  155. /// @brief Register a callback to be notified of editor events.
  156. void on_event(EventCallback callback) {
  157. event_callbacks_.push_back(std::move(callback));
  158. }
  159. /// @brief Clear all registered event callbacks.
  160. void clear_event_callbacks() {
  161. event_callbacks_.clear();
  162. }
  163. // === Actions ===
  164. /// @brief Request the application to quit.
  165. void request_quit() {
  166. emit_event(EditorEvent::Quit);
  167. }
  168. // === Undo/Redo ===
  169. bool undo();
  170. bool redo();
  171. bool can_undo() const;
  172. bool can_redo() const;
  173. // === Kill Ring ===
  174. /// @brief Access the global Kill Ring (clipboard history).
  175. [[nodiscard]] KillRing& kill_ring() noexcept { return kill_ring_; }
  176. [[nodiscard]] const KillRing& kill_ring() const noexcept { return kill_ring_; }
  177. // === Registers ===
  178. /// @brief Save text to a named register.
  179. void copy_to_register(char register_name, const std::string& text);
  180. /// @brief Insert text from a named register.
  181. bool insert_register(char register_name);
  182. /// @brief Copy the active region to a register (C-x r s).
  183. void copy_region_to_register(char register_name);
  184. /// @brief Insert text from a register (C-x r i).
  185. bool yank_from_register(char register_name);
  186. // === Keyboard Macros ===
  187. /// @brief Start recording a keyboard macro (F3).
  188. void start_kbd_macro();
  189. /// @brief Stop recording or execute the last macro (F4).
  190. void end_kbd_macro_or_call();
  191. /// @brief Record a key sequence if macro recording is active.
  192. void record_key_sequence(const std::string& key_sequence);
  193. /// @brief Check if macro recording is currently active.
  194. [[nodiscard]] bool is_recording_macro() const noexcept { return recording_macro_; }
  195. // === Rectangles ===
  196. /// @brief Kill (cut) a rectangular region (C-x r k).
  197. void kill_rectangle();
  198. /// @brief Yank (paste) the last killed rectangle (C-x r y).
  199. void yank_rectangle();
  200. /// @brief Replace a rectangular region with a string (C-x r t).
  201. void string_rectangle(const std::string& text);
  202. // === Standard Editing Commands ===
  203. /// @brief Kill text from cursor to end of line (C-k).
  204. void kill_line();
  205. /// @brief Kill the text in the active region (C-w).
  206. void kill_region();
  207. /// @brief Copy the active region to the kill ring (M-w).
  208. void copy_region_as_kill();
  209. /// @brief Yank (paste) from the kill ring (C-y).
  210. void yank();
  211. /// @brief Replace the just-yanked text with the next item in kill ring (M-y).
  212. void yank_pop();
  213. /// @brief Kill word forward (M-d).
  214. void kill_word();
  215. /// @brief Kill word backward (M-Backspace).
  216. void backward_kill_word();
  217. // === Theme Management ===
  218. [[nodiscard]] ThemeManager& theme_manager() noexcept { return theme_manager_; }
  219. [[nodiscard]] const ThemeManager& theme_manager() const noexcept { return theme_manager_; }
  220. /// @brief Set the active theme by name.
  221. void set_theme(const std::string& name) { theme_manager_.set_active_theme(name); }
  222. /// @brief Get the currently active theme.
  223. [[nodiscard]] std::shared_ptr<Theme> active_theme() const { return theme_manager_.active_theme(); }
  224. // === Configuration ===
  225. [[nodiscard]] Config& config() noexcept { return config_; }
  226. [[nodiscard]] const Config& config() const noexcept { return config_; }
  227. // === Key Binding System ===
  228. [[nodiscard]] KeyBindingManager& keybinding_manager() noexcept { return keybinding_manager_; }
  229. [[nodiscard]] const KeyBindingManager& keybinding_manager() const noexcept { return keybinding_manager_; }
  230. // === Lua API ===
  231. [[nodiscard]] LuaApi* lua_api() const { return lua_api_.get(); }
  232. // === Command System ===
  233. [[nodiscard]] CommandSystem& command_system() noexcept { return *command_system_; }
  234. [[nodiscard]] const CommandSystem& command_system() const noexcept { return *command_system_; }
  235. // === Modeline Manager ===
  236. [[nodiscard]] ModelineManager& modeline_manager() noexcept { return modeline_manager_; }
  237. [[nodiscard]] const ModelineManager& modeline_manager() const noexcept { return modeline_manager_; }
  238. // === Minibuffer Manager ===
  239. [[nodiscard]] MinibufferManager& minibuffer_manager() noexcept { return *minibuffer_manager_; }
  240. [[nodiscard]] const MinibufferManager& minibuffer_manager() const noexcept { return *minibuffer_manager_; }
  241. // === Completion System ===
  242. [[nodiscard]] CompletionSystem& completion_system() noexcept { return *completion_system_; }
  243. [[nodiscard]] const CompletionSystem& completion_system() const noexcept { return *completion_system_; }
  244. private:
  245. std::list<std::shared_ptr<Buffer>> buffers_;
  246. // Word movement helpers
  247. Position calculate_forward_word_pos(Position start_pos);
  248. Position calculate_backward_word_pos(Position start_pos);
  249. // Window layout
  250. std::shared_ptr<LayoutNode> root_node_;
  251. std::shared_ptr<Window> active_window_;
  252. std::string last_message_;
  253. std::optional<std::chrono::steady_clock::time_point> message_clear_time_;
  254. std::vector<EventCallback> event_callbacks_;
  255. // Kill ring for cut/copy/paste
  256. KillRing kill_ring_;
  257. // Last yank position (for yank-pop)
  258. std::optional<Position> last_yank_start_;
  259. std::optional<Position> last_yank_end_;
  260. // Registers for storing text (a-z, A-Z, 0-9)
  261. std::unordered_map<char, std::string> registers_;
  262. // Keyboard macros
  263. std::vector<std::string> current_macro_;
  264. std::vector<std::string> last_macro_;
  265. bool recording_macro_ = false;
  266. // Rectangle storage
  267. std::vector<std::string> rectangle_kill_ring_;
  268. // Subsystems
  269. ThemeManager theme_manager_;
  270. Config config_;
  271. std::unique_ptr<CommandSystem> command_system_; // Must be declared before KeyBindingManager
  272. KeyBindingManager keybinding_manager_;
  273. std::unique_ptr<LuaApi> lua_api_;
  274. ModelineManager modeline_manager_;
  275. void emit_event(EditorEvent event);
  276. // Helper to find a node containing the active window
  277. LayoutNode* find_parent_node(LayoutNode* root, std::shared_ptr<Window> target);
  278. // Helper to collect all windows in traversal order
  279. void collect_windows(LayoutNode* node, std::vector<std::shared_ptr<Window>>& windows);
  280. };
  281. } // namespace lumacs