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