1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use cell_sheet_core::model::CellPos;
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Direction {
Up,
Down,
Left,
Right,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CaseOp {
ToLower,
ToUpper,
/// Toggle every character's case.
ToggleAll,
/// Toggle only the first character's case; the handler also advances the
/// cursor right by one column (vim `~` semantics).
ToggleFirst,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SearchDirection {
Forward,
Backward,
}
/// Discriminator for the prompt rendered in `Mode::Command`.
///
/// `:` parses ex-style commands. `/` and `?` parse a search pattern that
/// is dispatched as `Action::Search` with the corresponding direction.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CommandKind {
Colon,
Slash,
Question,
}
impl CommandKind {
pub fn prefix(self) -> char {
match self {
CommandKind::Colon => ':',
CommandKind::Slash => '/',
CommandKind::Question => '?',
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Action {
Noop,
/// Move the cursor `count` cells in `dir`. Count of 1 is the default
/// single-step move; counts above 1 implement vim's `[count]hjkl`
/// semantics. The handler clamps to grid bounds with saturating
/// arithmetic and updates the viewport once at the end.
MoveCursor(Direction, usize),
#[allow(dead_code)]
MoveCursorTo(CellPos),
EditCell(CellPos, String),
ClearCell(CellPos),
ClearRange {
start: CellPos,
end: CellPos,
},
ChangeCell(CellPos),
ChangeRange {
start: CellPos,
end: CellPos,
},
#[allow(dead_code)]
YankCell(CellPos),
YankRange {
start: CellPos,
end: CellPos,
},
Paste(CellPos),
PasteBefore(CellPos),
Undo,
Redo,
ChangeMode(Mode),
Save(Option<PathBuf>),
ForceSave(Option<PathBuf>),
Open(PathBuf),
Quit {
force: bool,
},
Sort {
col: usize,
ascending: bool,
},
Search {
pattern: String,
direction: SearchDirection,
},
SearchNext,
SearchPrev,
/// Switch to `Mode::Command` with a search prompt (`/` or `?`).
/// Saves the current cursor as the search origin so Esc can restore it.
EnterSearch(SearchDirection),
/// Re-run the search from the saved origin as the user types in the
/// `/` or `?` prompt (vim's `incsearch`). Empty pattern restores the
/// cursor to the origin without committing a pattern.
SearchIncremental {
pattern: String,
direction: SearchDirection,
},
/// Esc out of a `/` or `?` prompt: restore the cursor to the saved
/// search origin and clear it.
CancelSearch,
/// Move the cursor to the next non-empty cell in the current row whose
/// displayed value starts with `ch` (case-insensitive). `forward = false`
/// scans left. `inclusive = true` lands on the match (`f`/`F`).
FindCharInRow {
ch: char,
forward: bool,
inclusive: bool,
},
/// Repeat the last `f`/`F` find. When `reversed`, flip the direction
/// (i.e. vim's `,`).
RepeatFind {
reversed: bool,
},
#[allow(dead_code)]
Resize,
PageDown,
PageUp,
HalfPageDown,
HalfPageUp,
GotoFirstRow,
GotoLastRow,
/// Jump to a specific 1-indexed row, vim's `[count]G` / `[count]gg`.
/// The handler clamps to `[1, row_count]`.
GotoRow(usize),
GotoFirstCol,
GotoLastCol,
NextNonEmpty(usize),
PrevNonEmpty(usize),
DeleteRow {
start: usize,
count: usize,
},
YankRow {
start: usize,
count: usize,
},
ShowHelp(Option<String>),
ScrollCursorTop,
ScrollCursorCenter,
ScrollCursorBottom,
CursorToViewportTop,
CursorToViewportMiddle,
CursorToViewportBottom,
ScrollLineDown,
ScrollLineUp,
SetMark(char),
JumpToMark {
name: char,
line_wise: bool,
},
JumpBack,
JumpForward,
BlockJumpDown,
BlockJumpUp,
SearchCellValue {
backward: bool,
},
ReselectLastVisual,
SetDelimiter(u8),
/// Toggle the mouse-enabled flag. The actual `Enable/DisableMouseCapture`
/// syscall is issued by `run_loop` after observing the flag change.
SetMouse(bool),
/// Flip `App.mouse_enabled` to its opposite. Distinct from
/// `SetMouse(b)` so the parser doesn't need to know the current
/// state.
ToggleMouse,
MouseClickCell(CellPos),
/// Drag in progress: extend the cursor to `pos`. The Visual selection
/// is set up by `run_loop` on the first drag event after the
/// originating `Down(Left)`; subsequent drags just move the cursor and
/// the existing `VisualState` re-derives the selection.
MouseDragTo(CellPos),
/// Anchor (or extend) a whole-column selection. The corresponding
/// VisualBlock setup happens in `run_loop` parallel to the existing
/// keyboard `v` flow.
MouseSelectColumn(usize),
/// Anchor (or extend) a whole-row selection. The corresponding
/// VisualBlock setup happens in `run_loop`.
MouseSelectRow(usize),
/// Scroll the viewport. `dy > 0` scrolls toward higher row indices
/// (content moves up, viewport reveals rows further down). `dx > 0`
/// scrolls right. The cursor is NOT moved (matches Vim's mouse
/// behaviour).
MouseScroll {
dx: i32,
dy: i32,
},
SetStatus(String),
/// Re-apply the last recorded cell-mutating operation at the current cursor.
RepeatLastChange,
/// Apply a case transformation to a single cell. `ToggleFirst` also
/// advances the cursor right by one column after the edit.
CaseOpCell {
pos: CellPos,
op: CaseOp,
},
/// Apply a case transformation to every non-formula cell in the range
/// `[start, end]` (inclusive). Formula cells in the range are skipped.
CaseOpRange {
start: CellPos,
end: CellPos,
op: CaseOp,
},
/// Increment (`delta > 0`) or decrement (`delta < 0`) the numeric value
/// of a cell by `delta`. No-op on formula cells (sets a status message)
/// and on non-numeric / empty cells.
AdjustNumber {
pos: CellPos,
delta: i64,
},
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Mode {
Normal,
Insert,
Visual,
VisualLine,
VisualBlock,
Command,
Help,
}