editor_core.hpp 9.4 KB

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