editor_core.hpp 12 KB

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