Skip to main content

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, Hash)]
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, Hash)]
31pub struct Mods {
32    pub ctrl: bool,
33    pub alt: bool,
34    pub shift: bool,
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
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    /// `y` with the fraction of a row, the same way. Without pixels it's the row's middle,
88    /// so something painted a little past its row is only hit there on its own row.
89    pub fine_y: f32,
90    pub mods: Mods,
91}
92
93impl MouseEvent {
94    /// Where the pointer is, in columns and rows with their fractions.
95    pub fn at(&self) -> (f32, f32) {
96        (self.fine_x, self.fine_y)
97    }
98}
99
100#[derive(Clone, Debug, PartialEq)]
101pub enum Event {
102    Key(KeyEvent),
103    Mouse(MouseEvent),
104    Paste(String),
105    Resize(u16, u16),
106    /// Periodic wake-up for background work such as checking files on disk.
107    Tick,
108}
109
110/// Markdown formatting, as the page's toolbar offers it.
111#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
112pub enum Format {
113    Bold,
114    Italic,
115    Code,
116    Heading,
117    Bullet,
118    Task,
119    Quote,
120    Strikethrough,
121    /// Takes a line's heading, list or quote marker away.
122    PlainText,
123    CodeBlock,
124    Table,
125    Numbered,
126    /// A heading of this level, or plain text at zero.
127    Level(u8),
128    /// A line across the page.
129    Rule,
130    Link,
131}
132
133/// Anything the user can ask for. Each entry is also a command-palette item unless it
134/// needs arguments.
135#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
136pub enum Command {
137    Move(Direction, bool),
138    Newline,
139    /// Types one character, as a key would.
140    Type(char),
141    Indent,
142    Outdent,
143    DeleteBackward,
144    DeleteForward,
145    Undo,
146    Redo,
147    Copy,
148    Cut,
149    Paste,
150    SelectAll,
151    SelectNextMatch,
152    Save,
153    CloseTab,
154    NextTab,
155    PreviousTab,
156    SplitPane,
157    FocusNextPane,
158    ToggleSidebar,
159    QuickOpen,
160    /// Quick Open with `@` typed: the file's symbols, grouped by kind.
161    GoToSymbol,
162    /// Quick Open with `#` typed: symbols across the workspace, from every running server.
163    GoToWorkspaceSymbol,
164    CommandPalette,
165    FindInProject,
166    OpenDiff,
167    AddComment,
168    /// Writes a comment on what's selected and sends it to the agent as it's written.
169    AskAgent,
170    BlameLine,
171    ReplaceInProject,
172    MarkdownPreview,
173    OpenInBrowser,
174    /// Moves the keyboard between the file and the side pane.
175    FocusAgent,
176    /// Builds the paper in view, and shows the PDF.
177    BuildPdf,
178    /// Writes the document as a PDF beside it.
179    ExportPdf,
180    /// The plan in review against the version before it, as suggestions.
181    ComparePlanVersions,
182    /// Scrolls the PDF to the line the caret is on.
183    ShowInPdf,
184    /// The side pane, or the editor in front, takes the window, or gives it back.
185    MaximizePane,
186    MaximizeEditor,
187    /// Wraps long lines of code at the pane's width, or stops: Alt+Z.
188    ToggleWordWrap,
189    /// Folds the region the caret is in, or opens the fold: Ctrl+Shift+[ and ].
190    Fold,
191    Unfold,
192    FoldAll,
193    UnfoldAll,
194    ToggleComment,
195    MoveLineUp,
196    MoveLineDown,
197    DuplicateLine,
198    /// VS Code's Ctrl+Shift+K.
199    DeleteLine,
200    /// A new line below this one, or above, without splitting it: Ctrl+Enter, Ctrl+Shift+Enter.
201    InsertLineBelow,
202    InsertLineAbove,
203    /// Every place the selection or word appears, as cursors: Ctrl+Shift+L.
204    SelectAllMatches,
205    /// A cursor on the line above or below as well: Ctrl+Alt+Up and Down.
206    AddCursorAbove,
207    AddCursorBelow,
208    /// The next suggestion in the document, or the one before: Alt+F5, Shift+Alt+F5.
209    NextChange,
210    PreviousChange,
211    /// The next comment on the file in view, or the one before, its words selected and its
212    /// card lit: no key of its own, as VS Code's Next Comment Thread has none.
213    NextComment,
214    PreviousComment,
215    /// The Comments pane's Check My Review, from the palette.
216    CheckReview,
217    /// The next problem in the file, or the one before: F8, Shift+F8.
218    NextProblem,
219    PreviousProblem,
220    GoToLine,
221    DeleteWordLeft,
222    DeleteWordRight,
223    DeleteToLineStart,
224    DeleteToLineEnd,
225    FindInFile,
226    /// Find with the replace row open.
227    ReplaceInFile,
228    Format(Format),
229    SendToAgent,
230    /// Every comment kept, as Markdown on the clipboard, a file and a kind at a time.
231    CopyReview,
232    /// The same, written to a file in the project, which then opens.
233    SaveReview,
234    MentionSelection,
235    AcceptProposal,
236    RejectProposal,
237    ApprovePlan,
238    SendBackPlan,
239    ShowAgents,
240    CloseDock,
241    /// Shows the side pane with what it showed last, or puts it away.
242    TogglePanel,
243    /// Shows the document's headings in the sidebar.
244    ShowOutline,
245    /// Shows what's changed, to stage and commit, in the sidebar.
246    ShowSourceControl,
247    /// Shows what an agent works from in the sidebar: its instructions, skills, hooks and
248    /// what its session read.
249    ShowAgentContext,
250    /// Brings a terminal up in the pane, starting one if there is none, or puts it away.
251    ToggleTerminal,
252    /// Starts another terminal in the pane.
253    NewTerminal,
254    /// Brings the comments up in the pane, or puts them away.
255    ShowComments,
256    /// Brings up what waits for the person in every workspace, or puts it away.
257    ShowInbox,
258    /// Brings the pane's browser up, or starts one, or puts it away.
259    ShowBrowser,
260    /// Brings the list of agents up in the pane, or puts it away.
261    ListAgents,
262    /// Runs the notebook cell the caret is in.
263    RunCell,
264    /// Clears what every cell printed.
265    ClearOutputs,
266    RunAllCells,
267    /// Stops what the notebook's kernel is doing.
268    InterruptKernel,
269    /// Starts the notebook's kernel again, forgetting what it held.
270    RestartKernel,
271    /// Opens the guide to getting started.
272    ShowGuide,
273    /// Starts a new document.
274    NewDocument,
275    /// Keeps typing in this document as suggested changes.
276    ToggleTracking,
277    AcceptChange,
278    RejectChange,
279    /// Turns spelling and grammar checks on or off.
280    ToggleSpelling,
281    AcceptAllChanges,
282    RejectAllChanges,
283    ReviewPreviousTurn,
284    /// Starts an agent reviewing the changes, from a menu of the agents.
285    ReviewWith,
286    UndoTurn,
287    /// The Task page: the agent work here as a timeline.
288    ShowAgentTask,
289    Commit,
290    OpenClaude,
291    OpenCodex,
292    /// Moves to the next workspace in the bar along the bottom.
293    NextWorkspace,
294    PreviousWorkspace,
295    /// Opens another folder beside this one, as a workspace of its own.
296    OpenWorkspace,
297    OpenGemini,
298    /// Makes a worktree beside the project for an agent, and opens it as a workspace.
299    NewWorktree,
300    /// Opens the transcript of the agent in the side pane as a page.
301    OpenTranscript,
302    /// Names the side pane's tab in front, over what its program calls it.
303    RenamePaneTab,
304    OpenSettings,
305    SwitchBranch,
306    /// Folders and files opened lately, to open again.
307    OpenRecent,
308    /// The project's tasks, to run one in the side pane: VS Code's Run Task.
309    RunTask,
310    /// The default build task: Ctrl+Shift+B.
311    RunBuildTask,
312    RerunLastTask,
313    GoToDefinition,
314    /// The language server's fixes and refactors at the caret: Ctrl+. in VS Code.
315    QuickFix,
316    /// What the language server says about the symbol at the caret.
317    ShowHover,
318    /// Back to where the last jump left from, as VS Code's Go Back and Vim's Ctrl+O.
319    GoBack,
320    GoForward,
321    GoToReferences,
322    FormatDocument,
323    Complete,
324    RenameSymbol,
325    /// Selects this many whole lines from the cursor.
326    SelectLines(usize),
327    /// Pastes after the cursor, or below it for a copied line.
328    PasteLine,
329    /// Git: puts the changes aside, with untracked files too, or only what's staged.
330    Stash,
331    StashUntracked,
332    StashStaged,
333    /// Brings the newest stash back and drops it.
334    PopStash,
335    /// Picks a stash to apply, pop or drop.
336    ApplyStash,
337    PickStashToPop,
338    DropStash,
339    /// Compares the file in view with a branch, a commit or the clipboard.
340    CompareWith,
341    /// The repository's commits, drawn as a graph.
342    ShowHistory,
343    /// The commits that changed the file in view.
344    FileHistory,
345    /// Settles the conflict at the caret, keeping one side or both.
346    AcceptCurrent,
347    AcceptIncoming,
348    AcceptBoth,
349    /// The conflict at the caret, its two sides in the diff.
350    CompareConflict,
351    NextConflict,
352    PreviousConflict,
353    /// Gives up the merge, rebase, cherry-pick or revert under way.
354    AbortMerge,
355    Cancel,
356    Quit,
357}
358
359/// A cursor movement, independent of the rope model.
360#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
361pub enum Direction {
362    Left,
363    Right,
364    Up,
365    Down,
366    WordLeft,
367    WordRight,
368    LineStart,
369    /// Column zero, where `LineStart` stops at the indent first.
370    LineColumnZero,
371    LineEnd,
372    Top,
373    Bottom,
374    PageUp,
375    PageDown,
376    /// The start of the next word, as Vim's `w`.
377    NextWordStart,
378    /// To a character on the line: forward or back, and stopping just before it.
379    FindChar(char, bool, bool),
380}