1//! Vim support for Zed.
2
3#[cfg(test)]
4mod test;
5
6mod change_list;
7mod command;
8mod digraph;
9mod helix;
10mod indent;
11mod insert;
12mod mode_indicator;
13mod motion;
14mod normal;
15mod object;
16mod replace;
17mod rewrap;
18mod state;
19mod surrounds;
20mod visual;
21
22use crate::normal::paste::Paste as VimPaste;
23use collections::HashMap;
24use editor::{
25 Anchor, Bias, Editor, EditorEvent, EditorSettings, HideMouseCursorOrigin, MultiBufferOffset,
26 SelectionEffects, ToPoint,
27 actions::Paste,
28 movement::{self, FindRange},
29};
30use gpui::{
31 Action, App, AppContext, Axis, Context, Entity, EventEmitter, KeyContext, KeystrokeEvent,
32 Render, Subscription, Task, WeakEntity, Window, actions,
33};
34use insert::{NormalBefore, TemporaryNormal};
35use language::{
36 CharKind, CharScopeContext, CursorShape, Point, Selection, SelectionGoal, TransactionId,
37};
38pub use mode_indicator::ModeIndicator;
39use motion::Motion;
40use normal::search::SearchSubmit;
41use object::Object;
42use schemars::JsonSchema;
43use serde::Deserialize;
44use settings::RegisterSetting;
45pub use settings::{
46 ModeContent, Settings, SettingsStore, UseSystemClipboard, update_settings_file,
47};
48use state::{Mode, Operator, RecordedSelection, SearchState, VimGlobals};
49use std::{mem, ops::Range, sync::Arc};
50use surrounds::SurroundsType;
51use theme::ThemeSettings;
52use ui::{IntoElement, SharedString, px};
53use vim_mode_setting::HelixModeSetting;
54use vim_mode_setting::VimModeSetting;
55use workspace::{self, Pane, Workspace};
56
57use crate::{
58 normal::{GoToPreviousTab, GoToTab},
59 state::ReplayableAction,
60};
61
62/// Number is used to manage vim's count. Pushing a digit
63/// multiplies the current value by 10 and adds the digit.
64#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
65#[action(namespace = vim)]
66struct Number(usize);
67
68#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
69#[action(namespace = vim)]
70struct SelectRegister(String);
71
72#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
73#[action(namespace = vim)]
74#[serde(deny_unknown_fields)]
75struct PushObject {
76 around: bool,
77}
78
79#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
80#[action(namespace = vim)]
81#[serde(deny_unknown_fields)]
82struct PushFindForward {
83 before: bool,
84 multiline: bool,
85}
86
87#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
88#[action(namespace = vim)]
89#[serde(deny_unknown_fields)]
90struct PushFindBackward {
91 after: bool,
92 multiline: bool,
93}
94
95#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
96#[action(namespace = vim)]
97#[serde(deny_unknown_fields)]
98/// Selects the next object.
99struct PushHelixNext {
100 around: bool,
101}
102
103#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
104#[action(namespace = vim)]
105#[serde(deny_unknown_fields)]
106/// Selects the previous object.
107struct PushHelixPrevious {
108 around: bool,
109}
110
111#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
112#[action(namespace = vim)]
113#[serde(deny_unknown_fields)]
114struct PushSneak {
115 first_char: Option<char>,
116}
117
118#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
119#[action(namespace = vim)]
120#[serde(deny_unknown_fields)]
121struct PushSneakBackward {
122 first_char: Option<char>,
123}
124
125#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
126#[action(namespace = vim)]
127#[serde(deny_unknown_fields)]
128struct PushAddSurrounds;
129
130#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
131#[action(namespace = vim)]
132#[serde(deny_unknown_fields)]
133struct PushChangeSurrounds {
134 target: Option<Object>,
135}
136
137#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
138#[action(namespace = vim)]
139#[serde(deny_unknown_fields)]
140struct PushJump {
141 line: bool,
142}
143
144#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
145#[action(namespace = vim)]
146#[serde(deny_unknown_fields)]
147struct PushDigraph {
148 first_char: Option<char>,
149}
150
151#[derive(Clone, Deserialize, JsonSchema, PartialEq, Action)]
152#[action(namespace = vim)]
153#[serde(deny_unknown_fields)]
154struct PushLiteral {
155 prefix: Option<String>,
156}
157
158actions!(
159 vim,
160 [
161 /// Switches to normal mode.
162 SwitchToNormalMode,
163 /// Switches to insert mode.
164 SwitchToInsertMode,
165 /// Switches to replace mode.
166 SwitchToReplaceMode,
167 /// Switches to visual mode.
168 SwitchToVisualMode,
169 /// Switches to visual line mode.
170 SwitchToVisualLineMode,
171 /// Switches to visual block mode.
172 SwitchToVisualBlockMode,
173 /// Switches to Helix-style normal mode.
174 SwitchToHelixNormalMode,
175 /// Clears any pending operators.
176 ClearOperators,
177 /// Clears the exchange register.
178 ClearExchange,
179 /// Inserts a tab character.
180 Tab,
181 /// Inserts a newline.
182 Enter,
183 /// Selects inner text object.
184 InnerObject,
185 /// Maximizes the current pane.
186 MaximizePane,
187 /// Resets all pane sizes to default.
188 ResetPaneSizes,
189 /// Resizes the pane to the right.
190 ResizePaneRight,
191 /// Resizes the pane to the left.
192 ResizePaneLeft,
193 /// Resizes the pane upward.
194 ResizePaneUp,
195 /// Resizes the pane downward.
196 ResizePaneDown,
197 /// Starts a change operation.
198 PushChange,
199 /// Starts a delete operation.
200 PushDelete,
201 /// Exchanges text regions.
202 Exchange,
203 /// Starts a yank operation.
204 PushYank,
205 /// Starts a replace operation.
206 PushReplace,
207 /// Deletes surrounding characters.
208 PushDeleteSurrounds,
209 /// Sets a mark at the current position.
210 PushMark,
211 /// Toggles the marks view.
212 ToggleMarksView,
213 /// Starts a forced motion.
214 PushForcedMotion,
215 /// Starts an indent operation.
216 PushIndent,
217 /// Starts an outdent operation.
218 PushOutdent,
219 /// Starts an auto-indent operation.
220 PushAutoIndent,
221 /// Starts a rewrap operation.
222 PushRewrap,
223 /// Starts a shell command operation.
224 PushShellCommand,
225 /// Converts to lowercase.
226 PushLowercase,
227 /// Converts to uppercase.
228 PushUppercase,
229 /// Toggles case.
230 PushOppositeCase,
231 /// Applies ROT13 encoding.
232 PushRot13,
233 /// Applies ROT47 encoding.
234 PushRot47,
235 /// Toggles the registers view.
236 ToggleRegistersView,
237 /// Selects a register.
238 PushRegister,
239 /// Starts recording to a register.
240 PushRecordRegister,
241 /// Replays a register.
242 PushReplayRegister,
243 /// Replaces with register contents.
244 PushReplaceWithRegister,
245 /// Toggles comments.
246 PushToggleComments,
247 /// Selects (count) next menu item
248 MenuSelectNext,
249 /// Selects (count) previous menu item
250 MenuSelectPrevious,
251 /// Clears count or toggles project panel focus
252 ToggleProjectPanelFocus,
253 /// Starts a match operation.
254 PushHelixMatch,
255 ]
256);
257
258// in the workspace namespace so it's not filtered out when vim is disabled.
259actions!(
260 workspace,
261 [
262 /// Toggles Vim mode on or off.
263 ToggleVimMode,
264 /// Toggles Helix mode on or off.
265 ToggleHelixMode,
266 ]
267);
268
269/// Initializes the `vim` crate.
270pub fn init(cx: &mut App) {
271 VimGlobals::register(cx);
272
273 cx.observe_new(Vim::register).detach();
274
275 cx.observe_new(|workspace: &mut Workspace, _, _| {
276 workspace.register_action(|workspace, _: &ToggleVimMode, _, cx| {
277 let fs = workspace.app_state().fs.clone();
278 let currently_enabled = VimModeSetting::get_global(cx).0;
279 update_settings_file(fs, cx, move |setting, _| {
280 setting.vim_mode = Some(!currently_enabled);
281 if let Some(helix_mode) = &mut setting.helix_mode {
282 *helix_mode = false;
283 }
284 })
285 });
286
287 workspace.register_action(|workspace, _: &ToggleHelixMode, _, cx| {
288 let fs = workspace.app_state().fs.clone();
289 let currently_enabled = HelixModeSetting::get_global(cx).0;
290 update_settings_file(fs, cx, move |setting, _| {
291 setting.helix_mode = Some(!currently_enabled);
292 if let Some(vim_mode) = &mut setting.vim_mode {
293 *vim_mode = false;
294 }
295 })
296 });
297
298 workspace.register_action(|_, _: &MenuSelectNext, window, cx| {
299 let count = Vim::take_count(cx).unwrap_or(1);
300
301 for _ in 0..count {
302 window.dispatch_action(menu::SelectNext.boxed_clone(), cx);
303 }
304 });
305
306 workspace.register_action(|_, _: &MenuSelectPrevious, window, cx| {
307 let count = Vim::take_count(cx).unwrap_or(1);
308
309 for _ in 0..count {
310 window.dispatch_action(menu::SelectPrevious.boxed_clone(), cx);
311 }
312 });
313
314 workspace.register_action(|_, _: &ToggleProjectPanelFocus, window, cx| {
315 if Vim::take_count(cx).is_none() {
316 window.dispatch_action(zed_actions::project_panel::ToggleFocus.boxed_clone(), cx);
317 }
318 });
319
320 workspace.register_action(|workspace, n: &Number, window, cx| {
321 let vim = workspace
322 .focused_pane(window, cx)
323 .read(cx)
324 .active_item()
325 .and_then(|item| item.act_as::<Editor>(cx))
326 .and_then(|editor| editor.read(cx).addon::<VimAddon>().cloned());
327 if let Some(vim) = vim {
328 let digit = n.0;
329 vim.entity.update(cx, |_, cx| {
330 cx.defer_in(window, move |vim, window, cx| {
331 vim.push_count_digit(digit, window, cx)
332 })
333 });
334 } else {
335 let count = Vim::globals(cx).pre_count.unwrap_or(0);
336 Vim::globals(cx).pre_count = Some(
337 count
338 .checked_mul(10)
339 .and_then(|c| c.checked_add(n.0))
340 .unwrap_or(count),
341 );
342 };
343 });
344
345 workspace.register_action(|_, _: &zed_actions::vim::OpenDefaultKeymap, _, cx| {
346 cx.emit(workspace::Event::OpenBundledFile {
347 text: settings::vim_keymap(),
348 title: "Default Vim Bindings",
349 language: "JSON",
350 });
351 });
352
353 workspace.register_action(|workspace, _: &ResetPaneSizes, _, cx| {
354 workspace.reset_pane_sizes(cx);
355 });
356
357 workspace.register_action(|workspace, _: &MaximizePane, window, cx| {
358 let pane = workspace.active_pane();
359 let Some(size) = workspace.bounding_box_for_pane(pane) else {
360 return;
361 };
362
363 let theme = ThemeSettings::get_global(cx);
364 let height = theme.buffer_font_size(cx) * theme.buffer_line_height.value();
365
366 let desired_size = if let Some(count) = Vim::take_count(cx) {
367 height * count
368 } else {
369 px(10000.)
370 };
371 workspace.resize_pane(Axis::Vertical, desired_size - size.size.height, window, cx)
372 });
373
374 workspace.register_action(|workspace, _: &ResizePaneRight, window, cx| {
375 let count = Vim::take_count(cx).unwrap_or(1) as f32;
376 Vim::take_forced_motion(cx);
377 let theme = ThemeSettings::get_global(cx);
378 let font_id = window.text_system().resolve_font(&theme.buffer_font);
379 let Ok(width) = window
380 .text_system()
381 .advance(font_id, theme.buffer_font_size(cx), 'm')
382 else {
383 return;
384 };
385 workspace.resize_pane(Axis::Horizontal, width.width * count, window, cx);
386 });
387
388 workspace.register_action(|workspace, _: &ResizePaneLeft, window, cx| {
389 let count = Vim::take_count(cx).unwrap_or(1) as f32;
390 Vim::take_forced_motion(cx);
391 let theme = ThemeSettings::get_global(cx);
392 let font_id = window.text_system().resolve_font(&theme.buffer_font);
393 let Ok(width) = window
394 .text_system()
395 .advance(font_id, theme.buffer_font_size(cx), 'm')
396 else {
397 return;
398 };
399 workspace.resize_pane(Axis::Horizontal, -width.width * count, window, cx);
400 });
401
402 workspace.register_action(|workspace, _: &ResizePaneUp, window, cx| {
403 let count = Vim::take_count(cx).unwrap_or(1) as f32;
404 Vim::take_forced_motion(cx);
405 let theme = ThemeSettings::get_global(cx);
406 let height = theme.buffer_font_size(cx) * theme.buffer_line_height.value();
407 workspace.resize_pane(Axis::Vertical, height * count, window, cx);
408 });
409
410 workspace.register_action(|workspace, _: &ResizePaneDown, window, cx| {
411 let count = Vim::take_count(cx).unwrap_or(1) as f32;
412 Vim::take_forced_motion(cx);
413 let theme = ThemeSettings::get_global(cx);
414 let height = theme.buffer_font_size(cx) * theme.buffer_line_height.value();
415 workspace.resize_pane(Axis::Vertical, -height * count, window, cx);
416 });
417
418 workspace.register_action(|workspace, _: &SearchSubmit, window, cx| {
419 let vim = workspace
420 .focused_pane(window, cx)
421 .read(cx)
422 .active_item()
423 .and_then(|item| item.act_as::<Editor>(cx))
424 .and_then(|editor| editor.read(cx).addon::<VimAddon>().cloned());
425 let Some(vim) = vim else { return };
426 vim.entity.update(cx, |_, cx| {
427 cx.defer_in(window, |vim, window, cx| vim.search_submit(window, cx))
428 })
429 });
430 workspace.register_action(|_, _: &GoToTab, window, cx| {
431 let count = Vim::take_count(cx);
432 Vim::take_forced_motion(cx);
433
434 if let Some(tab_index) = count {
435 // <count>gt goes to tab <count> (1-based).
436 let zero_based_index = tab_index.saturating_sub(1);
437 window.dispatch_action(
438 workspace::pane::ActivateItem(zero_based_index).boxed_clone(),
439 cx,
440 );
441 } else {
442 // If no count is provided, go to the next tab.
443 window.dispatch_action(workspace::pane::ActivateNextItem.boxed_clone(), cx);
444 }
445 });
446
447 workspace.register_action(|workspace, _: &GoToPreviousTab, window, cx| {
448 let count = Vim::take_count(cx);
449 Vim::take_forced_motion(cx);
450
451 if let Some(count) = count {
452 // gT with count goes back that many tabs with wraparound (not the same as gt!).
453 let pane = workspace.active_pane().read(cx);
454 let item_count = pane.items().count();
455 if item_count > 0 {
456 let current_index = pane.active_item_index();
457 let target_index = (current_index as isize - count as isize)
458 .rem_euclid(item_count as isize)
459 as usize;
460 window.dispatch_action(
461 workspace::pane::ActivateItem(target_index).boxed_clone(),
462 cx,
463 );
464 }
465 } else {
466 // No count provided, go to the previous tab.
467 window.dispatch_action(workspace::pane::ActivatePreviousItem.boxed_clone(), cx);
468 }
469 });
470 })
471 .detach();
472}
473
474#[derive(Clone)]
475pub(crate) struct VimAddon {
476 pub(crate) entity: Entity<Vim>,
477}
478
479impl editor::Addon for VimAddon {
480 fn extend_key_context(&self, key_context: &mut KeyContext, cx: &App) {
481 self.entity.read(cx).extend_key_context(key_context, cx)
482 }
483
484 fn to_any(&self) -> &dyn std::any::Any {
485 self
486 }
487}
488
489/// The state pertaining to Vim mode.
490pub(crate) struct Vim {
491 pub(crate) mode: Mode,
492 pub last_mode: Mode,
493 pub temp_mode: bool,
494 pub status_label: Option<SharedString>,
495 pub exit_temporary_mode: bool,
496
497 operator_stack: Vec<Operator>,
498 pub(crate) replacements: Vec<(Range<editor::Anchor>, String)>,
499
500 pub(crate) stored_visual_mode: Option<(Mode, Vec<bool>)>,
501
502 pub(crate) current_tx: Option<TransactionId>,
503 pub(crate) current_anchor: Option<Selection<Anchor>>,
504 pub(crate) undo_modes: HashMap<TransactionId, Mode>,
505 pub(crate) undo_last_line_tx: Option<TransactionId>,
506
507 selected_register: Option<char>,
508 pub search: SearchState,
509
510 editor: WeakEntity<Editor>,
511
512 last_command: Option<String>,
513 running_command: Option<Task<()>>,
514 _subscriptions: Vec<Subscription>,
515}
516
517// Hack: Vim intercepts events dispatched to a window and updates the view in response.
518// This means it needs a VisualContext. The easiest way to satisfy that constraint is
519// to make Vim a "View" that is just never actually rendered.
520impl Render for Vim {
521 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
522 gpui::Empty
523 }
524}
525
526enum VimEvent {
527 Focused,
528}
529impl EventEmitter<VimEvent> for Vim {}
530
531impl Vim {
532 /// The namespace for Vim actions.
533 const NAMESPACE: &'static str = "vim";
534
535 pub fn new(window: &mut Window, cx: &mut Context<Editor>) -> Entity<Self> {
536 let editor = cx.entity();
537
538 let initial_vim_mode = VimSettings::get_global(cx).default_mode;
539 let (mode, last_mode) = if HelixModeSetting::get_global(cx).0 {
540 let initial_helix_mode = match initial_vim_mode {
541 Mode::Normal => Mode::HelixNormal,
542 Mode::Insert => Mode::Insert,
543 // Otherwise, we panic with a note that we should never get there due to the
544 // possible values of VimSettings::get_global(cx).default_mode being either Mode::Normal or Mode::Insert.
545 _ => unreachable!("Invalid default mode"),
546 };
547 (initial_helix_mode, Mode::HelixNormal)
548 } else {
549 (initial_vim_mode, Mode::Normal)
550 };
551
552 cx.new(|cx| Vim {
553 mode,
554 last_mode,
555 temp_mode: false,
556 exit_temporary_mode: false,
557 operator_stack: Vec::new(),
558 replacements: Vec::new(),
559
560 stored_visual_mode: None,
561 current_tx: None,
562 undo_last_line_tx: None,
563 current_anchor: None,
564 undo_modes: HashMap::default(),
565
566 status_label: None,
567 selected_register: None,
568 search: SearchState::default(),
569
570 last_command: None,
571 running_command: None,
572
573 editor: editor.downgrade(),
574 _subscriptions: vec![
575 cx.observe_keystrokes(Self::observe_keystrokes),
576 cx.subscribe_in(&editor, window, |this, _, event, window, cx| {
577 this.handle_editor_event(event, window, cx)
578 }),
579 ],
580 })
581 }
582
583 fn register(editor: &mut Editor, window: Option<&mut Window>, cx: &mut Context<Editor>) {
584 let Some(window) = window else {
585 return;
586 };
587
588 if !editor.use_modal_editing() {
589 return;
590 }
591
592 let mut was_enabled = Vim::enabled(cx);
593 let mut was_toggle = VimSettings::get_global(cx).toggle_relative_line_numbers;
594 cx.observe_global_in::<SettingsStore>(window, move |editor, window, cx| {
595 let enabled = Vim::enabled(cx);
596 let toggle = VimSettings::get_global(cx).toggle_relative_line_numbers;
597 if enabled && was_enabled && (toggle != was_toggle) {
598 if toggle {
599 let is_relative = editor
600 .addon::<VimAddon>()
601 .map(|vim| vim.entity.read(cx).mode != Mode::Insert);
602 editor.set_relative_line_number(is_relative, cx)
603 } else {
604 editor.set_relative_line_number(None, cx)
605 }
606 }
607 was_toggle = VimSettings::get_global(cx).toggle_relative_line_numbers;
608 if was_enabled == enabled {
609 return;
610 }
611 was_enabled = enabled;
612 if enabled {
613 Self::activate(editor, window, cx)
614 } else {
615 Self::deactivate(editor, cx)
616 }
617 })
618 .detach();
619 if was_enabled {
620 Self::activate(editor, window, cx)
621 }
622 }
623
624 fn activate(editor: &mut Editor, window: &mut Window, cx: &mut Context<Editor>) {
625 let vim = Vim::new(window, cx);
626
627 if !editor.mode().is_full() {
628 vim.update(cx, |vim, _| {
629 vim.mode = Mode::Insert;
630 });
631 }
632
633 editor.register_addon(VimAddon {
634 entity: vim.clone(),
635 });
636
637 vim.update(cx, |_, cx| {
638 Vim::action(editor, cx, |vim, _: &SwitchToNormalMode, window, cx| {
639 vim.switch_mode(Mode::Normal, false, window, cx)
640 });
641
642 Vim::action(editor, cx, |vim, _: &SwitchToInsertMode, window, cx| {
643 vim.switch_mode(Mode::Insert, false, window, cx)
644 });
645
646 Vim::action(editor, cx, |vim, _: &SwitchToReplaceMode, window, cx| {
647 vim.switch_mode(Mode::Replace, false, window, cx)
648 });
649
650 Vim::action(editor, cx, |vim, _: &SwitchToVisualMode, window, cx| {
651 vim.switch_mode(Mode::Visual, false, window, cx)
652 });
653
654 Vim::action(editor, cx, |vim, _: &SwitchToVisualLineMode, window, cx| {
655 vim.switch_mode(Mode::VisualLine, false, window, cx)
656 });
657
658 Vim::action(
659 editor,
660 cx,
661 |vim, _: &SwitchToVisualBlockMode, window, cx| {
662 vim.switch_mode(Mode::VisualBlock, false, window, cx)
663 },
664 );
665
666 Vim::action(
667 editor,
668 cx,
669 |vim, _: &SwitchToHelixNormalMode, window, cx| {
670 vim.switch_mode(Mode::HelixNormal, true, window, cx)
671 },
672 );
673 Vim::action(editor, cx, |_, _: &PushForcedMotion, _, cx| {
674 Vim::globals(cx).forced_motion = true;
675 });
676 Vim::action(editor, cx, |vim, action: &PushObject, window, cx| {
677 vim.push_operator(
678 Operator::Object {
679 around: action.around,
680 },
681 window,
682 cx,
683 )
684 });
685
686 Vim::action(editor, cx, |vim, action: &PushFindForward, window, cx| {
687 vim.push_operator(
688 Operator::FindForward {
689 before: action.before,
690 multiline: action.multiline,
691 },
692 window,
693 cx,
694 )
695 });
696
697 Vim::action(editor, cx, |vim, action: &PushFindBackward, window, cx| {
698 vim.push_operator(
699 Operator::FindBackward {
700 after: action.after,
701 multiline: action.multiline,
702 },
703 window,
704 cx,
705 )
706 });
707
708 Vim::action(editor, cx, |vim, action: &PushSneak, window, cx| {
709 vim.push_operator(
710 Operator::Sneak {
711 first_char: action.first_char,
712 },
713 window,
714 cx,
715 )
716 });
717
718 Vim::action(editor, cx, |vim, action: &PushSneakBackward, window, cx| {
719 vim.push_operator(
720 Operator::SneakBackward {
721 first_char: action.first_char,
722 },
723 window,
724 cx,
725 )
726 });
727
728 Vim::action(editor, cx, |vim, _: &PushAddSurrounds, window, cx| {
729 vim.push_operator(Operator::AddSurrounds { target: None }, window, cx)
730 });
731
732 Vim::action(
733 editor,
734 cx,
735 |vim, action: &PushChangeSurrounds, window, cx| {
736 vim.push_operator(
737 Operator::ChangeSurrounds {
738 target: action.target,
739 opening: false,
740 },
741 window,
742 cx,
743 )
744 },
745 );
746
747 Vim::action(editor, cx, |vim, action: &PushJump, window, cx| {
748 vim.push_operator(Operator::Jump { line: action.line }, window, cx)
749 });
750
751 Vim::action(editor, cx, |vim, action: &PushDigraph, window, cx| {
752 vim.push_operator(
753 Operator::Digraph {
754 first_char: action.first_char,
755 },
756 window,
757 cx,
758 )
759 });
760
761 Vim::action(editor, cx, |vim, action: &PushLiteral, window, cx| {
762 vim.push_operator(
763 Operator::Literal {
764 prefix: action.prefix.clone(),
765 },
766 window,
767 cx,
768 )
769 });
770
771 Vim::action(editor, cx, |vim, _: &PushChange, window, cx| {
772 vim.push_operator(Operator::Change, window, cx)
773 });
774
775 Vim::action(editor, cx, |vim, _: &PushDelete, window, cx| {
776 vim.push_operator(Operator::Delete, window, cx)
777 });
778
779 Vim::action(editor, cx, |vim, _: &PushYank, window, cx| {
780 vim.push_operator(Operator::Yank, window, cx)
781 });
782
783 Vim::action(editor, cx, |vim, _: &PushReplace, window, cx| {
784 vim.push_operator(Operator::Replace, window, cx)
785 });
786
787 Vim::action(editor, cx, |vim, _: &PushDeleteSurrounds, window, cx| {
788 vim.push_operator(Operator::DeleteSurrounds, window, cx)
789 });
790
791 Vim::action(editor, cx, |vim, _: &PushMark, window, cx| {
792 vim.push_operator(Operator::Mark, window, cx)
793 });
794
795 Vim::action(editor, cx, |vim, _: &PushIndent, window, cx| {
796 vim.push_operator(Operator::Indent, window, cx)
797 });
798
799 Vim::action(editor, cx, |vim, _: &PushOutdent, window, cx| {
800 vim.push_operator(Operator::Outdent, window, cx)
801 });
802
803 Vim::action(editor, cx, |vim, _: &PushAutoIndent, window, cx| {
804 vim.push_operator(Operator::AutoIndent, window, cx)
805 });
806
807 Vim::action(editor, cx, |vim, _: &PushRewrap, window, cx| {
808 vim.push_operator(Operator::Rewrap, window, cx)
809 });
810
811 Vim::action(editor, cx, |vim, _: &PushShellCommand, window, cx| {
812 vim.push_operator(Operator::ShellCommand, window, cx)
813 });
814
815 Vim::action(editor, cx, |vim, _: &PushLowercase, window, cx| {
816 vim.push_operator(Operator::Lowercase, window, cx)
817 });
818
819 Vim::action(editor, cx, |vim, _: &PushUppercase, window, cx| {
820 vim.push_operator(Operator::Uppercase, window, cx)
821 });
822
823 Vim::action(editor, cx, |vim, _: &PushOppositeCase, window, cx| {
824 vim.push_operator(Operator::OppositeCase, window, cx)
825 });
826
827 Vim::action(editor, cx, |vim, _: &PushRot13, window, cx| {
828 vim.push_operator(Operator::Rot13, window, cx)
829 });
830
831 Vim::action(editor, cx, |vim, _: &PushRot47, window, cx| {
832 vim.push_operator(Operator::Rot47, window, cx)
833 });
834
835 Vim::action(editor, cx, |vim, _: &PushRegister, window, cx| {
836 vim.push_operator(Operator::Register, window, cx)
837 });
838
839 Vim::action(editor, cx, |vim, _: &PushRecordRegister, window, cx| {
840 vim.push_operator(Operator::RecordRegister, window, cx)
841 });
842
843 Vim::action(editor, cx, |vim, _: &PushReplayRegister, window, cx| {
844 vim.push_operator(Operator::ReplayRegister, window, cx)
845 });
846
847 Vim::action(
848 editor,
849 cx,
850 |vim, _: &PushReplaceWithRegister, window, cx| {
851 vim.push_operator(Operator::ReplaceWithRegister, window, cx)
852 },
853 );
854
855 Vim::action(editor, cx, |vim, _: &Exchange, window, cx| {
856 if vim.mode.is_visual() {
857 vim.exchange_visual(window, cx)
858 } else {
859 vim.push_operator(Operator::Exchange, window, cx)
860 }
861 });
862
863 Vim::action(editor, cx, |vim, _: &ClearExchange, window, cx| {
864 vim.clear_exchange(window, cx)
865 });
866
867 Vim::action(editor, cx, |vim, _: &PushToggleComments, window, cx| {
868 vim.push_operator(Operator::ToggleComments, window, cx)
869 });
870
871 Vim::action(editor, cx, |vim, _: &ClearOperators, window, cx| {
872 vim.clear_operator(window, cx)
873 });
874 Vim::action(editor, cx, |vim, n: &Number, window, cx| {
875 vim.push_count_digit(n.0, window, cx);
876 });
877 Vim::action(editor, cx, |vim, _: &Tab, window, cx| {
878 vim.input_ignored(" ".into(), window, cx)
879 });
880 Vim::action(
881 editor,
882 cx,
883 |vim, action: &editor::actions::AcceptEditPrediction, window, cx| {
884 vim.update_editor(cx, |_, editor, cx| {
885 editor.accept_edit_prediction(action, window, cx);
886 });
887 // In non-insertion modes, predictions will be hidden and instead a jump will be
888 // displayed (and performed by `accept_edit_prediction`). This switches to
889 // insert mode so that the prediction is displayed after the jump.
890 match vim.mode {
891 Mode::Replace => {}
892 _ => vim.switch_mode(Mode::Insert, true, window, cx),
893 };
894 },
895 );
896 Vim::action(editor, cx, |vim, _: &Enter, window, cx| {
897 vim.input_ignored("\n".into(), window, cx)
898 });
899 Vim::action(editor, cx, |vim, _: &PushHelixMatch, window, cx| {
900 vim.push_operator(Operator::HelixMatch, window, cx)
901 });
902 Vim::action(editor, cx, |vim, action: &PushHelixNext, window, cx| {
903 vim.push_operator(
904 Operator::HelixNext {
905 around: action.around,
906 },
907 window,
908 cx,
909 );
910 });
911 Vim::action(editor, cx, |vim, action: &PushHelixPrevious, window, cx| {
912 vim.push_operator(
913 Operator::HelixPrevious {
914 around: action.around,
915 },
916 window,
917 cx,
918 );
919 });
920
921 Vim::action(
922 editor,
923 cx,
924 |vim, _: &editor::actions::Paste, window, cx| match vim.mode {
925 Mode::Replace => vim.paste_replace(window, cx),
926 Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
927 vim.selected_register.replace('+');
928 vim.paste(&VimPaste::default(), window, cx);
929 }
930 _ => {
931 vim.update_editor(cx, |_, editor, cx| editor.paste(&Paste, window, cx));
932 }
933 },
934 );
935
936 normal::register(editor, cx);
937 insert::register(editor, cx);
938 helix::register(editor, cx);
939 motion::register(editor, cx);
940 command::register(editor, cx);
941 replace::register(editor, cx);
942 indent::register(editor, cx);
943 rewrap::register(editor, cx);
944 object::register(editor, cx);
945 visual::register(editor, cx);
946 change_list::register(editor, cx);
947 digraph::register(editor, cx);
948
949 if editor.is_focused(window) {
950 cx.defer_in(window, |vim, window, cx| {
951 vim.focused(false, window, cx);
952 })
953 }
954 })
955 }
956
957 fn deactivate(editor: &mut Editor, cx: &mut Context<Editor>) {
958 editor.set_cursor_shape(
959 EditorSettings::get_global(cx)
960 .cursor_shape
961 .unwrap_or_default(),
962 cx,
963 );
964 editor.set_clip_at_line_ends(false, cx);
965 editor.set_collapse_matches(false);
966 editor.set_input_enabled(true);
967 editor.set_autoindent(true);
968 editor.selections.set_line_mode(false);
969 editor.unregister_addon::<VimAddon>();
970 editor.set_relative_line_number(None, cx);
971 if let Some(vim) = Vim::globals(cx).focused_vim()
972 && vim.entity_id() == cx.entity().entity_id()
973 {
974 Vim::globals(cx).focused_vim = None;
975 }
976 }
977
978 /// Register an action on the editor.
979 pub fn action<A: Action>(
980 editor: &mut Editor,
981 cx: &mut Context<Vim>,
982 f: impl Fn(&mut Vim, &A, &mut Window, &mut Context<Vim>) + 'static,
983 ) {
984 let subscription = editor.register_action(cx.listener(f));
985 cx.on_release(|_, _| drop(subscription)).detach();
986 }
987
988 pub fn editor(&self) -> Option<Entity<Editor>> {
989 self.editor.upgrade()
990 }
991
992 pub fn workspace(&self, window: &mut Window) -> Option<Entity<Workspace>> {
993 window.root::<Workspace>().flatten()
994 }
995
996 pub fn pane(&self, window: &mut Window, cx: &mut Context<Self>) -> Option<Entity<Pane>> {
997 self.workspace(window)
998 .map(|workspace| workspace.read(cx).focused_pane(window, cx))
999 }
1000
1001 pub fn enabled(cx: &mut App) -> bool {
1002 VimModeSetting::get_global(cx).0 || HelixModeSetting::get_global(cx).0
1003 }
1004
1005 /// Called whenever an keystroke is typed so vim can observe all actions
1006 /// and keystrokes accordingly.
1007 fn observe_keystrokes(
1008 &mut self,
1009 keystroke_event: &KeystrokeEvent,
1010 window: &mut Window,
1011 cx: &mut Context<Self>,
1012 ) {
1013 if self.exit_temporary_mode {
1014 self.exit_temporary_mode = false;
1015 // Don't switch to insert mode if the action is temporary_normal.
1016 if let Some(action) = keystroke_event.action.as_ref()
1017 && action.as_any().downcast_ref::<TemporaryNormal>().is_some()
1018 {
1019 return;
1020 }
1021 self.switch_mode(Mode::Insert, false, window, cx)
1022 }
1023 if let Some(action) = keystroke_event.action.as_ref() {
1024 // Keystroke is handled by the vim system, so continue forward
1025 if action.name().starts_with("vim::") {
1026 self.update_editor(cx, |_, editor, cx| {
1027 editor.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx)
1028 });
1029
1030 return;
1031 }
1032 } else if window.has_pending_keystrokes() || keystroke_event.keystroke.is_ime_in_progress()
1033 {
1034 return;
1035 }
1036
1037 if let Some(operator) = self.active_operator() {
1038 match operator {
1039 Operator::Literal { prefix } => {
1040 self.handle_literal_keystroke(
1041 keystroke_event,
1042 prefix.unwrap_or_default(),
1043 window,
1044 cx,
1045 );
1046 }
1047 _ if !operator.is_waiting(self.mode) => {
1048 self.clear_operator(window, cx);
1049 self.stop_recording_immediately(Box::new(ClearOperators), cx)
1050 }
1051 _ => {}
1052 }
1053 }
1054 }
1055
1056 fn handle_editor_event(
1057 &mut self,
1058 event: &EditorEvent,
1059 window: &mut Window,
1060 cx: &mut Context<Self>,
1061 ) {
1062 match event {
1063 EditorEvent::Focused => self.focused(true, window, cx),
1064 EditorEvent::Blurred => self.blurred(window, cx),
1065 EditorEvent::SelectionsChanged { local: true } => {
1066 self.local_selections_changed(window, cx);
1067 }
1068 EditorEvent::InputIgnored { text } => {
1069 self.input_ignored(text.clone(), window, cx);
1070 Vim::globals(cx).observe_insertion(text, None)
1071 }
1072 EditorEvent::InputHandled {
1073 text,
1074 utf16_range_to_replace: range_to_replace,
1075 } => Vim::globals(cx).observe_insertion(text, range_to_replace.clone()),
1076 EditorEvent::TransactionBegun { transaction_id } => {
1077 self.transaction_begun(*transaction_id, window, cx)
1078 }
1079 EditorEvent::TransactionUndone { transaction_id } => {
1080 self.transaction_undone(transaction_id, window, cx)
1081 }
1082 EditorEvent::Edited { .. } => self.push_to_change_list(window, cx),
1083 EditorEvent::FocusedIn => self.sync_vim_settings(window, cx),
1084 EditorEvent::CursorShapeChanged => self.cursor_shape_changed(window, cx),
1085 EditorEvent::PushedToNavHistory {
1086 anchor,
1087 is_deactivate,
1088 } => {
1089 self.update_editor(cx, |vim, editor, cx| {
1090 let mark = if *is_deactivate {
1091 "\"".to_string()
1092 } else {
1093 "'".to_string()
1094 };
1095 vim.set_mark(mark, vec![*anchor], editor.buffer(), window, cx);
1096 });
1097 }
1098 _ => {}
1099 }
1100 }
1101
1102 fn push_operator(&mut self, operator: Operator, window: &mut Window, cx: &mut Context<Self>) {
1103 if operator.starts_dot_recording() {
1104 self.start_recording(cx);
1105 }
1106 // Since these operations can only be entered with pre-operators,
1107 // we need to clear the previous operators when pushing,
1108 // so that the current stack is the most correct
1109 if matches!(
1110 operator,
1111 Operator::AddSurrounds { .. }
1112 | Operator::ChangeSurrounds { .. }
1113 | Operator::DeleteSurrounds
1114 | Operator::Exchange
1115 ) {
1116 self.operator_stack.clear();
1117 };
1118 self.operator_stack.push(operator);
1119 self.sync_vim_settings(window, cx);
1120 }
1121
1122 pub fn switch_mode(
1123 &mut self,
1124 mode: Mode,
1125 leave_selections: bool,
1126 window: &mut Window,
1127 cx: &mut Context<Self>,
1128 ) {
1129 if self.temp_mode && mode == Mode::Normal {
1130 self.temp_mode = false;
1131 self.switch_mode(Mode::Normal, leave_selections, window, cx);
1132 self.switch_mode(Mode::Insert, false, window, cx);
1133 return;
1134 } else if self.temp_mode
1135 && !matches!(mode, Mode::Visual | Mode::VisualLine | Mode::VisualBlock)
1136 {
1137 self.temp_mode = false;
1138 }
1139
1140 let last_mode = self.mode;
1141 let prior_mode = self.last_mode;
1142 let prior_tx = self.current_tx;
1143 self.status_label.take();
1144 self.last_mode = last_mode;
1145 self.mode = mode;
1146 self.operator_stack.clear();
1147 self.selected_register.take();
1148 self.cancel_running_command(window, cx);
1149 if mode == Mode::Normal || mode != last_mode {
1150 self.current_tx.take();
1151 self.current_anchor.take();
1152 self.update_editor(cx, |_, editor, _| {
1153 editor.clear_selection_drag_state();
1154 });
1155 }
1156 Vim::take_forced_motion(cx);
1157 if mode != Mode::Insert && mode != Mode::Replace {
1158 Vim::take_count(cx);
1159 }
1160
1161 // Sync editor settings like clip mode
1162 self.sync_vim_settings(window, cx);
1163
1164 if VimSettings::get_global(cx).toggle_relative_line_numbers
1165 && self.mode != self.last_mode
1166 && (self.mode == Mode::Insert || self.last_mode == Mode::Insert)
1167 {
1168 self.update_editor(cx, |vim, editor, cx| {
1169 let is_relative = vim.mode != Mode::Insert;
1170 editor.set_relative_line_number(Some(is_relative), cx)
1171 });
1172 }
1173 if HelixModeSetting::get_global(cx).0 {
1174 if self.mode == Mode::Normal {
1175 self.mode = Mode::HelixNormal
1176 } else if self.mode == Mode::Visual {
1177 self.mode = Mode::HelixSelect
1178 }
1179 }
1180
1181 if leave_selections {
1182 return;
1183 }
1184
1185 if !mode.is_visual() && last_mode.is_visual() {
1186 self.create_visual_marks(last_mode, window, cx);
1187 }
1188
1189 // Adjust selections
1190 self.update_editor(cx, |vim, editor, cx| {
1191 if last_mode != Mode::VisualBlock && last_mode.is_visual() && mode == Mode::VisualBlock
1192 {
1193 vim.visual_block_motion(true, editor, window, cx, |_, point, goal| {
1194 Some((point, goal))
1195 })
1196 }
1197 if (last_mode == Mode::Insert || last_mode == Mode::Replace)
1198 && let Some(prior_tx) = prior_tx
1199 {
1200 editor.group_until_transaction(prior_tx, cx)
1201 }
1202
1203 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1204 // we cheat with visual block mode and use multiple cursors.
1205 // the cost of this cheat is we need to convert back to a single
1206 // cursor whenever vim would.
1207 if last_mode == Mode::VisualBlock
1208 && (mode != Mode::VisualBlock && mode != Mode::Insert)
1209 {
1210 let tail = s.oldest_anchor().tail();
1211 let head = s.newest_anchor().head();
1212 s.select_anchor_ranges(vec![tail..head]);
1213 } else if last_mode == Mode::Insert
1214 && prior_mode == Mode::VisualBlock
1215 && mode != Mode::VisualBlock
1216 {
1217 let pos = s.first_anchor().head();
1218 s.select_anchor_ranges(vec![pos..pos])
1219 }
1220
1221 let snapshot = s.display_snapshot();
1222 if let Some(pending) = s.pending_anchor_mut()
1223 && pending.reversed
1224 && mode.is_visual()
1225 && !last_mode.is_visual()
1226 {
1227 let mut end = pending.end.to_point(&snapshot.buffer_snapshot());
1228 end = snapshot
1229 .buffer_snapshot()
1230 .clip_point(end + Point::new(0, 1), Bias::Right);
1231 pending.end = snapshot.buffer_snapshot().anchor_before(end);
1232 }
1233
1234 s.move_with(|map, selection| {
1235 if last_mode.is_visual() && !mode.is_visual() {
1236 let mut point = selection.head();
1237 if !selection.reversed && !selection.is_empty() {
1238 point = movement::left(map, selection.head());
1239 } else if selection.is_empty() {
1240 point = map.clip_point(point, Bias::Left);
1241 }
1242 selection.collapse_to(point, selection.goal)
1243 } else if !last_mode.is_visual() && mode.is_visual() && selection.is_empty() {
1244 selection.end = movement::right(map, selection.start);
1245 }
1246 });
1247 })
1248 });
1249 }
1250
1251 pub fn take_count(cx: &mut App) -> Option<usize> {
1252 let global_state = cx.global_mut::<VimGlobals>();
1253 if global_state.dot_replaying {
1254 return global_state.recorded_count;
1255 }
1256
1257 let count = if global_state.post_count.is_none() && global_state.pre_count.is_none() {
1258 return None;
1259 } else {
1260 Some(
1261 global_state.post_count.take().unwrap_or(1)
1262 * global_state.pre_count.take().unwrap_or(1),
1263 )
1264 };
1265
1266 if global_state.dot_recording {
1267 global_state.recording_count = count;
1268 }
1269 count
1270 }
1271
1272 pub fn take_forced_motion(cx: &mut App) -> bool {
1273 let global_state = cx.global_mut::<VimGlobals>();
1274 let forced_motion = global_state.forced_motion;
1275 global_state.forced_motion = false;
1276 forced_motion
1277 }
1278
1279 pub fn cursor_shape(&self, cx: &mut App) -> CursorShape {
1280 let cursor_shape = VimSettings::get_global(cx).cursor_shape;
1281 match self.mode {
1282 Mode::Normal => {
1283 if let Some(operator) = self.operator_stack.last() {
1284 match operator {
1285 // Navigation operators -> Block cursor
1286 Operator::FindForward { .. }
1287 | Operator::FindBackward { .. }
1288 | Operator::Mark
1289 | Operator::Jump { .. }
1290 | Operator::Register
1291 | Operator::RecordRegister
1292 | Operator::ReplayRegister => CursorShape::Block,
1293
1294 // All other operators -> Underline cursor
1295 _ => CursorShape::Underline,
1296 }
1297 } else {
1298 cursor_shape.normal.unwrap_or(CursorShape::Block)
1299 }
1300 }
1301 Mode::HelixNormal => cursor_shape.normal.unwrap_or(CursorShape::Block),
1302 Mode::Replace => cursor_shape.replace.unwrap_or(CursorShape::Underline),
1303 Mode::Visual | Mode::VisualLine | Mode::VisualBlock | Mode::HelixSelect => {
1304 cursor_shape.visual.unwrap_or(CursorShape::Block)
1305 }
1306 Mode::Insert => cursor_shape.insert.unwrap_or({
1307 let editor_settings = EditorSettings::get_global(cx);
1308 editor_settings.cursor_shape.unwrap_or_default()
1309 }),
1310 }
1311 }
1312
1313 pub fn editor_input_enabled(&self) -> bool {
1314 match self.mode {
1315 Mode::Insert => {
1316 if let Some(operator) = self.operator_stack.last() {
1317 !operator.is_waiting(self.mode)
1318 } else {
1319 true
1320 }
1321 }
1322 Mode::Normal
1323 | Mode::HelixNormal
1324 | Mode::Replace
1325 | Mode::Visual
1326 | Mode::VisualLine
1327 | Mode::VisualBlock
1328 | Mode::HelixSelect => false,
1329 }
1330 }
1331
1332 pub fn should_autoindent(&self) -> bool {
1333 !(self.mode == Mode::Insert && self.last_mode == Mode::VisualBlock)
1334 }
1335
1336 pub fn clip_at_line_ends(&self) -> bool {
1337 match self.mode {
1338 Mode::Insert
1339 | Mode::Visual
1340 | Mode::VisualLine
1341 | Mode::VisualBlock
1342 | Mode::Replace
1343 | Mode::HelixNormal
1344 | Mode::HelixSelect => false,
1345 Mode::Normal => true,
1346 }
1347 }
1348
1349 pub fn extend_key_context(&self, context: &mut KeyContext, cx: &App) {
1350 let mut mode = match self.mode {
1351 Mode::Normal => "normal",
1352 Mode::Visual | Mode::VisualLine | Mode::VisualBlock => "visual",
1353 Mode::Insert => "insert",
1354 Mode::Replace => "replace",
1355 Mode::HelixNormal => "helix_normal",
1356 Mode::HelixSelect => "helix_select",
1357 }
1358 .to_string();
1359
1360 let mut operator_id = "none";
1361
1362 let active_operator = self.active_operator();
1363 if active_operator.is_none() && cx.global::<VimGlobals>().pre_count.is_some()
1364 || active_operator.is_some() && cx.global::<VimGlobals>().post_count.is_some()
1365 {
1366 context.add("VimCount");
1367 }
1368
1369 if let Some(active_operator) = active_operator {
1370 if active_operator.is_waiting(self.mode) {
1371 if matches!(active_operator, Operator::Literal { .. }) {
1372 mode = "literal".to_string();
1373 } else {
1374 mode = "waiting".to_string();
1375 }
1376 } else {
1377 operator_id = active_operator.id();
1378 mode = "operator".to_string();
1379 }
1380 }
1381
1382 if mode == "normal"
1383 || mode == "visual"
1384 || mode == "operator"
1385 || mode == "helix_normal"
1386 || mode == "helix_select"
1387 {
1388 context.add("VimControl");
1389 }
1390 context.set("vim_mode", mode);
1391 context.set("vim_operator", operator_id);
1392 }
1393
1394 fn focused(&mut self, preserve_selection: bool, window: &mut Window, cx: &mut Context<Self>) {
1395 let Some(editor) = self.editor() else {
1396 return;
1397 };
1398 let newest_selection_empty = editor.update(cx, |editor, cx| {
1399 editor
1400 .selections
1401 .newest::<MultiBufferOffset>(&editor.display_snapshot(cx))
1402 .is_empty()
1403 });
1404 let editor = editor.read(cx);
1405 let editor_mode = editor.mode();
1406
1407 if editor_mode.is_full()
1408 && !newest_selection_empty
1409 && self.mode == Mode::Normal
1410 // When following someone, don't switch vim mode.
1411 && editor.leader_id().is_none()
1412 {
1413 if preserve_selection {
1414 self.switch_mode(Mode::Visual, true, window, cx);
1415 } else {
1416 self.update_editor(cx, |_, editor, cx| {
1417 editor.set_clip_at_line_ends(false, cx);
1418 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1419 s.move_with(|_, selection| {
1420 selection.collapse_to(selection.start, selection.goal)
1421 })
1422 });
1423 });
1424 }
1425 }
1426
1427 cx.emit(VimEvent::Focused);
1428 self.sync_vim_settings(window, cx);
1429
1430 if VimSettings::get_global(cx).toggle_relative_line_numbers {
1431 if let Some(old_vim) = Vim::globals(cx).focused_vim() {
1432 if old_vim.entity_id() != cx.entity().entity_id() {
1433 old_vim.update(cx, |vim, cx| {
1434 vim.update_editor(cx, |_, editor, cx| {
1435 editor.set_relative_line_number(None, cx)
1436 });
1437 });
1438
1439 self.update_editor(cx, |vim, editor, cx| {
1440 let is_relative = vim.mode != Mode::Insert;
1441 editor.set_relative_line_number(Some(is_relative), cx)
1442 });
1443 }
1444 } else {
1445 self.update_editor(cx, |vim, editor, cx| {
1446 let is_relative = vim.mode != Mode::Insert;
1447 editor.set_relative_line_number(Some(is_relative), cx)
1448 });
1449 }
1450 }
1451 Vim::globals(cx).focused_vim = Some(cx.entity().downgrade());
1452 }
1453
1454 fn blurred(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1455 self.stop_recording_immediately(NormalBefore.boxed_clone(), cx);
1456 self.store_visual_marks(window, cx);
1457 self.clear_operator(window, cx);
1458 self.update_editor(cx, |vim, editor, cx| {
1459 if vim.cursor_shape(cx) == CursorShape::Block {
1460 editor.set_cursor_shape(CursorShape::Hollow, cx);
1461 }
1462 });
1463 }
1464
1465 fn cursor_shape_changed(&mut self, _: &mut Window, cx: &mut Context<Self>) {
1466 self.update_editor(cx, |vim, editor, cx| {
1467 editor.set_cursor_shape(vim.cursor_shape(cx), cx);
1468 });
1469 }
1470
1471 fn update_editor<S>(
1472 &mut self,
1473 cx: &mut Context<Self>,
1474 update: impl FnOnce(&mut Self, &mut Editor, &mut Context<Editor>) -> S,
1475 ) -> Option<S> {
1476 let editor = self.editor.upgrade()?;
1477 Some(editor.update(cx, |editor, cx| update(self, editor, cx)))
1478 }
1479
1480 fn editor_selections(&mut self, _: &mut Window, cx: &mut Context<Self>) -> Vec<Range<Anchor>> {
1481 self.update_editor(cx, |_, editor, _| {
1482 editor
1483 .selections
1484 .disjoint_anchors_arc()
1485 .iter()
1486 .map(|selection| selection.tail()..selection.head())
1487 .collect()
1488 })
1489 .unwrap_or_default()
1490 }
1491
1492 fn editor_cursor_word(
1493 &mut self,
1494 window: &mut Window,
1495 cx: &mut Context<Self>,
1496 ) -> Option<String> {
1497 self.update_editor(cx, |_, editor, cx| {
1498 let snapshot = &editor.snapshot(window, cx);
1499 let selection = editor
1500 .selections
1501 .newest::<MultiBufferOffset>(&snapshot.display_snapshot);
1502
1503 let snapshot = snapshot.buffer_snapshot();
1504 let (range, kind) =
1505 snapshot.surrounding_word(selection.start, Some(CharScopeContext::Completion));
1506 if kind == Some(CharKind::Word) {
1507 let text: String = snapshot.text_for_range(range).collect();
1508 if !text.trim().is_empty() {
1509 return Some(text);
1510 }
1511 }
1512
1513 None
1514 })
1515 .unwrap_or_default()
1516 }
1517
1518 /// When doing an action that modifies the buffer, we start recording so that `.`
1519 /// will replay the action.
1520 pub fn start_recording(&mut self, cx: &mut Context<Self>) {
1521 Vim::update_globals(cx, |globals, cx| {
1522 if !globals.dot_replaying {
1523 globals.dot_recording = true;
1524 globals.recording_actions = Default::default();
1525 globals.recording_count = None;
1526
1527 let selections = self.editor().map(|editor| {
1528 editor.update(cx, |editor, cx| {
1529 let snapshot = editor.display_snapshot(cx);
1530
1531 (
1532 editor.selections.oldest::<Point>(&snapshot),
1533 editor.selections.newest::<Point>(&snapshot),
1534 )
1535 })
1536 });
1537
1538 if let Some((oldest, newest)) = selections {
1539 globals.recorded_selection = match self.mode {
1540 Mode::Visual if newest.end.row == newest.start.row => {
1541 RecordedSelection::SingleLine {
1542 cols: newest.end.column - newest.start.column,
1543 }
1544 }
1545 Mode::Visual => RecordedSelection::Visual {
1546 rows: newest.end.row - newest.start.row,
1547 cols: newest.end.column,
1548 },
1549 Mode::VisualLine => RecordedSelection::VisualLine {
1550 rows: newest.end.row - newest.start.row,
1551 },
1552 Mode::VisualBlock => RecordedSelection::VisualBlock {
1553 rows: newest.end.row.abs_diff(oldest.start.row),
1554 cols: newest.end.column.abs_diff(oldest.start.column),
1555 },
1556 _ => RecordedSelection::None,
1557 }
1558 } else {
1559 globals.recorded_selection = RecordedSelection::None;
1560 }
1561 }
1562 })
1563 }
1564
1565 pub fn stop_replaying(&mut self, cx: &mut Context<Self>) {
1566 let globals = Vim::globals(cx);
1567 globals.dot_replaying = false;
1568 if let Some(replayer) = globals.replayer.take() {
1569 replayer.stop();
1570 }
1571 }
1572
1573 /// When finishing an action that modifies the buffer, stop recording.
1574 /// as you usually call this within a keystroke handler we also ensure that
1575 /// the current action is recorded.
1576 pub fn stop_recording(&mut self, cx: &mut Context<Self>) {
1577 let globals = Vim::globals(cx);
1578 if globals.dot_recording {
1579 globals.stop_recording_after_next_action = true;
1580 }
1581 self.exit_temporary_mode = self.temp_mode;
1582 }
1583
1584 /// Stops recording actions immediately rather than waiting until after the
1585 /// next action to stop recording.
1586 ///
1587 /// This doesn't include the current action.
1588 pub fn stop_recording_immediately(&mut self, action: Box<dyn Action>, cx: &mut Context<Self>) {
1589 let globals = Vim::globals(cx);
1590 if globals.dot_recording {
1591 globals
1592 .recording_actions
1593 .push(ReplayableAction::Action(action.boxed_clone()));
1594 globals.recorded_actions = mem::take(&mut globals.recording_actions);
1595 globals.recorded_count = globals.recording_count.take();
1596 globals.dot_recording = false;
1597 globals.stop_recording_after_next_action = false;
1598 }
1599 self.exit_temporary_mode = self.temp_mode;
1600 }
1601
1602 /// Explicitly record one action (equivalents to start_recording and stop_recording)
1603 pub fn record_current_action(&mut self, cx: &mut Context<Self>) {
1604 self.start_recording(cx);
1605 self.stop_recording(cx);
1606 }
1607
1608 fn push_count_digit(&mut self, number: usize, window: &mut Window, cx: &mut Context<Self>) {
1609 if self.active_operator().is_some() {
1610 let post_count = Vim::globals(cx).post_count.unwrap_or(0);
1611
1612 Vim::globals(cx).post_count = Some(
1613 post_count
1614 .checked_mul(10)
1615 .and_then(|post_count| post_count.checked_add(number))
1616 .filter(|post_count| *post_count < isize::MAX as usize)
1617 .unwrap_or(post_count),
1618 )
1619 } else {
1620 let pre_count = Vim::globals(cx).pre_count.unwrap_or(0);
1621
1622 Vim::globals(cx).pre_count = Some(
1623 pre_count
1624 .checked_mul(10)
1625 .and_then(|pre_count| pre_count.checked_add(number))
1626 .filter(|pre_count| *pre_count < isize::MAX as usize)
1627 .unwrap_or(pre_count),
1628 )
1629 }
1630 // update the keymap so that 0 works
1631 self.sync_vim_settings(window, cx)
1632 }
1633
1634 fn select_register(&mut self, register: Arc<str>, window: &mut Window, cx: &mut Context<Self>) {
1635 if register.chars().count() == 1 {
1636 self.selected_register
1637 .replace(register.chars().next().unwrap());
1638 }
1639 self.operator_stack.clear();
1640 self.sync_vim_settings(window, cx);
1641 }
1642
1643 fn maybe_pop_operator(&mut self) -> Option<Operator> {
1644 self.operator_stack.pop()
1645 }
1646
1647 fn pop_operator(&mut self, window: &mut Window, cx: &mut Context<Self>) -> Operator {
1648 let popped_operator = self.operator_stack.pop()
1649 .expect("Operator popped when no operator was on the stack. This likely means there is an invalid keymap config");
1650 self.sync_vim_settings(window, cx);
1651 popped_operator
1652 }
1653
1654 fn clear_operator(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1655 Vim::take_count(cx);
1656 Vim::take_forced_motion(cx);
1657 self.selected_register.take();
1658 self.operator_stack.clear();
1659 self.sync_vim_settings(window, cx);
1660 }
1661
1662 fn active_operator(&self) -> Option<Operator> {
1663 self.operator_stack.last().cloned()
1664 }
1665
1666 fn transaction_begun(
1667 &mut self,
1668 transaction_id: TransactionId,
1669 _window: &mut Window,
1670 _: &mut Context<Self>,
1671 ) {
1672 let mode = if (self.mode == Mode::Insert
1673 || self.mode == Mode::Replace
1674 || self.mode == Mode::Normal)
1675 && self.current_tx.is_none()
1676 {
1677 self.current_tx = Some(transaction_id);
1678 self.last_mode
1679 } else {
1680 self.mode
1681 };
1682 if mode == Mode::VisualLine || mode == Mode::VisualBlock {
1683 self.undo_modes.insert(transaction_id, mode);
1684 }
1685 }
1686
1687 fn transaction_undone(
1688 &mut self,
1689 transaction_id: &TransactionId,
1690 window: &mut Window,
1691 cx: &mut Context<Self>,
1692 ) {
1693 match self.mode {
1694 Mode::VisualLine | Mode::VisualBlock | Mode::Visual | Mode::HelixSelect => {
1695 self.update_editor(cx, |vim, editor, cx| {
1696 let original_mode = vim.undo_modes.get(transaction_id);
1697 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1698 match original_mode {
1699 Some(Mode::VisualLine) => {
1700 s.move_with(|map, selection| {
1701 selection.collapse_to(
1702 map.prev_line_boundary(selection.start.to_point(map)).1,
1703 SelectionGoal::None,
1704 )
1705 });
1706 }
1707 Some(Mode::VisualBlock) => {
1708 let mut first = s.first_anchor();
1709 first.collapse_to(first.start, first.goal);
1710 s.select_anchors(vec![first]);
1711 }
1712 _ => {
1713 s.move_with(|map, selection| {
1714 selection.collapse_to(
1715 map.clip_at_line_end(selection.start),
1716 selection.goal,
1717 );
1718 });
1719 }
1720 }
1721 });
1722 });
1723 self.switch_mode(Mode::Normal, true, window, cx)
1724 }
1725 Mode::Normal => {
1726 self.update_editor(cx, |_, editor, cx| {
1727 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1728 s.move_with(|map, selection| {
1729 selection
1730 .collapse_to(map.clip_at_line_end(selection.end), selection.goal)
1731 })
1732 })
1733 });
1734 }
1735 Mode::Insert | Mode::Replace | Mode::HelixNormal => {}
1736 }
1737 }
1738
1739 fn local_selections_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1740 let Some(editor) = self.editor() else { return };
1741
1742 if editor.read(cx).leader_id().is_some() {
1743 return;
1744 }
1745
1746 let newest = editor.read(cx).selections.newest_anchor().clone();
1747 let is_multicursor = editor.read(cx).selections.count() > 1;
1748 if self.mode == Mode::Insert && self.current_tx.is_some() {
1749 if self.current_anchor.is_none() {
1750 self.current_anchor = Some(newest);
1751 } else if self.current_anchor.as_ref().unwrap() != &newest
1752 && let Some(tx_id) = self.current_tx.take()
1753 {
1754 self.update_editor(cx, |_, editor, cx| {
1755 editor.group_until_transaction(tx_id, cx)
1756 });
1757 }
1758 } else if self.mode == Mode::Normal && newest.start != newest.end {
1759 if matches!(newest.goal, SelectionGoal::HorizontalRange { .. }) {
1760 self.switch_mode(Mode::VisualBlock, false, window, cx);
1761 } else {
1762 self.switch_mode(Mode::Visual, false, window, cx)
1763 }
1764 } else if newest.start == newest.end
1765 && !is_multicursor
1766 && [Mode::Visual, Mode::VisualLine, Mode::VisualBlock].contains(&self.mode)
1767 {
1768 self.switch_mode(Mode::Normal, false, window, cx);
1769 }
1770 }
1771
1772 fn input_ignored(&mut self, text: Arc<str>, window: &mut Window, cx: &mut Context<Self>) {
1773 if text.is_empty() {
1774 return;
1775 }
1776
1777 match self.active_operator() {
1778 Some(Operator::FindForward { before, multiline }) => {
1779 let find = Motion::FindForward {
1780 before,
1781 char: text.chars().next().unwrap(),
1782 mode: if multiline {
1783 FindRange::MultiLine
1784 } else {
1785 FindRange::SingleLine
1786 },
1787 smartcase: VimSettings::get_global(cx).use_smartcase_find,
1788 };
1789 Vim::globals(cx).last_find = Some(find.clone());
1790 self.motion(find, window, cx)
1791 }
1792 Some(Operator::FindBackward { after, multiline }) => {
1793 let find = Motion::FindBackward {
1794 after,
1795 char: text.chars().next().unwrap(),
1796 mode: if multiline {
1797 FindRange::MultiLine
1798 } else {
1799 FindRange::SingleLine
1800 },
1801 smartcase: VimSettings::get_global(cx).use_smartcase_find,
1802 };
1803 Vim::globals(cx).last_find = Some(find.clone());
1804 self.motion(find, window, cx)
1805 }
1806 Some(Operator::Sneak { first_char }) => {
1807 if let Some(first_char) = first_char {
1808 if let Some(second_char) = text.chars().next() {
1809 let sneak = Motion::Sneak {
1810 first_char,
1811 second_char,
1812 smartcase: VimSettings::get_global(cx).use_smartcase_find,
1813 };
1814 Vim::globals(cx).last_find = Some(sneak.clone());
1815 self.motion(sneak, window, cx)
1816 }
1817 } else {
1818 let first_char = text.chars().next();
1819 self.pop_operator(window, cx);
1820 self.push_operator(Operator::Sneak { first_char }, window, cx);
1821 }
1822 }
1823 Some(Operator::SneakBackward { first_char }) => {
1824 if let Some(first_char) = first_char {
1825 if let Some(second_char) = text.chars().next() {
1826 let sneak = Motion::SneakBackward {
1827 first_char,
1828 second_char,
1829 smartcase: VimSettings::get_global(cx).use_smartcase_find,
1830 };
1831 Vim::globals(cx).last_find = Some(sneak.clone());
1832 self.motion(sneak, window, cx)
1833 }
1834 } else {
1835 let first_char = text.chars().next();
1836 self.pop_operator(window, cx);
1837 self.push_operator(Operator::SneakBackward { first_char }, window, cx);
1838 }
1839 }
1840 Some(Operator::Replace) => match self.mode {
1841 Mode::Normal => self.normal_replace(text, window, cx),
1842 Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
1843 self.visual_replace(text, window, cx)
1844 }
1845 Mode::HelixNormal => self.helix_replace(&text, window, cx),
1846 _ => self.clear_operator(window, cx),
1847 },
1848 Some(Operator::Digraph { first_char }) => {
1849 if let Some(first_char) = first_char {
1850 if let Some(second_char) = text.chars().next() {
1851 self.insert_digraph(first_char, second_char, window, cx);
1852 }
1853 } else {
1854 let first_char = text.chars().next();
1855 self.pop_operator(window, cx);
1856 self.push_operator(Operator::Digraph { first_char }, window, cx);
1857 }
1858 }
1859 Some(Operator::Literal { prefix }) => {
1860 self.handle_literal_input(prefix.unwrap_or_default(), &text, window, cx)
1861 }
1862 Some(Operator::AddSurrounds { target }) => match self.mode {
1863 Mode::Normal => {
1864 if let Some(target) = target {
1865 self.add_surrounds(text, target, window, cx);
1866 self.clear_operator(window, cx);
1867 }
1868 }
1869 Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
1870 self.add_surrounds(text, SurroundsType::Selection, window, cx);
1871 self.clear_operator(window, cx);
1872 }
1873 _ => self.clear_operator(window, cx),
1874 },
1875 Some(Operator::ChangeSurrounds { target, opening }) => match self.mode {
1876 Mode::Normal => {
1877 if let Some(target) = target {
1878 self.change_surrounds(text, target, opening, window, cx);
1879 self.clear_operator(window, cx);
1880 }
1881 }
1882 _ => self.clear_operator(window, cx),
1883 },
1884 Some(Operator::DeleteSurrounds) => match self.mode {
1885 Mode::Normal => {
1886 self.delete_surrounds(text, window, cx);
1887 self.clear_operator(window, cx);
1888 }
1889 _ => self.clear_operator(window, cx),
1890 },
1891 Some(Operator::Mark) => self.create_mark(text, window, cx),
1892 Some(Operator::RecordRegister) => {
1893 self.record_register(text.chars().next().unwrap(), window, cx)
1894 }
1895 Some(Operator::ReplayRegister) => {
1896 self.replay_register(text.chars().next().unwrap(), window, cx)
1897 }
1898 Some(Operator::Register) => match self.mode {
1899 Mode::Insert => {
1900 self.update_editor(cx, |_, editor, cx| {
1901 if let Some(register) = Vim::update_globals(cx, |globals, cx| {
1902 globals.read_register(text.chars().next(), Some(editor), cx)
1903 }) {
1904 editor.do_paste(
1905 ®ister.text.to_string(),
1906 register.clipboard_selections,
1907 false,
1908 window,
1909 cx,
1910 )
1911 }
1912 });
1913 self.clear_operator(window, cx);
1914 }
1915 _ => {
1916 self.select_register(text, window, cx);
1917 }
1918 },
1919 Some(Operator::Jump { line }) => self.jump(text, line, true, window, cx),
1920 _ => {
1921 if self.mode == Mode::Replace {
1922 self.multi_replace(text, window, cx)
1923 }
1924
1925 if self.mode == Mode::Normal {
1926 self.update_editor(cx, |_, editor, cx| {
1927 editor.accept_edit_prediction(
1928 &editor::actions::AcceptEditPrediction {},
1929 window,
1930 cx,
1931 );
1932 });
1933 }
1934 }
1935 }
1936 }
1937
1938 fn sync_vim_settings(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1939 self.update_editor(cx, |vim, editor, cx| {
1940 editor.set_cursor_shape(vim.cursor_shape(cx), cx);
1941 editor.set_clip_at_line_ends(vim.clip_at_line_ends(), cx);
1942 let collapse_matches = !HelixModeSetting::get_global(cx).0;
1943 editor.set_collapse_matches(collapse_matches);
1944 editor.set_input_enabled(vim.editor_input_enabled());
1945 editor.set_autoindent(vim.should_autoindent());
1946 editor.set_cursor_offset_on_selection(vim.mode.is_visual());
1947 editor
1948 .selections
1949 .set_line_mode(matches!(vim.mode, Mode::VisualLine));
1950
1951 let hide_edit_predictions = !matches!(vim.mode, Mode::Insert | Mode::Replace);
1952 editor.set_edit_predictions_hidden_for_vim_mode(hide_edit_predictions, window, cx);
1953 });
1954 cx.notify()
1955 }
1956}
1957
1958#[derive(RegisterSetting)]
1959struct VimSettings {
1960 pub default_mode: Mode,
1961 pub toggle_relative_line_numbers: bool,
1962 pub use_system_clipboard: settings::UseSystemClipboard,
1963 pub use_smartcase_find: bool,
1964 pub custom_digraphs: HashMap<String, Arc<str>>,
1965 pub highlight_on_yank_duration: u64,
1966 pub cursor_shape: CursorShapeSettings,
1967}
1968
1969/// The settings for cursor shape.
1970#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1971pub struct CursorShapeSettings {
1972 /// Cursor shape for the normal mode.
1973 ///
1974 /// Default: block
1975 pub normal: Option<CursorShape>,
1976 /// Cursor shape for the replace mode.
1977 ///
1978 /// Default: underline
1979 pub replace: Option<CursorShape>,
1980 /// Cursor shape for the visual mode.
1981 ///
1982 /// Default: block
1983 pub visual: Option<CursorShape>,
1984 /// Cursor shape for the insert mode.
1985 ///
1986 /// The default value follows the primary cursor_shape.
1987 pub insert: Option<CursorShape>,
1988}
1989
1990impl From<settings::CursorShapeSettings> for CursorShapeSettings {
1991 fn from(settings: settings::CursorShapeSettings) -> Self {
1992 Self {
1993 normal: settings.normal.map(Into::into),
1994 replace: settings.replace.map(Into::into),
1995 visual: settings.visual.map(Into::into),
1996 insert: settings.insert.map(Into::into),
1997 }
1998 }
1999}
2000
2001impl From<settings::ModeContent> for Mode {
2002 fn from(mode: ModeContent) -> Self {
2003 match mode {
2004 ModeContent::Normal => Self::Normal,
2005 ModeContent::Insert => Self::Insert,
2006 }
2007 }
2008}
2009
2010impl Settings for VimSettings {
2011 fn from_settings(content: &settings::SettingsContent) -> Self {
2012 let vim = content.vim.clone().unwrap();
2013 Self {
2014 default_mode: vim.default_mode.unwrap().into(),
2015 toggle_relative_line_numbers: vim.toggle_relative_line_numbers.unwrap(),
2016 use_system_clipboard: vim.use_system_clipboard.unwrap(),
2017 use_smartcase_find: vim.use_smartcase_find.unwrap(),
2018 custom_digraphs: vim.custom_digraphs.unwrap(),
2019 highlight_on_yank_duration: vim.highlight_on_yank_duration.unwrap(),
2020 cursor_shape: vim.cursor_shape.unwrap().into(),
2021 }
2022 }
2023}