cellix_input/lib.rs
1//! Input events and the commands they map to. Editing modes are key maps over the same
2//! commands, so views never depend on which mode is active.
3
4mod keymap;
5mod vim;
6
7pub use keymap::simple_keymap;
8pub use vim::{Vim, VimAction, VimMode};
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum Key {
12 Char(char),
13 Enter,
14 Tab,
15 BackTab,
16 Backspace,
17 Delete,
18 Esc,
19 Left,
20 Right,
21 Up,
22 Down,
23 Home,
24 End,
25 PageUp,
26 PageDown,
27 F(u8),
28}
29
30#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
31pub struct Mods {
32 pub ctrl: bool,
33 pub alt: bool,
34 pub shift: bool,
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38pub struct KeyEvent {
39 pub key: Key,
40 pub mods: Mods,
41}
42
43impl KeyEvent {
44 pub fn plain(key: Key) -> Self {
45 Self {
46 key,
47 mods: Mods::default(),
48 }
49 }
50
51 pub fn ctrl(c: char) -> Self {
52 Self {
53 key: Key::Char(c),
54 mods: Mods {
55 ctrl: true,
56 ..Mods::default()
57 },
58 }
59 }
60}
61
62#[derive(Clone, Copy, Debug, PartialEq, Eq)]
63pub enum MouseKind {
64 Left,
65 Middle,
66 Right,
67 Up,
68 Drag,
69 /// The pointer moved with no button held.
70 Move,
71 ScrollUp,
72 ScrollDown,
73 /// The wheel tipped sideways, or a trackpad moving across. Only what can move sideways
74 /// uses these: elsewhere a trackpad's drift as it scrolls down would jitter the view.
75 ScrollLeft,
76 ScrollRight,
77}
78
79#[derive(Clone, Copy, Debug, PartialEq)]
80pub struct MouseEvent {
81 pub kind: MouseKind,
82 pub x: u16,
83 pub y: u16,
84 /// `x` with the fraction of a cell, where the terminal reports pixels. Proportional
85 /// text needs this: a cell can hold two thin letters.
86 pub fine_x: f32,
87 pub mods: Mods,
88}
89
90#[derive(Clone, Debug, PartialEq)]
91pub enum Event {
92 Key(KeyEvent),
93 Mouse(MouseEvent),
94 Paste(String),
95 Resize(u16, u16),
96 /// Periodic wake-up for background work such as checking files on disk.
97 Tick,
98}
99
100/// Markdown formatting, as the page's toolbar offers it.
101#[derive(Clone, Copy, Debug, PartialEq, Eq)]
102pub enum Format {
103 Bold,
104 Italic,
105 Code,
106 Heading,
107 Bullet,
108 Task,
109 Quote,
110 Strikethrough,
111 /// Takes a line's heading, list or quote marker away.
112 PlainText,
113 CodeBlock,
114 Table,
115 Numbered,
116 /// A heading of this level, or plain text at zero.
117 Level(u8),
118 /// A line across the page.
119 Rule,
120 Link,
121}
122
123/// Anything the user can ask for. Each entry is also a command-palette item unless it
124/// needs arguments.
125#[derive(Clone, Copy, Debug, PartialEq, Eq)]
126pub enum Command {
127 Move(Direction, bool),
128 Newline,
129 /// Types one character, as a key would.
130 Type(char),
131 Indent,
132 Outdent,
133 DeleteBackward,
134 DeleteForward,
135 Undo,
136 Redo,
137 Copy,
138 Cut,
139 Paste,
140 SelectAll,
141 SelectNextMatch,
142 Save,
143 CloseTab,
144 NextTab,
145 PreviousTab,
146 SplitPane,
147 FocusNextPane,
148 ToggleSidebar,
149 QuickOpen,
150 CommandPalette,
151 FindInProject,
152 OpenDiff,
153 AddComment,
154 BlameLine,
155 ReplaceInProject,
156 MarkdownPreview,
157 OpenInBrowser,
158 /// Moves the keyboard between the file and the side pane.
159 FocusAgent,
160 /// Builds the paper in view, and shows the PDF.
161 BuildPdf,
162 /// Writes the document as a PDF beside it.
163 ExportPdf,
164 /// The plan in review against the version before it, as suggestions.
165 ComparePlanVersions,
166 /// Scrolls the PDF to the line the caret is on.
167 ShowInPdf,
168 /// The side pane, or the editor in front, takes the window, or gives it back.
169 MaximizePane,
170 MaximizeEditor,
171 ToggleComment,
172 MoveLineUp,
173 MoveLineDown,
174 DuplicateLine,
175 /// VS Code's Ctrl+Shift+K.
176 DeleteLine,
177 /// A new line below this one, or above, without splitting it: Ctrl+Enter, Ctrl+Shift+Enter.
178 InsertLineBelow,
179 InsertLineAbove,
180 /// Every place the selection or word appears, as cursors: Ctrl+Shift+L.
181 SelectAllMatches,
182 /// A cursor on the line above or below as well: Ctrl+Alt+Up and Down.
183 AddCursorAbove,
184 AddCursorBelow,
185 /// The next suggestion in the document, or the one before: Alt+F5, Shift+Alt+F5.
186 NextChange,
187 PreviousChange,
188 /// The next problem in the file, or the one before: F8, Shift+F8.
189 NextProblem,
190 PreviousProblem,
191 GoToLine,
192 DeleteWordLeft,
193 DeleteWordRight,
194 DeleteToLineStart,
195 DeleteToLineEnd,
196 FindInFile,
197 /// Find with the replace row open.
198 ReplaceInFile,
199 Format(Format),
200 SendToAgent,
201 MentionSelection,
202 AcceptProposal,
203 RejectProposal,
204 ApprovePlan,
205 SendBackPlan,
206 ShowAgents,
207 CloseDock,
208 /// Shows the document's headings in the sidebar.
209 ShowOutline,
210 /// Shows what's changed, to stage and commit, in the sidebar.
211 ShowSourceControl,
212 /// Brings a terminal up in the pane, starting one if there is none, or puts it away.
213 ToggleTerminal,
214 /// Starts another terminal in the pane.
215 NewTerminal,
216 /// Brings the comments up in the pane, or puts them away.
217 ShowComments,
218 /// Brings the pane's browser up, or starts one, or puts it away.
219 ShowBrowser,
220 /// Brings the list of agents up in the pane, or puts it away.
221 ListAgents,
222 /// Runs the notebook cell the caret is in.
223 RunCell,
224 /// Clears what every cell printed.
225 ClearOutputs,
226 RunAllCells,
227 /// Stops what the notebook's kernel is doing.
228 InterruptKernel,
229 /// Starts the notebook's kernel again, forgetting what it held.
230 RestartKernel,
231 /// Opens the guide to getting started.
232 ShowGuide,
233 /// Starts a new document.
234 NewDocument,
235 /// Keeps typing in this document as suggested changes.
236 ToggleTracking,
237 AcceptChange,
238 RejectChange,
239 /// Turns spelling and grammar checks on or off.
240 ToggleSpelling,
241 AcceptAllChanges,
242 RejectAllChanges,
243 ReviewPreviousTurn,
244 UndoTurn,
245 Commit,
246 OpenClaude,
247 OpenCodex,
248 OpenGemini,
249 OpenSettings,
250 SwitchBranch,
251 GoToDefinition,
252 /// The language server's fixes and refactors at the caret: Ctrl+. in VS Code.
253 QuickFix,
254 /// What the language server says about the symbol at the caret.
255 ShowHover,
256 /// Back to where the last jump left from, as VS Code's Go Back and Vim's Ctrl+O.
257 GoBack,
258 GoForward,
259 GoToReferences,
260 FormatDocument,
261 Complete,
262 RenameSymbol,
263 /// Selects this many whole lines from the cursor.
264 SelectLines(usize),
265 /// Pastes after the cursor, or below it for a copied line.
266 PasteLine,
267 Cancel,
268 Quit,
269}
270
271/// A cursor movement, independent of the rope model.
272#[derive(Clone, Copy, Debug, PartialEq, Eq)]
273pub enum Direction {
274 Left,
275 Right,
276 Up,
277 Down,
278 WordLeft,
279 WordRight,
280 LineStart,
281 /// Column zero, where `LineStart` stops at the indent first.
282 LineColumnZero,
283 LineEnd,
284 Top,
285 Bottom,
286 PageUp,
287 PageDown,
288 /// The start of the next word, as Vim's `w`.
289 NextWordStart,
290 /// To a character on the line: forward or back, and stopping just before it.
291 FindChar(char, bool, bool),
292}