editor_core.hpp 9.7 KB

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