editor_core.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. /// Set the active window (if it exists in the window tree)
  105. bool set_active_window(std::shared_ptr<Window> window);
  106. /// Get the root of the layout tree (for rendering)
  107. std::shared_ptr<LayoutNode> root_layout() const { return root_node_; }
  108. // === Cursor Management (Proxies to active window) ===
  109. [[nodiscard]] Position cursor() const noexcept;
  110. void set_cursor(Position pos);
  111. // === Cursor Movement (Proxies to active window) ===
  112. void move_up();
  113. void move_down();
  114. void move_left();
  115. void move_right();
  116. void move_to_line_start();
  117. void move_to_line_end();
  118. void move_forward_word();
  119. void move_backward_word();
  120. void page_up();
  121. void page_down();
  122. void goto_beginning();
  123. void goto_end();
  124. void goto_line(size_t line);
  125. // === Viewport Management (Proxies to active window) ===
  126. const Viewport& viewport() const noexcept;
  127. void set_viewport_size(int width, int height);
  128. void adjust_scroll();
  129. std::pair<size_t, size_t> visible_line_range() const;
  130. // === Event Callbacks ===
  131. using EventCallback = std::function<void(EditorEvent)>;
  132. void on_event(EventCallback callback) {
  133. event_callbacks_.push_back(std::move(callback));
  134. }
  135. void clear_event_callbacks() {
  136. event_callbacks_.clear();
  137. }
  138. // === Actions ===
  139. void request_quit() {
  140. emit_event(EditorEvent::Quit);
  141. }
  142. // === Undo/Redo ===
  143. bool undo();
  144. bool redo();
  145. bool can_undo() const;
  146. bool can_redo() const;
  147. // === Kill Ring ===
  148. /// Get the kill ring
  149. [[nodiscard]] KillRing& kill_ring() noexcept { return kill_ring_; }
  150. [[nodiscard]] const KillRing& kill_ring() const noexcept { return kill_ring_; }
  151. // === Registers ===
  152. /// Copy text to register
  153. void copy_to_register(char register_name, const std::string& text);
  154. /// Insert text from register
  155. bool insert_register(char register_name);
  156. /// Save region to register (C-x r s)
  157. void copy_region_to_register(char register_name);
  158. /// Insert register at cursor (C-x r i)
  159. bool yank_from_register(char register_name);
  160. // === Keyboard Macros ===
  161. /// Start recording a keyboard macro (F3)
  162. void start_kbd_macro();
  163. /// End macro recording or call last macro (F4)
  164. void end_kbd_macro_or_call();
  165. /// Add a key sequence to the current macro being recorded
  166. void record_key_sequence(const std::string& key_sequence);
  167. /// Check if currently recording a macro
  168. [[nodiscard]] bool is_recording_macro() const noexcept { return recording_macro_; }
  169. // === Rectangles ===
  170. /// Kill rectangle (C-x r k) - Cut rectangular region
  171. void kill_rectangle();
  172. /// Yank rectangle (C-x r y) - Paste last rectangle
  173. void yank_rectangle();
  174. /// String rectangle (C-x r t) - Fill rectangle with string
  175. void string_rectangle(const std::string& text);
  176. /// Kill (cut) text from position to end of line
  177. void kill_line();
  178. /// Kill (cut) the active region
  179. void kill_region();
  180. /// Copy the active region to kill ring (without deleting)
  181. void copy_region_as_kill();
  182. /// Yank (paste) from kill ring
  183. void yank();
  184. /// Yank and pop to previous kill ring entry
  185. void yank_pop();
  186. /// Kill word forward
  187. void kill_word();
  188. /// Kill word backward
  189. void backward_kill_word();
  190. // === Theme Management ===
  191. /// Get the theme manager
  192. [[nodiscard]] ThemeManager& theme_manager() noexcept { return theme_manager_; }
  193. [[nodiscard]] const ThemeManager& theme_manager() const noexcept { return theme_manager_; }
  194. /// Set active theme
  195. void set_theme(const std::string& name) { theme_manager_.set_active_theme(name); }
  196. /// Get active theme
  197. [[nodiscard]] std::shared_ptr<Theme> active_theme() const { return theme_manager_.active_theme(); }
  198. // === Configuration ===
  199. /// Get the configuration manager
  200. [[nodiscard]] Config& config() noexcept { return config_; }
  201. [[nodiscard]] const Config& config() const noexcept { return config_; }
  202. // === Key Binding System ===
  203. [[nodiscard]] KeyBindingManager& keybinding_manager() noexcept { return keybinding_manager_; }
  204. [[nodiscard]] const KeyBindingManager& keybinding_manager() const noexcept { return keybinding_manager_; }
  205. // === Lua API ===
  206. [[nodiscard]] LuaApi* lua_api() const { return lua_api_.get(); }
  207. private:
  208. // All open buffers
  209. std::list<std::shared_ptr<Buffer>> buffers_;
  210. // Word movement helpers
  211. Position calculate_forward_word_pos(Position start_pos);
  212. Position calculate_backward_word_pos(Position start_pos);
  213. // Window layout
  214. std::shared_ptr<LayoutNode> root_node_;
  215. std::shared_ptr<Window> active_window_;
  216. std::string last_message_;
  217. std::vector<EventCallback> event_callbacks_;
  218. // Kill ring for cut/copy/paste
  219. KillRing kill_ring_;
  220. // Last yank position (for yank-pop)
  221. std::optional<Position> last_yank_start_;
  222. std::optional<Position> last_yank_end_;
  223. // Registers for storing text (a-z, A-Z, 0-9)
  224. std::unordered_map<char, std::string> registers_;
  225. // Keyboard macros
  226. std::vector<std::string> current_macro_;
  227. std::vector<std::string> last_macro_;
  228. bool recording_macro_ = false;
  229. // Rectangle storage (each string is one row of the rectangle)
  230. std::vector<std::string> rectangle_kill_ring_;
  231. // Theme manager
  232. ThemeManager theme_manager_;
  233. // Configuration
  234. Config config_;
  235. // Key binding system
  236. KeyBindingManager keybinding_manager_;
  237. // Lua API
  238. std::unique_ptr<LuaApi> lua_api_;
  239. void emit_event(EditorEvent event);
  240. // Helper to find a node containing the active window
  241. LayoutNode* find_parent_node(LayoutNode* root, std::shared_ptr<Window> target);
  242. // Helper to collect all windows in traversal order
  243. void collect_windows(LayoutNode* node, std::vector<std::shared_ptr<Window>>& windows);
  244. };
  245. struct LayoutNode {
  246. enum class Type { Leaf, HorizontalSplit, VerticalSplit };
  247. Type type;
  248. // If Leaf
  249. std::shared_ptr<Window> window;
  250. // If Split
  251. std::shared_ptr<LayoutNode> child1;
  252. std::shared_ptr<LayoutNode> child2;
  253. float ratio = 0.5f; // For future resizing
  254. LayoutNode(std::shared_ptr<Window> w) : type(Type::Leaf), window(w) {}
  255. LayoutNode(Type t, std::shared_ptr<LayoutNode> c1, std::shared_ptr<LayoutNode> c2)
  256. : type(t), child1(c1), child2(c2) {}
  257. };
  258. } // namespace lumacs