editor_core.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 <memory>
  9. #include <functional>
  10. #include <vector>
  11. #include <list>
  12. #include <unordered_map>
  13. namespace lumacs {
  14. class LuaApi; // Forward declaration
  15. class CommandSystem; // Forward declaration
  16. /// Editor state change events
  17. enum class EditorEvent {
  18. BufferModified,
  19. CursorMoved,
  20. ViewportChanged,
  21. WindowLayoutChanged, // New event
  22. WindowFocused, // New event: a different window gained focus
  23. Message, // New event
  24. CommandMode, // Trigger command mode (minibuffer)
  25. BufferSwitchMode, // Trigger buffer switch mode
  26. KillBufferMode, // Trigger kill buffer mode
  27. FindFileMode, // Trigger find file mode
  28. ISearchMode, // Trigger incremental search mode
  29. ISearchBackwardMode, // Trigger incremental search mode (backward)
  30. Quit
  31. };
  32. struct LayoutNode;
  33. /// Core editor logic, independent of UI framework
  34. class EditorCore {
  35. public:
  36. EditorCore();
  37. ~EditorCore();
  38. // Disable copy, allow move
  39. EditorCore(const EditorCore&) = delete;
  40. EditorCore& operator=(const EditorCore&) = delete;
  41. EditorCore(EditorCore&&) noexcept = default;
  42. EditorCore& operator=(EditorCore&&) noexcept = default;
  43. // === Message System ===
  44. void set_message(std::string msg) {
  45. last_message_ = std::move(msg);
  46. emit_event(EditorEvent::Message);
  47. }
  48. const std::string& last_message() const { return last_message_; }
  49. // === Actions ===
  50. void enter_command_mode() {
  51. emit_event(EditorEvent::CommandMode);
  52. }
  53. void enter_buffer_switch_mode() {
  54. emit_event(EditorEvent::BufferSwitchMode);
  55. }
  56. void enter_kill_buffer_mode() {
  57. emit_event(EditorEvent::KillBufferMode);
  58. }
  59. void enter_find_file_mode() {
  60. emit_event(EditorEvent::FindFileMode);
  61. }
  62. void enter_isearch_mode() {
  63. emit_event(EditorEvent::ISearchMode);
  64. }
  65. void enter_isearch_backward_mode() {
  66. emit_event(EditorEvent::ISearchBackwardMode);
  67. }
  68. // === Buffer Management ===
  69. /// Get the current buffer (of the active window)
  70. [[nodiscard]] const Buffer& buffer() const noexcept;
  71. [[nodiscard]] Buffer& buffer() noexcept;
  72. /// Load a file into the current window
  73. bool load_file(const std::filesystem::path& path);
  74. /// Create a new empty buffer in current window
  75. void new_buffer(std::string name = "*scratch*");
  76. /// Get list of all buffer names
  77. [[nodiscard]] std::vector<std::string> get_buffer_names() const;
  78. /// Get buffer by name (returns nullptr if not found)
  79. [[nodiscard]] std::shared_ptr<Buffer> get_buffer_by_name(const std::string& name);
  80. /// Switch active window to buffer by name
  81. bool switch_buffer_in_window(const std::string& name);
  82. /// Close buffer by name (returns false if buffer is displayed or doesn't exist)
  83. bool close_buffer(const std::string& name);
  84. /// Buffer information for list display
  85. struct BufferInfo {
  86. std::string name;
  87. size_t size;
  88. bool modified;
  89. std::string mode;
  90. std::optional<std::filesystem::path> filepath;
  91. };
  92. /// Get information about all buffers
  93. [[nodiscard]] std::vector<BufferInfo> get_all_buffer_info() const;
  94. // === Window Management ===
  95. /// Split the current window horizontally (active window becomes top, new one bottom)
  96. void split_horizontally();
  97. /// Split the current window vertically (active window becomes left, new one right)
  98. void split_vertically();
  99. /// Close the current window
  100. void close_active_window();
  101. /// Move focus to the next window
  102. void next_window();
  103. /// Move focus to the next window (only if called intentionally, not during rapid typing)
  104. void next_window_safe();
  105. /// Get the active window
  106. std::shared_ptr<Window> active_window() const { return active_window_; }
  107. /// Set the active window (if it exists in the window tree)
  108. bool set_active_window(std::shared_ptr<Window> window);
  109. /// Get the root of the layout tree (for rendering)
  110. std::shared_ptr<LayoutNode> root_layout() const { return root_node_; }
  111. // === Cursor Management (Proxies to active window) ===
  112. [[nodiscard]] Position cursor() const noexcept;
  113. void set_cursor(Position pos);
  114. // === Cursor Movement (Proxies to active window) ===
  115. void move_up();
  116. void move_down();
  117. void move_left();
  118. void move_right();
  119. void move_to_line_start();
  120. void move_to_line_end();
  121. void move_forward_word();
  122. void move_backward_word();
  123. void page_up();
  124. void page_down();
  125. void goto_beginning();
  126. void goto_end();
  127. void goto_line(size_t line);
  128. // === Viewport Management (Proxies to active window) ===
  129. const Viewport& viewport() const noexcept;
  130. void set_viewport_size(int width, int height);
  131. void adjust_scroll();
  132. std::pair<size_t, size_t> visible_line_range() const;
  133. // === Event Callbacks ===
  134. using EventCallback = std::function<void(EditorEvent)>;
  135. void on_event(EventCallback callback) {
  136. event_callbacks_.push_back(std::move(callback));
  137. }
  138. void clear_event_callbacks() {
  139. event_callbacks_.clear();
  140. }
  141. // === Actions ===
  142. void request_quit() {
  143. emit_event(EditorEvent::Quit);
  144. }
  145. // === Undo/Redo ===
  146. bool undo();
  147. bool redo();
  148. bool can_undo() const;
  149. bool can_redo() const;
  150. // === Kill Ring ===
  151. /// Get the kill ring
  152. [[nodiscard]] KillRing& kill_ring() noexcept { return kill_ring_; }
  153. [[nodiscard]] const KillRing& kill_ring() const noexcept { return kill_ring_; }
  154. // === Registers ===
  155. /// Copy text to register
  156. void copy_to_register(char register_name, const std::string& text);
  157. /// Insert text from register
  158. bool insert_register(char register_name);
  159. /// Save region to register (C-x r s)
  160. void copy_region_to_register(char register_name);
  161. /// Insert register at cursor (C-x r i)
  162. bool yank_from_register(char register_name);
  163. // === Keyboard Macros ===
  164. /// Start recording a keyboard macro (F3)
  165. void start_kbd_macro();
  166. /// End macro recording or call last macro (F4)
  167. void end_kbd_macro_or_call();
  168. /// Add a key sequence to the current macro being recorded
  169. void record_key_sequence(const std::string& key_sequence);
  170. /// Check if currently recording a macro
  171. [[nodiscard]] bool is_recording_macro() const noexcept { return recording_macro_; }
  172. // === Rectangles ===
  173. /// Kill rectangle (C-x r k) - Cut rectangular region
  174. void kill_rectangle();
  175. /// Yank rectangle (C-x r y) - Paste last rectangle
  176. void yank_rectangle();
  177. /// String rectangle (C-x r t) - Fill rectangle with string
  178. void string_rectangle(const std::string& text);
  179. /// Kill (cut) text from position to end of line
  180. void kill_line();
  181. /// Kill (cut) the active region
  182. void kill_region();
  183. /// Copy the active region to kill ring (without deleting)
  184. void copy_region_as_kill();
  185. /// Yank (paste) from kill ring
  186. void yank();
  187. /// Yank and pop to previous kill ring entry
  188. void yank_pop();
  189. /// Kill word forward
  190. void kill_word();
  191. /// Kill word backward
  192. void backward_kill_word();
  193. // === Theme Management ===
  194. /// Get the theme manager
  195. [[nodiscard]] ThemeManager& theme_manager() noexcept { return theme_manager_; }
  196. [[nodiscard]] const ThemeManager& theme_manager() const noexcept { return theme_manager_; }
  197. /// Set active theme
  198. void set_theme(const std::string& name) { theme_manager_.set_active_theme(name); }
  199. /// Get active theme
  200. [[nodiscard]] std::shared_ptr<Theme> active_theme() const { return theme_manager_.active_theme(); }
  201. // === Configuration ===
  202. /// Get the configuration manager
  203. [[nodiscard]] Config& config() noexcept { return config_; }
  204. [[nodiscard]] const Config& config() const noexcept { return config_; }
  205. // === Key Binding System ===
  206. [[nodiscard]] KeyBindingManager& keybinding_manager() noexcept { return keybinding_manager_; }
  207. [[nodiscard]] const KeyBindingManager& keybinding_manager() const noexcept { return keybinding_manager_; }
  208. // === Lua API ===
  209. [[nodiscard]] LuaApi* lua_api() const { return lua_api_.get(); }
  210. // === Command System ===
  211. [[nodiscard]] CommandSystem& command_system() noexcept { return *command_system_; }
  212. [[nodiscard]] const CommandSystem& command_system() const noexcept { return *command_system_; }
  213. private:
  214. // All open buffers
  215. std::list<std::shared_ptr<Buffer>> buffers_;
  216. // Word movement helpers
  217. Position calculate_forward_word_pos(Position start_pos);
  218. Position calculate_backward_word_pos(Position start_pos);
  219. // Window layout
  220. std::shared_ptr<LayoutNode> root_node_;
  221. std::shared_ptr<Window> active_window_;
  222. std::string last_message_;
  223. std::vector<EventCallback> event_callbacks_;
  224. // Kill ring for cut/copy/paste
  225. KillRing kill_ring_;
  226. // Last yank position (for yank-pop)
  227. std::optional<Position> last_yank_start_;
  228. std::optional<Position> last_yank_end_;
  229. // Registers for storing text (a-z, A-Z, 0-9)
  230. std::unordered_map<char, std::string> registers_;
  231. // Keyboard macros
  232. std::vector<std::string> current_macro_;
  233. std::vector<std::string> last_macro_;
  234. bool recording_macro_ = false;
  235. // Rectangle storage (each string is one row of the rectangle)
  236. std::vector<std::string> rectangle_kill_ring_;
  237. // Theme manager
  238. ThemeManager theme_manager_;
  239. // Configuration
  240. Config config_;
  241. // Key binding system
  242. KeyBindingManager keybinding_manager_;
  243. // Lua API
  244. std::unique_ptr<LuaApi> lua_api_;
  245. // Command System
  246. std::unique_ptr<CommandSystem> command_system_;
  247. void emit_event(EditorEvent event);
  248. // Helper to find a node containing the active window
  249. LayoutNode* find_parent_node(LayoutNode* root, std::shared_ptr<Window> target);
  250. // Helper to collect all windows in traversal order
  251. void collect_windows(LayoutNode* node, std::vector<std::shared_ptr<Window>>& windows);
  252. };
  253. struct LayoutNode {
  254. enum class Type { Leaf, HorizontalSplit, VerticalSplit };
  255. Type type;
  256. // If Leaf
  257. std::shared_ptr<Window> window;
  258. // If Split
  259. std::shared_ptr<LayoutNode> child1;
  260. std::shared_ptr<LayoutNode> child2;
  261. float ratio = 0.5f; // For future resizing
  262. LayoutNode(std::shared_ptr<Window> w) : type(Type::Leaf), window(w) {}
  263. LayoutNode(Type t, std::shared_ptr<LayoutNode> c1, std::shared_ptr<LayoutNode> c2)
  264. : type(t), child1(c1), child2(c2) {}
  265. };
  266. } // namespace lumacs