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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
use std::iter::Peekable;
use crate::{
edit_mode::vi::ViMode, Direction, EditCommand, MotionTarget, ReedlineEvent, Vi, WordEdge,
WordKind,
};
use super::parser::{ParseResult, ReedlineOption};
pub fn parse_motion<'iter, I>(
input: &mut Peekable<I>,
command_char: Option<char>,
) -> ParseResult<Motion>
where
I: Iterator<Item = &'iter char>,
{
match input.peek() {
Some('h') => {
let _ = input.next();
ParseResult::Valid(Motion::Left)
}
Some('l') => {
let _ = input.next();
ParseResult::Valid(Motion::Right)
}
Some('j') => {
let _ = input.next();
ParseResult::Valid(Motion::Down)
}
Some('k') => {
let _ = input.next();
ParseResult::Valid(Motion::Up)
}
Some('b') => {
let _ = input.next();
ParseResult::Valid(Motion::PreviousWord)
}
Some('B') => {
let _ = input.next();
ParseResult::Valid(Motion::PreviousBigWord)
}
Some('w') => {
let _ = input.next();
ParseResult::Valid(Motion::NextWord)
}
Some('W') => {
let _ = input.next();
ParseResult::Valid(Motion::NextBigWord)
}
Some('e') => {
let _ = input.next();
ParseResult::Valid(Motion::NextWordEnd)
}
Some('E') => {
let _ = input.next();
ParseResult::Valid(Motion::NextBigWordEnd)
}
Some('0') => {
let _ = input.next();
ParseResult::Valid(Motion::Start)
}
Some('^') => {
let _ = input.next();
ParseResult::Valid(Motion::NonBlankStart)
}
Some('$') => {
let _ = input.next();
ParseResult::Valid(Motion::End)
}
Some('f') => {
let _ = input.next();
match input.peek() {
Some(&x) => {
input.next();
ParseResult::Valid(Motion::RightUntil(*x))
}
None => ParseResult::Incomplete,
}
}
Some('t') => {
let _ = input.next();
match input.peek() {
Some(&x) => {
input.next();
ParseResult::Valid(Motion::RightBefore(*x))
}
None => ParseResult::Incomplete,
}
}
Some('F') => {
let _ = input.next();
match input.peek() {
Some(&x) => {
input.next();
ParseResult::Valid(Motion::LeftUntil(*x))
}
None => ParseResult::Incomplete,
}
}
Some('T') => {
let _ = input.next();
match input.peek() {
Some(&x) => {
input.next();
ParseResult::Valid(Motion::LeftBefore(*x))
}
None => ParseResult::Incomplete,
}
}
Some(';') => {
let _ = input.next();
ParseResult::Valid(Motion::ReplayCharSearch)
}
Some(',') => {
let _ = input.next();
ParseResult::Valid(Motion::ReverseCharSearch)
}
Some('g') => {
let _ = input.next();
match input.peek() {
Some('g') => {
input.next();
ParseResult::Valid(Motion::FirstLine)
}
Some(_) => ParseResult::Invalid,
None => ParseResult::Incomplete,
}
}
Some('G') => {
let _ = input.next();
ParseResult::Valid(Motion::LastLine)
}
ch if ch == command_char.as_ref().as_ref() && command_char.is_some() => {
let _ = input.next();
ParseResult::Valid(Motion::Line)
}
None => ParseResult::Incomplete,
_ => ParseResult::Invalid,
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum Motion {
Left,
Right,
Up,
Down,
NextWord,
NextBigWord,
NextWordEnd,
NextBigWordEnd,
PreviousWord,
PreviousBigWord,
Line,
Start,
NonBlankStart,
End,
FirstLine,
LastLine,
RightUntil(char),
RightBefore(char),
LeftUntil(char),
LeftBefore(char),
ReplayCharSearch,
ReverseCharSearch,
}
impl Motion {
/// The [`MotionTarget`] this motion resolves to, for motions with a static
/// target.
///
/// `None` for motions that have no fixed mapping: `h`/`l`, `^`, the
/// doubled-operator `Line` (`dd`/`cc`/`yy`), and the `;`/`,` replays (whose
/// target is the dynamic `last_char_search`). Those keep their own lowering.
/// Every motion with a static target reads this one map on both the
/// bare-motion path (`Move`/`Extend`) and the operator path (`Cut`/`Copy`),
/// so its semantics live in a single place.
///
/// `j`/`k` map to [`MotionTarget::Line`] for the *operator* path
/// (`dj`/`cj`/`yj` snap linewise); their bare-motion arms in
/// [`Motion::to_reedline`] never consult this map, because a bare `j`/`k`
/// is not a buffer motion at all — it lowers to menu/history events.
pub(super) fn target(&self) -> Option<MotionTarget> {
// A word target, spelled compactly.
let word = |kind: WordKind, edge: WordEdge, direction: Direction| MotionTarget::Word {
kind,
edge,
direction,
};
match self {
// `w` / `W` — start of the next word, forward.
Motion::NextWord => Some(word(WordKind::Word, WordEdge::Start, Direction::Forward)),
Motion::NextBigWord => Some(word(
WordKind::LongWord,
WordEdge::Start,
Direction::Forward,
)),
// `e` / `E` — end of the next word, forward.
Motion::NextWordEnd => Some(word(WordKind::Word, WordEdge::End, Direction::Forward)),
Motion::NextBigWordEnd => {
Some(word(WordKind::LongWord, WordEdge::End, Direction::Forward))
}
// `b` / `B` — start of the previous word, backward.
Motion::PreviousWord => {
Some(word(WordKind::Word, WordEdge::Start, Direction::Backward))
}
Motion::PreviousBigWord => Some(word(
WordKind::LongWord,
WordEdge::Start,
Direction::Backward,
)),
// `0` / `$` — start / end of the current line.
Motion::Start => Some(MotionTarget::LineEdge(Direction::Backward)),
Motion::End => Some(MotionTarget::LineEdge(Direction::Forward)),
Motion::RightUntil(c) => Some(MotionTarget::Find {
ch: *c,
direction: Direction::Forward,
stop: crate::FindStop::On,
}),
Motion::RightBefore(c) => Some(MotionTarget::Find {
ch: *c,
direction: Direction::Forward,
stop: crate::FindStop::Before,
}),
Motion::LeftUntil(c) => Some(MotionTarget::Find {
ch: *c,
direction: Direction::Backward,
stop: crate::FindStop::On,
}),
Motion::LeftBefore(c) => Some(MotionTarget::Find {
ch: *c,
direction: Direction::Backward,
stop: crate::FindStop::Before,
}),
Motion::FirstLine => Some(MotionTarget::BufferEdge(Direction::Backward)),
Motion::LastLine => Some(MotionTarget::BufferEdge(Direction::Forward)),
// `j`/`k` — the adjacent line, for the operator path only (see the
// method docs; the bare-motion arms lower to events instead).
Motion::Down => Some(MotionTarget::Line(Direction::Forward)),
Motion::Up => Some(MotionTarget::Line(Direction::Backward)),
// Not yet lowered — keep the existing per-variant EditCommand path.
_ => None,
}
}
pub fn to_reedline(&self, vi_state: &mut Vi) -> Vec<ReedlineOption> {
let select_mode = vi_state.mode == ViMode::Visual;
match self {
Motion::Left => vec![ReedlineOption::Event(ReedlineEvent::UntilFound(vec![
ReedlineEvent::MenuLeft,
ReedlineEvent::Edit(vec![EditCommand::MoveLeft {
select: select_mode,
}]),
]))],
Motion::Right => vec![ReedlineOption::Event(ReedlineEvent::UntilFound(vec![
ReedlineEvent::HistoryHintComplete,
ReedlineEvent::MenuRight,
ReedlineEvent::Edit(vec![EditCommand::MoveRight {
select: select_mode,
}]),
]))],
Motion::Up => vec![if select_mode {
ReedlineOption::Edit(EditCommand::MoveLineUp { select: true })
} else {
ReedlineOption::Event(ReedlineEvent::UntilFound(vec![
ReedlineEvent::MenuUp,
ReedlineEvent::Up,
]))
}],
Motion::Down => vec![if select_mode {
ReedlineOption::Edit(EditCommand::MoveLineDown { select: true })
} else {
ReedlineOption::Event(ReedlineEvent::UntilFound(vec![
ReedlineEvent::MenuDown,
ReedlineEvent::Down,
]))
}],
// Motions with a `MotionTarget` collapse to one dispatch: resolve the
// target (see `Motion::target`), then move or extend by visual mode.
Motion::NextWord
| Motion::NextBigWord
| Motion::NextWordEnd
| Motion::NextBigWordEnd
| Motion::PreviousWord
| Motion::PreviousBigWord
| Motion::FirstLine
| Motion::LastLine
| Motion::Start
| Motion::End => {
// These arms cover exactly the variants `target()` resolves, so
// `None` is unreachable; degrade to a no-op rather than panic if
// that ever drifts.
let Some(target) = self.target() else {
return vec![];
};
let edit = if select_mode {
EditCommand::Extend(target)
} else {
EditCommand::Move(target)
};
vec![ReedlineOption::Edit(edit)]
}
Motion::Line => vec![], // Placeholder as unusable standalone motion
Motion::NonBlankStart => {
vec![ReedlineOption::Edit(EditCommand::MoveToLineNonBlankStart {
select: select_mode,
})]
}
Motion::RightUntil(_)
| Motion::RightBefore(_)
| Motion::LeftUntil(_)
| Motion::LeftBefore(_) => {
let Some(target) = self.target() else {
return vec![];
};
vi_state.last_char_search = Some(target);
let edit = if select_mode {
EditCommand::Extend(target)
} else {
EditCommand::Move(target)
};
vec![ReedlineOption::Edit(edit)]
}
Motion::ReplayCharSearch => vi_state
.last_char_search
.map(|target| {
let edit = if select_mode {
EditCommand::Extend(target)
} else {
EditCommand::Move(target)
};
vec![ReedlineOption::Edit(edit)]
})
.unwrap_or_default(),
Motion::ReverseCharSearch => vi_state
.last_char_search
.map(|target| {
let edit = if select_mode {
EditCommand::Extend(target.reversed())
} else {
EditCommand::Move(target.reversed())
};
vec![ReedlineOption::Edit(edit)]
})
.unwrap_or_default(),
}
}
}