editor_core.hpp 12 KB

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