vim.rs

   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.paste(&VimPaste::default(), window, cx);
 928                    }
 929                    _ => {
 930                        vim.update_editor(cx, |_, editor, cx| editor.paste(&Paste, window, cx));
 931                    }
 932                },
 933            );
 934
 935            normal::register(editor, cx);
 936            insert::register(editor, cx);
 937            helix::register(editor, cx);
 938            motion::register(editor, cx);
 939            command::register(editor, cx);
 940            replace::register(editor, cx);
 941            indent::register(editor, cx);
 942            rewrap::register(editor, cx);
 943            object::register(editor, cx);
 944            visual::register(editor, cx);
 945            change_list::register(editor, cx);
 946            digraph::register(editor, cx);
 947
 948            if editor.is_focused(window) {
 949                cx.defer_in(window, |vim, window, cx| {
 950                    vim.focused(false, window, cx);
 951                })
 952            }
 953        })
 954    }
 955
 956    fn deactivate(editor: &mut Editor, cx: &mut Context<Editor>) {
 957        editor.set_cursor_shape(
 958            EditorSettings::get_global(cx)
 959                .cursor_shape
 960                .unwrap_or_default(),
 961            cx,
 962        );
 963        editor.set_clip_at_line_ends(false, cx);
 964        editor.set_collapse_matches(false);
 965        editor.set_input_enabled(true);
 966        editor.set_autoindent(true);
 967        editor.selections.set_line_mode(false);
 968        editor.unregister_addon::<VimAddon>();
 969        editor.set_relative_line_number(None, cx);
 970        if let Some(vim) = Vim::globals(cx).focused_vim()
 971            && vim.entity_id() == cx.entity().entity_id()
 972        {
 973            Vim::globals(cx).focused_vim = None;
 974        }
 975    }
 976
 977    /// Register an action on the editor.
 978    pub fn action<A: Action>(
 979        editor: &mut Editor,
 980        cx: &mut Context<Vim>,
 981        f: impl Fn(&mut Vim, &A, &mut Window, &mut Context<Vim>) + 'static,
 982    ) {
 983        let subscription = editor.register_action(cx.listener(f));
 984        cx.on_release(|_, _| drop(subscription)).detach();
 985    }
 986
 987    pub fn editor(&self) -> Option<Entity<Editor>> {
 988        self.editor.upgrade()
 989    }
 990
 991    pub fn workspace(&self, window: &mut Window) -> Option<Entity<Workspace>> {
 992        window.root::<Workspace>().flatten()
 993    }
 994
 995    pub fn pane(&self, window: &mut Window, cx: &mut Context<Self>) -> Option<Entity<Pane>> {
 996        self.workspace(window)
 997            .map(|workspace| workspace.read(cx).focused_pane(window, cx))
 998    }
 999
1000    pub fn enabled(cx: &mut App) -> bool {
1001        VimModeSetting::get_global(cx).0 || HelixModeSetting::get_global(cx).0
1002    }
1003
1004    /// Called whenever an keystroke is typed so vim can observe all actions
1005    /// and keystrokes accordingly.
1006    fn observe_keystrokes(
1007        &mut self,
1008        keystroke_event: &KeystrokeEvent,
1009        window: &mut Window,
1010        cx: &mut Context<Self>,
1011    ) {
1012        if self.exit_temporary_mode {
1013            self.exit_temporary_mode = false;
1014            // Don't switch to insert mode if the action is temporary_normal.
1015            if let Some(action) = keystroke_event.action.as_ref()
1016                && action.as_any().downcast_ref::<TemporaryNormal>().is_some()
1017            {
1018                return;
1019            }
1020            self.switch_mode(Mode::Insert, false, window, cx)
1021        }
1022        if let Some(action) = keystroke_event.action.as_ref() {
1023            // Keystroke is handled by the vim system, so continue forward
1024            if action.name().starts_with("vim::") {
1025                self.update_editor(cx, |_, editor, cx| {
1026                    editor.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx)
1027                });
1028
1029                return;
1030            }
1031        } else if window.has_pending_keystrokes() || keystroke_event.keystroke.is_ime_in_progress()
1032        {
1033            return;
1034        }
1035
1036        if let Some(operator) = self.active_operator() {
1037            match operator {
1038                Operator::Literal { prefix } => {
1039                    self.handle_literal_keystroke(
1040                        keystroke_event,
1041                        prefix.unwrap_or_default(),
1042                        window,
1043                        cx,
1044                    );
1045                }
1046                _ if !operator.is_waiting(self.mode) => {
1047                    self.clear_operator(window, cx);
1048                    self.stop_recording_immediately(Box::new(ClearOperators), cx)
1049                }
1050                _ => {}
1051            }
1052        }
1053    }
1054
1055    fn handle_editor_event(
1056        &mut self,
1057        event: &EditorEvent,
1058        window: &mut Window,
1059        cx: &mut Context<Self>,
1060    ) {
1061        match event {
1062            EditorEvent::Focused => self.focused(true, window, cx),
1063            EditorEvent::Blurred => self.blurred(window, cx),
1064            EditorEvent::SelectionsChanged { local: true } => {
1065                self.local_selections_changed(window, cx);
1066            }
1067            EditorEvent::InputIgnored { text } => {
1068                self.input_ignored(text.clone(), window, cx);
1069                Vim::globals(cx).observe_insertion(text, None)
1070            }
1071            EditorEvent::InputHandled {
1072                text,
1073                utf16_range_to_replace: range_to_replace,
1074            } => Vim::globals(cx).observe_insertion(text, range_to_replace.clone()),
1075            EditorEvent::TransactionBegun { transaction_id } => {
1076                self.transaction_begun(*transaction_id, window, cx)
1077            }
1078            EditorEvent::TransactionUndone { transaction_id } => {
1079                self.transaction_undone(transaction_id, window, cx)
1080            }
1081            EditorEvent::Edited { .. } => self.push_to_change_list(window, cx),
1082            EditorEvent::FocusedIn => self.sync_vim_settings(window, cx),
1083            EditorEvent::CursorShapeChanged => self.cursor_shape_changed(window, cx),
1084            EditorEvent::PushedToNavHistory {
1085                anchor,
1086                is_deactivate,
1087            } => {
1088                self.update_editor(cx, |vim, editor, cx| {
1089                    let mark = if *is_deactivate {
1090                        "\"".to_string()
1091                    } else {
1092                        "'".to_string()
1093                    };
1094                    vim.set_mark(mark, vec![*anchor], editor.buffer(), window, cx);
1095                });
1096            }
1097            _ => {}
1098        }
1099    }
1100
1101    fn push_operator(&mut self, operator: Operator, window: &mut Window, cx: &mut Context<Self>) {
1102        if operator.starts_dot_recording() {
1103            self.start_recording(cx);
1104        }
1105        // Since these operations can only be entered with pre-operators,
1106        // we need to clear the previous operators when pushing,
1107        // so that the current stack is the most correct
1108        if matches!(
1109            operator,
1110            Operator::AddSurrounds { .. }
1111                | Operator::ChangeSurrounds { .. }
1112                | Operator::DeleteSurrounds
1113                | Operator::Exchange
1114        ) {
1115            self.operator_stack.clear();
1116        };
1117        self.operator_stack.push(operator);
1118        self.sync_vim_settings(window, cx);
1119    }
1120
1121    pub fn switch_mode(
1122        &mut self,
1123        mode: Mode,
1124        leave_selections: bool,
1125        window: &mut Window,
1126        cx: &mut Context<Self>,
1127    ) {
1128        if self.temp_mode && mode == Mode::Normal {
1129            self.temp_mode = false;
1130            self.switch_mode(Mode::Normal, leave_selections, window, cx);
1131            self.switch_mode(Mode::Insert, false, window, cx);
1132            return;
1133        } else if self.temp_mode
1134            && !matches!(mode, Mode::Visual | Mode::VisualLine | Mode::VisualBlock)
1135        {
1136            self.temp_mode = false;
1137        }
1138
1139        let last_mode = self.mode;
1140        let prior_mode = self.last_mode;
1141        let prior_tx = self.current_tx;
1142        self.status_label.take();
1143        self.last_mode = last_mode;
1144        self.mode = mode;
1145        self.operator_stack.clear();
1146        self.selected_register.take();
1147        self.cancel_running_command(window, cx);
1148        if mode == Mode::Normal || mode != last_mode {
1149            self.current_tx.take();
1150            self.current_anchor.take();
1151            self.update_editor(cx, |_, editor, _| {
1152                editor.clear_selection_drag_state();
1153            });
1154        }
1155        Vim::take_forced_motion(cx);
1156        if mode != Mode::Insert && mode != Mode::Replace {
1157            Vim::take_count(cx);
1158        }
1159
1160        // Sync editor settings like clip mode
1161        self.sync_vim_settings(window, cx);
1162
1163        if VimSettings::get_global(cx).toggle_relative_line_numbers
1164            && self.mode != self.last_mode
1165            && (self.mode == Mode::Insert || self.last_mode == Mode::Insert)
1166        {
1167            self.update_editor(cx, |vim, editor, cx| {
1168                let is_relative = vim.mode != Mode::Insert;
1169                editor.set_relative_line_number(Some(is_relative), cx)
1170            });
1171        }
1172        if HelixModeSetting::get_global(cx).0 {
1173            if self.mode == Mode::Normal {
1174                self.mode = Mode::HelixNormal
1175            } else if self.mode == Mode::Visual {
1176                self.mode = Mode::HelixSelect
1177            }
1178        }
1179
1180        if leave_selections {
1181            return;
1182        }
1183
1184        if !mode.is_visual() && last_mode.is_visual() {
1185            self.create_visual_marks(last_mode, window, cx);
1186        }
1187
1188        // Adjust selections
1189        self.update_editor(cx, |vim, editor, cx| {
1190            if last_mode != Mode::VisualBlock && last_mode.is_visual() && mode == Mode::VisualBlock
1191            {
1192                vim.visual_block_motion(true, editor, window, cx, |_, point, goal| {
1193                    Some((point, goal))
1194                })
1195            }
1196            if (last_mode == Mode::Insert || last_mode == Mode::Replace)
1197                && let Some(prior_tx) = prior_tx
1198            {
1199                editor.group_until_transaction(prior_tx, cx)
1200            }
1201
1202            editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1203                // we cheat with visual block mode and use multiple cursors.
1204                // the cost of this cheat is we need to convert back to a single
1205                // cursor whenever vim would.
1206                if last_mode == Mode::VisualBlock
1207                    && (mode != Mode::VisualBlock && mode != Mode::Insert)
1208                {
1209                    let tail = s.oldest_anchor().tail();
1210                    let head = s.newest_anchor().head();
1211                    s.select_anchor_ranges(vec![tail..head]);
1212                } else if last_mode == Mode::Insert
1213                    && prior_mode == Mode::VisualBlock
1214                    && mode != Mode::VisualBlock
1215                {
1216                    let pos = s.first_anchor().head();
1217                    s.select_anchor_ranges(vec![pos..pos])
1218                }
1219
1220                let snapshot = s.display_snapshot();
1221                if let Some(pending) = s.pending_anchor_mut()
1222                    && pending.reversed
1223                    && mode.is_visual()
1224                    && !last_mode.is_visual()
1225                {
1226                    let mut end = pending.end.to_point(&snapshot.buffer_snapshot());
1227                    end = snapshot
1228                        .buffer_snapshot()
1229                        .clip_point(end + Point::new(0, 1), Bias::Right);
1230                    pending.end = snapshot.buffer_snapshot().anchor_before(end);
1231                }
1232
1233                s.move_with(|map, selection| {
1234                    if last_mode.is_visual() && !mode.is_visual() {
1235                        let mut point = selection.head();
1236                        if !selection.reversed && !selection.is_empty() {
1237                            point = movement::left(map, selection.head());
1238                        } else if selection.is_empty() {
1239                            point = map.clip_point(point, Bias::Left);
1240                        }
1241                        selection.collapse_to(point, selection.goal)
1242                    } else if !last_mode.is_visual() && mode.is_visual() && selection.is_empty() {
1243                        selection.end = movement::right(map, selection.start);
1244                    }
1245                });
1246            })
1247        });
1248    }
1249
1250    pub fn take_count(cx: &mut App) -> Option<usize> {
1251        let global_state = cx.global_mut::<VimGlobals>();
1252        if global_state.dot_replaying {
1253            return global_state.recorded_count;
1254        }
1255
1256        let count = if global_state.post_count.is_none() && global_state.pre_count.is_none() {
1257            return None;
1258        } else {
1259            Some(
1260                global_state.post_count.take().unwrap_or(1)
1261                    * global_state.pre_count.take().unwrap_or(1),
1262            )
1263        };
1264
1265        if global_state.dot_recording {
1266            global_state.recording_count = count;
1267        }
1268        count
1269    }
1270
1271    pub fn take_forced_motion(cx: &mut App) -> bool {
1272        let global_state = cx.global_mut::<VimGlobals>();
1273        let forced_motion = global_state.forced_motion;
1274        global_state.forced_motion = false;
1275        forced_motion
1276    }
1277
1278    pub fn cursor_shape(&self, cx: &mut App) -> CursorShape {
1279        let cursor_shape = VimSettings::get_global(cx).cursor_shape;
1280        match self.mode {
1281            Mode::Normal => {
1282                if let Some(operator) = self.operator_stack.last() {
1283                    match operator {
1284                        // Navigation operators -> Block cursor
1285                        Operator::FindForward { .. }
1286                        | Operator::FindBackward { .. }
1287                        | Operator::Mark
1288                        | Operator::Jump { .. }
1289                        | Operator::Register
1290                        | Operator::RecordRegister
1291                        | Operator::ReplayRegister => CursorShape::Block,
1292
1293                        // All other operators -> Underline cursor
1294                        _ => CursorShape::Underline,
1295                    }
1296                } else {
1297                    cursor_shape.normal.unwrap_or(CursorShape::Block)
1298                }
1299            }
1300            Mode::HelixNormal => cursor_shape.normal.unwrap_or(CursorShape::Block),
1301            Mode::Replace => cursor_shape.replace.unwrap_or(CursorShape::Underline),
1302            Mode::Visual | Mode::VisualLine | Mode::VisualBlock | Mode::HelixSelect => {
1303                cursor_shape.visual.unwrap_or(CursorShape::Block)
1304            }
1305            Mode::Insert => cursor_shape.insert.unwrap_or({
1306                let editor_settings = EditorSettings::get_global(cx);
1307                editor_settings.cursor_shape.unwrap_or_default()
1308            }),
1309        }
1310    }
1311
1312    pub fn editor_input_enabled(&self) -> bool {
1313        match self.mode {
1314            Mode::Insert => {
1315                if let Some(operator) = self.operator_stack.last() {
1316                    !operator.is_waiting(self.mode)
1317                } else {
1318                    true
1319                }
1320            }
1321            Mode::Normal
1322            | Mode::HelixNormal
1323            | Mode::Replace
1324            | Mode::Visual
1325            | Mode::VisualLine
1326            | Mode::VisualBlock
1327            | Mode::HelixSelect => false,
1328        }
1329    }
1330
1331    pub fn should_autoindent(&self) -> bool {
1332        !(self.mode == Mode::Insert && self.last_mode == Mode::VisualBlock)
1333    }
1334
1335    pub fn clip_at_line_ends(&self) -> bool {
1336        match self.mode {
1337            Mode::Insert
1338            | Mode::Visual
1339            | Mode::VisualLine
1340            | Mode::VisualBlock
1341            | Mode::Replace
1342            | Mode::HelixNormal
1343            | Mode::HelixSelect => false,
1344            Mode::Normal => true,
1345        }
1346    }
1347
1348    pub fn extend_key_context(&self, context: &mut KeyContext, cx: &App) {
1349        let mut mode = match self.mode {
1350            Mode::Normal => "normal",
1351            Mode::Visual | Mode::VisualLine | Mode::VisualBlock => "visual",
1352            Mode::Insert => "insert",
1353            Mode::Replace => "replace",
1354            Mode::HelixNormal => "helix_normal",
1355            Mode::HelixSelect => "helix_select",
1356        }
1357        .to_string();
1358
1359        let mut operator_id = "none";
1360
1361        let active_operator = self.active_operator();
1362        if active_operator.is_none() && cx.global::<VimGlobals>().pre_count.is_some()
1363            || active_operator.is_some() && cx.global::<VimGlobals>().post_count.is_some()
1364        {
1365            context.add("VimCount");
1366        }
1367
1368        if let Some(active_operator) = active_operator {
1369            if active_operator.is_waiting(self.mode) {
1370                if matches!(active_operator, Operator::Literal { .. }) {
1371                    mode = "literal".to_string();
1372                } else {
1373                    mode = "waiting".to_string();
1374                }
1375            } else {
1376                operator_id = active_operator.id();
1377                mode = "operator".to_string();
1378            }
1379        }
1380
1381        if mode == "normal"
1382            || mode == "visual"
1383            || mode == "operator"
1384            || mode == "helix_normal"
1385            || mode == "helix_select"
1386        {
1387            context.add("VimControl");
1388        }
1389        context.set("vim_mode", mode);
1390        context.set("vim_operator", operator_id);
1391    }
1392
1393    fn focused(&mut self, preserve_selection: bool, window: &mut Window, cx: &mut Context<Self>) {
1394        let Some(editor) = self.editor() else {
1395            return;
1396        };
1397        let newest_selection_empty = editor.update(cx, |editor, cx| {
1398            editor
1399                .selections
1400                .newest::<MultiBufferOffset>(&editor.display_snapshot(cx))
1401                .is_empty()
1402        });
1403        let editor = editor.read(cx);
1404        let editor_mode = editor.mode();
1405
1406        if editor_mode.is_full()
1407            && !newest_selection_empty
1408            && self.mode == Mode::Normal
1409            // When following someone, don't switch vim mode.
1410            && editor.leader_id().is_none()
1411        {
1412            if preserve_selection {
1413                self.switch_mode(Mode::Visual, true, window, cx);
1414            } else {
1415                self.update_editor(cx, |_, editor, cx| {
1416                    editor.set_clip_at_line_ends(false, cx);
1417                    editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1418                        s.move_with(|_, selection| {
1419                            selection.collapse_to(selection.start, selection.goal)
1420                        })
1421                    });
1422                });
1423            }
1424        }
1425
1426        cx.emit(VimEvent::Focused);
1427        self.sync_vim_settings(window, cx);
1428
1429        if VimSettings::get_global(cx).toggle_relative_line_numbers {
1430            if let Some(old_vim) = Vim::globals(cx).focused_vim() {
1431                if old_vim.entity_id() != cx.entity().entity_id() {
1432                    old_vim.update(cx, |vim, cx| {
1433                        vim.update_editor(cx, |_, editor, cx| {
1434                            editor.set_relative_line_number(None, cx)
1435                        });
1436                    });
1437
1438                    self.update_editor(cx, |vim, editor, cx| {
1439                        let is_relative = vim.mode != Mode::Insert;
1440                        editor.set_relative_line_number(Some(is_relative), cx)
1441                    });
1442                }
1443            } else {
1444                self.update_editor(cx, |vim, editor, cx| {
1445                    let is_relative = vim.mode != Mode::Insert;
1446                    editor.set_relative_line_number(Some(is_relative), cx)
1447                });
1448            }
1449        }
1450        Vim::globals(cx).focused_vim = Some(cx.entity().downgrade());
1451    }
1452
1453    fn blurred(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1454        self.stop_recording_immediately(NormalBefore.boxed_clone(), cx);
1455        self.store_visual_marks(window, cx);
1456        self.clear_operator(window, cx);
1457        self.update_editor(cx, |vim, editor, cx| {
1458            if vim.cursor_shape(cx) == CursorShape::Block {
1459                editor.set_cursor_shape(CursorShape::Hollow, cx);
1460            }
1461        });
1462    }
1463
1464    fn cursor_shape_changed(&mut self, _: &mut Window, cx: &mut Context<Self>) {
1465        self.update_editor(cx, |vim, editor, cx| {
1466            editor.set_cursor_shape(vim.cursor_shape(cx), cx);
1467        });
1468    }
1469
1470    fn update_editor<S>(
1471        &mut self,
1472        cx: &mut Context<Self>,
1473        update: impl FnOnce(&mut Self, &mut Editor, &mut Context<Editor>) -> S,
1474    ) -> Option<S> {
1475        let editor = self.editor.upgrade()?;
1476        Some(editor.update(cx, |editor, cx| update(self, editor, cx)))
1477    }
1478
1479    fn editor_selections(&mut self, _: &mut Window, cx: &mut Context<Self>) -> Vec<Range<Anchor>> {
1480        self.update_editor(cx, |_, editor, _| {
1481            editor
1482                .selections
1483                .disjoint_anchors_arc()
1484                .iter()
1485                .map(|selection| selection.tail()..selection.head())
1486                .collect()
1487        })
1488        .unwrap_or_default()
1489    }
1490
1491    fn editor_cursor_word(
1492        &mut self,
1493        window: &mut Window,
1494        cx: &mut Context<Self>,
1495    ) -> Option<String> {
1496        self.update_editor(cx, |_, editor, cx| {
1497            let snapshot = &editor.snapshot(window, cx);
1498            let selection = editor
1499                .selections
1500                .newest::<MultiBufferOffset>(&snapshot.display_snapshot);
1501
1502            let snapshot = snapshot.buffer_snapshot();
1503            let (range, kind) =
1504                snapshot.surrounding_word(selection.start, Some(CharScopeContext::Completion));
1505            if kind == Some(CharKind::Word) {
1506                let text: String = snapshot.text_for_range(range).collect();
1507                if !text.trim().is_empty() {
1508                    return Some(text);
1509                }
1510            }
1511
1512            None
1513        })
1514        .unwrap_or_default()
1515    }
1516
1517    /// When doing an action that modifies the buffer, we start recording so that `.`
1518    /// will replay the action.
1519    pub fn start_recording(&mut self, cx: &mut Context<Self>) {
1520        Vim::update_globals(cx, |globals, cx| {
1521            if !globals.dot_replaying {
1522                globals.dot_recording = true;
1523                globals.recording_actions = Default::default();
1524                globals.recording_count = None;
1525
1526                let selections = self.editor().map(|editor| {
1527                    editor.update(cx, |editor, cx| {
1528                        let snapshot = editor.display_snapshot(cx);
1529
1530                        (
1531                            editor.selections.oldest::<Point>(&snapshot),
1532                            editor.selections.newest::<Point>(&snapshot),
1533                        )
1534                    })
1535                });
1536
1537                if let Some((oldest, newest)) = selections {
1538                    globals.recorded_selection = match self.mode {
1539                        Mode::Visual if newest.end.row == newest.start.row => {
1540                            RecordedSelection::SingleLine {
1541                                cols: newest.end.column - newest.start.column,
1542                            }
1543                        }
1544                        Mode::Visual => RecordedSelection::Visual {
1545                            rows: newest.end.row - newest.start.row,
1546                            cols: newest.end.column,
1547                        },
1548                        Mode::VisualLine => RecordedSelection::VisualLine {
1549                            rows: newest.end.row - newest.start.row,
1550                        },
1551                        Mode::VisualBlock => RecordedSelection::VisualBlock {
1552                            rows: newest.end.row.abs_diff(oldest.start.row),
1553                            cols: newest.end.column.abs_diff(oldest.start.column),
1554                        },
1555                        _ => RecordedSelection::None,
1556                    }
1557                } else {
1558                    globals.recorded_selection = RecordedSelection::None;
1559                }
1560            }
1561        })
1562    }
1563
1564    pub fn stop_replaying(&mut self, cx: &mut Context<Self>) {
1565        let globals = Vim::globals(cx);
1566        globals.dot_replaying = false;
1567        if let Some(replayer) = globals.replayer.take() {
1568            replayer.stop();
1569        }
1570    }
1571
1572    /// When finishing an action that modifies the buffer, stop recording.
1573    /// as you usually call this within a keystroke handler we also ensure that
1574    /// the current action is recorded.
1575    pub fn stop_recording(&mut self, cx: &mut Context<Self>) {
1576        let globals = Vim::globals(cx);
1577        if globals.dot_recording {
1578            globals.stop_recording_after_next_action = true;
1579        }
1580        self.exit_temporary_mode = self.temp_mode;
1581    }
1582
1583    /// Stops recording actions immediately rather than waiting until after the
1584    /// next action to stop recording.
1585    ///
1586    /// This doesn't include the current action.
1587    pub fn stop_recording_immediately(&mut self, action: Box<dyn Action>, cx: &mut Context<Self>) {
1588        let globals = Vim::globals(cx);
1589        if globals.dot_recording {
1590            globals
1591                .recording_actions
1592                .push(ReplayableAction::Action(action.boxed_clone()));
1593            globals.recorded_actions = mem::take(&mut globals.recording_actions);
1594            globals.recorded_count = globals.recording_count.take();
1595            globals.dot_recording = false;
1596            globals.stop_recording_after_next_action = false;
1597        }
1598        self.exit_temporary_mode = self.temp_mode;
1599    }
1600
1601    /// Explicitly record one action (equivalents to start_recording and stop_recording)
1602    pub fn record_current_action(&mut self, cx: &mut Context<Self>) {
1603        self.start_recording(cx);
1604        self.stop_recording(cx);
1605    }
1606
1607    fn push_count_digit(&mut self, number: usize, window: &mut Window, cx: &mut Context<Self>) {
1608        if self.active_operator().is_some() {
1609            let post_count = Vim::globals(cx).post_count.unwrap_or(0);
1610
1611            Vim::globals(cx).post_count = Some(
1612                post_count
1613                    .checked_mul(10)
1614                    .and_then(|post_count| post_count.checked_add(number))
1615                    .filter(|post_count| *post_count < isize::MAX as usize)
1616                    .unwrap_or(post_count),
1617            )
1618        } else {
1619            let pre_count = Vim::globals(cx).pre_count.unwrap_or(0);
1620
1621            Vim::globals(cx).pre_count = Some(
1622                pre_count
1623                    .checked_mul(10)
1624                    .and_then(|pre_count| pre_count.checked_add(number))
1625                    .filter(|pre_count| *pre_count < isize::MAX as usize)
1626                    .unwrap_or(pre_count),
1627            )
1628        }
1629        // update the keymap so that 0 works
1630        self.sync_vim_settings(window, cx)
1631    }
1632
1633    fn select_register(&mut self, register: Arc<str>, window: &mut Window, cx: &mut Context<Self>) {
1634        if register.chars().count() == 1 {
1635            self.selected_register
1636                .replace(register.chars().next().unwrap());
1637        }
1638        self.operator_stack.clear();
1639        self.sync_vim_settings(window, cx);
1640    }
1641
1642    fn maybe_pop_operator(&mut self) -> Option<Operator> {
1643        self.operator_stack.pop()
1644    }
1645
1646    fn pop_operator(&mut self, window: &mut Window, cx: &mut Context<Self>) -> Operator {
1647        let popped_operator = self.operator_stack.pop()
1648            .expect("Operator popped when no operator was on the stack. This likely means there is an invalid keymap config");
1649        self.sync_vim_settings(window, cx);
1650        popped_operator
1651    }
1652
1653    fn clear_operator(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1654        Vim::take_count(cx);
1655        Vim::take_forced_motion(cx);
1656        self.selected_register.take();
1657        self.operator_stack.clear();
1658        self.sync_vim_settings(window, cx);
1659    }
1660
1661    fn active_operator(&self) -> Option<Operator> {
1662        self.operator_stack.last().cloned()
1663    }
1664
1665    fn transaction_begun(
1666        &mut self,
1667        transaction_id: TransactionId,
1668        _window: &mut Window,
1669        _: &mut Context<Self>,
1670    ) {
1671        let mode = if (self.mode == Mode::Insert
1672            || self.mode == Mode::Replace
1673            || self.mode == Mode::Normal)
1674            && self.current_tx.is_none()
1675        {
1676            self.current_tx = Some(transaction_id);
1677            self.last_mode
1678        } else {
1679            self.mode
1680        };
1681        if mode == Mode::VisualLine || mode == Mode::VisualBlock {
1682            self.undo_modes.insert(transaction_id, mode);
1683        }
1684    }
1685
1686    fn transaction_undone(
1687        &mut self,
1688        transaction_id: &TransactionId,
1689        window: &mut Window,
1690        cx: &mut Context<Self>,
1691    ) {
1692        match self.mode {
1693            Mode::VisualLine | Mode::VisualBlock | Mode::Visual | Mode::HelixSelect => {
1694                self.update_editor(cx, |vim, editor, cx| {
1695                    let original_mode = vim.undo_modes.get(transaction_id);
1696                    editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1697                        match original_mode {
1698                            Some(Mode::VisualLine) => {
1699                                s.move_with(|map, selection| {
1700                                    selection.collapse_to(
1701                                        map.prev_line_boundary(selection.start.to_point(map)).1,
1702                                        SelectionGoal::None,
1703                                    )
1704                                });
1705                            }
1706                            Some(Mode::VisualBlock) => {
1707                                let mut first = s.first_anchor();
1708                                first.collapse_to(first.start, first.goal);
1709                                s.select_anchors(vec![first]);
1710                            }
1711                            _ => {
1712                                s.move_with(|map, selection| {
1713                                    selection.collapse_to(
1714                                        map.clip_at_line_end(selection.start),
1715                                        selection.goal,
1716                                    );
1717                                });
1718                            }
1719                        }
1720                    });
1721                });
1722                self.switch_mode(Mode::Normal, true, window, cx)
1723            }
1724            Mode::Normal => {
1725                self.update_editor(cx, |_, editor, cx| {
1726                    editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1727                        s.move_with(|map, selection| {
1728                            selection
1729                                .collapse_to(map.clip_at_line_end(selection.end), selection.goal)
1730                        })
1731                    })
1732                });
1733            }
1734            Mode::Insert | Mode::Replace | Mode::HelixNormal => {}
1735        }
1736    }
1737
1738    fn local_selections_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1739        let Some(editor) = self.editor() else { return };
1740
1741        if editor.read(cx).leader_id().is_some() {
1742            return;
1743        }
1744
1745        let newest = editor.read(cx).selections.newest_anchor().clone();
1746        let is_multicursor = editor.read(cx).selections.count() > 1;
1747        if self.mode == Mode::Insert && self.current_tx.is_some() {
1748            if self.current_anchor.is_none() {
1749                self.current_anchor = Some(newest);
1750            } else if self.current_anchor.as_ref().unwrap() != &newest
1751                && let Some(tx_id) = self.current_tx.take()
1752            {
1753                self.update_editor(cx, |_, editor, cx| {
1754                    editor.group_until_transaction(tx_id, cx)
1755                });
1756            }
1757        } else if self.mode == Mode::Normal && newest.start != newest.end {
1758            if matches!(newest.goal, SelectionGoal::HorizontalRange { .. }) {
1759                self.switch_mode(Mode::VisualBlock, false, window, cx);
1760            } else {
1761                self.switch_mode(Mode::Visual, false, window, cx)
1762            }
1763        } else if newest.start == newest.end
1764            && !is_multicursor
1765            && [Mode::Visual, Mode::VisualLine, Mode::VisualBlock].contains(&self.mode)
1766        {
1767            self.switch_mode(Mode::Normal, false, window, cx);
1768        }
1769    }
1770
1771    fn input_ignored(&mut self, text: Arc<str>, window: &mut Window, cx: &mut Context<Self>) {
1772        if text.is_empty() {
1773            return;
1774        }
1775
1776        match self.active_operator() {
1777            Some(Operator::FindForward { before, multiline }) => {
1778                let find = Motion::FindForward {
1779                    before,
1780                    char: text.chars().next().unwrap(),
1781                    mode: if multiline {
1782                        FindRange::MultiLine
1783                    } else {
1784                        FindRange::SingleLine
1785                    },
1786                    smartcase: VimSettings::get_global(cx).use_smartcase_find,
1787                };
1788                Vim::globals(cx).last_find = Some(find.clone());
1789                self.motion(find, window, cx)
1790            }
1791            Some(Operator::FindBackward { after, multiline }) => {
1792                let find = Motion::FindBackward {
1793                    after,
1794                    char: text.chars().next().unwrap(),
1795                    mode: if multiline {
1796                        FindRange::MultiLine
1797                    } else {
1798                        FindRange::SingleLine
1799                    },
1800                    smartcase: VimSettings::get_global(cx).use_smartcase_find,
1801                };
1802                Vim::globals(cx).last_find = Some(find.clone());
1803                self.motion(find, window, cx)
1804            }
1805            Some(Operator::Sneak { first_char }) => {
1806                if let Some(first_char) = first_char {
1807                    if let Some(second_char) = text.chars().next() {
1808                        let sneak = Motion::Sneak {
1809                            first_char,
1810                            second_char,
1811                            smartcase: VimSettings::get_global(cx).use_smartcase_find,
1812                        };
1813                        Vim::globals(cx).last_find = Some(sneak.clone());
1814                        self.motion(sneak, window, cx)
1815                    }
1816                } else {
1817                    let first_char = text.chars().next();
1818                    self.pop_operator(window, cx);
1819                    self.push_operator(Operator::Sneak { first_char }, window, cx);
1820                }
1821            }
1822            Some(Operator::SneakBackward { first_char }) => {
1823                if let Some(first_char) = first_char {
1824                    if let Some(second_char) = text.chars().next() {
1825                        let sneak = Motion::SneakBackward {
1826                            first_char,
1827                            second_char,
1828                            smartcase: VimSettings::get_global(cx).use_smartcase_find,
1829                        };
1830                        Vim::globals(cx).last_find = Some(sneak.clone());
1831                        self.motion(sneak, window, cx)
1832                    }
1833                } else {
1834                    let first_char = text.chars().next();
1835                    self.pop_operator(window, cx);
1836                    self.push_operator(Operator::SneakBackward { first_char }, window, cx);
1837                }
1838            }
1839            Some(Operator::Replace) => match self.mode {
1840                Mode::Normal => self.normal_replace(text, window, cx),
1841                Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
1842                    self.visual_replace(text, window, cx)
1843                }
1844                Mode::HelixNormal => self.helix_replace(&text, window, cx),
1845                _ => self.clear_operator(window, cx),
1846            },
1847            Some(Operator::Digraph { first_char }) => {
1848                if let Some(first_char) = first_char {
1849                    if let Some(second_char) = text.chars().next() {
1850                        self.insert_digraph(first_char, second_char, window, cx);
1851                    }
1852                } else {
1853                    let first_char = text.chars().next();
1854                    self.pop_operator(window, cx);
1855                    self.push_operator(Operator::Digraph { first_char }, window, cx);
1856                }
1857            }
1858            Some(Operator::Literal { prefix }) => {
1859                self.handle_literal_input(prefix.unwrap_or_default(), &text, window, cx)
1860            }
1861            Some(Operator::AddSurrounds { target }) => match self.mode {
1862                Mode::Normal => {
1863                    if let Some(target) = target {
1864                        self.add_surrounds(text, target, window, cx);
1865                        self.clear_operator(window, cx);
1866                    }
1867                }
1868                Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
1869                    self.add_surrounds(text, SurroundsType::Selection, window, cx);
1870                    self.clear_operator(window, cx);
1871                }
1872                _ => self.clear_operator(window, cx),
1873            },
1874            Some(Operator::ChangeSurrounds { target, opening }) => match self.mode {
1875                Mode::Normal => {
1876                    if let Some(target) = target {
1877                        self.change_surrounds(text, target, opening, window, cx);
1878                        self.clear_operator(window, cx);
1879                    }
1880                }
1881                _ => self.clear_operator(window, cx),
1882            },
1883            Some(Operator::DeleteSurrounds) => match self.mode {
1884                Mode::Normal => {
1885                    self.delete_surrounds(text, window, cx);
1886                    self.clear_operator(window, cx);
1887                }
1888                _ => self.clear_operator(window, cx),
1889            },
1890            Some(Operator::Mark) => self.create_mark(text, window, cx),
1891            Some(Operator::RecordRegister) => {
1892                self.record_register(text.chars().next().unwrap(), window, cx)
1893            }
1894            Some(Operator::ReplayRegister) => {
1895                self.replay_register(text.chars().next().unwrap(), window, cx)
1896            }
1897            Some(Operator::Register) => match self.mode {
1898                Mode::Insert => {
1899                    self.update_editor(cx, |_, editor, cx| {
1900                        if let Some(register) = Vim::update_globals(cx, |globals, cx| {
1901                            globals.read_register(text.chars().next(), Some(editor), cx)
1902                        }) {
1903                            editor.do_paste(
1904                                &register.text.to_string(),
1905                                register.clipboard_selections,
1906                                false,
1907                                window,
1908                                cx,
1909                            )
1910                        }
1911                    });
1912                    self.clear_operator(window, cx);
1913                }
1914                _ => {
1915                    self.select_register(text, window, cx);
1916                }
1917            },
1918            Some(Operator::Jump { line }) => self.jump(text, line, true, window, cx),
1919            _ => {
1920                if self.mode == Mode::Replace {
1921                    self.multi_replace(text, window, cx)
1922                }
1923
1924                if self.mode == Mode::Normal {
1925                    self.update_editor(cx, |_, editor, cx| {
1926                        editor.accept_edit_prediction(
1927                            &editor::actions::AcceptEditPrediction {},
1928                            window,
1929                            cx,
1930                        );
1931                    });
1932                }
1933            }
1934        }
1935    }
1936
1937    fn sync_vim_settings(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1938        self.update_editor(cx, |vim, editor, cx| {
1939            editor.set_cursor_shape(vim.cursor_shape(cx), cx);
1940            editor.set_clip_at_line_ends(vim.clip_at_line_ends(), cx);
1941            let collapse_matches = !HelixModeSetting::get_global(cx).0;
1942            editor.set_collapse_matches(collapse_matches);
1943            editor.set_input_enabled(vim.editor_input_enabled());
1944            editor.set_autoindent(vim.should_autoindent());
1945            editor
1946                .selections
1947                .set_line_mode(matches!(vim.mode, Mode::VisualLine));
1948
1949            let hide_edit_predictions = !matches!(vim.mode, Mode::Insert | Mode::Replace);
1950            editor.set_edit_predictions_hidden_for_vim_mode(hide_edit_predictions, window, cx);
1951        });
1952        cx.notify()
1953    }
1954}
1955
1956#[derive(RegisterSetting)]
1957struct VimSettings {
1958    pub default_mode: Mode,
1959    pub toggle_relative_line_numbers: bool,
1960    pub use_system_clipboard: settings::UseSystemClipboard,
1961    pub use_smartcase_find: bool,
1962    pub custom_digraphs: HashMap<String, Arc<str>>,
1963    pub highlight_on_yank_duration: u64,
1964    pub cursor_shape: CursorShapeSettings,
1965}
1966
1967/// The settings for cursor shape.
1968#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1969pub struct CursorShapeSettings {
1970    /// Cursor shape for the normal mode.
1971    ///
1972    /// Default: block
1973    pub normal: Option<CursorShape>,
1974    /// Cursor shape for the replace mode.
1975    ///
1976    /// Default: underline
1977    pub replace: Option<CursorShape>,
1978    /// Cursor shape for the visual mode.
1979    ///
1980    /// Default: block
1981    pub visual: Option<CursorShape>,
1982    /// Cursor shape for the insert mode.
1983    ///
1984    /// The default value follows the primary cursor_shape.
1985    pub insert: Option<CursorShape>,
1986}
1987
1988impl From<settings::CursorShapeSettings> for CursorShapeSettings {
1989    fn from(settings: settings::CursorShapeSettings) -> Self {
1990        Self {
1991            normal: settings.normal.map(Into::into),
1992            replace: settings.replace.map(Into::into),
1993            visual: settings.visual.map(Into::into),
1994            insert: settings.insert.map(Into::into),
1995        }
1996    }
1997}
1998
1999impl From<settings::ModeContent> for Mode {
2000    fn from(mode: ModeContent) -> Self {
2001        match mode {
2002            ModeContent::Normal => Self::Normal,
2003            ModeContent::Insert => Self::Insert,
2004        }
2005    }
2006}
2007
2008impl Settings for VimSettings {
2009    fn from_settings(content: &settings::SettingsContent) -> Self {
2010        let vim = content.vim.clone().unwrap();
2011        Self {
2012            default_mode: vim.default_mode.unwrap().into(),
2013            toggle_relative_line_numbers: vim.toggle_relative_line_numbers.unwrap(),
2014            use_system_clipboard: vim.use_system_clipboard.unwrap(),
2015            use_smartcase_find: vim.use_smartcase_find.unwrap(),
2016            custom_digraphs: vim.custom_digraphs.unwrap(),
2017            highlight_on_yank_duration: vim.highlight_on_yank_duration.unwrap(),
2018            cursor_shape: vim.cursor_shape.unwrap().into(),
2019        }
2020    }
2021}