diff --git a/src/bridge/events.rs b/src/bridge/events.rs index 5e1f037..5cf7adb 100644 --- a/src/bridge/events.rs +++ b/src/bridge/events.rs @@ -114,6 +114,20 @@ pub enum WindowAnchor { SouthEast, } +#[derive(Debug)] +pub enum EditorMode { + // The set of modes reported will change in new versions of Nvim, for + // instance more sub-modes and temporary states might be represented as + // separate modes. (however we can safely do this as these are the main modes) + // for instance if we are in Terminal mode and even though status-line shows Terminal, + // we still get one of these as the _editor_ mode + Normal, + Insert, + Visual, + CmdLine, + Unknown(String), +} + #[derive(Debug)] pub enum RedrawEvent { SetTitle { @@ -126,6 +140,7 @@ pub enum RedrawEvent { gui_option: GuiOption, }, ModeChange { + mode: EditorMode, mode_index: u64, }, MouseOn, @@ -373,9 +388,17 @@ fn parse_option_set(option_set_arguments: Vec) -> Result { } fn parse_mode_change(mode_change_arguments: Vec) -> Result { - let [_mode, mode_index] = extract_values(mode_change_arguments, [Value::Nil, Value::Nil])?; + let [mode, mode_index] = extract_values(mode_change_arguments, [Value::Nil, Value::Nil])?; + let mode_name = parse_string(mode)?; Ok(RedrawEvent::ModeChange { + mode: match mode_name.as_str() { + "normal" => EditorMode::Normal, + "insert" => EditorMode::Insert, + "visual" => EditorMode::Visual, + "cmdline_normal" => EditorMode::CmdLine, + _ => EditorMode::Unknown(mode_name), + }, mode_index: parse_u64(mode_index)?, }) } diff --git a/src/editor/cursor.rs b/src/editor/cursor.rs index 5e35797..9f8bf10 100644 --- a/src/editor/cursor.rs +++ b/src/editor/cursor.rs @@ -43,7 +43,6 @@ pub struct Cursor { pub blinkoff: Option, pub style: Option>, pub enabled: bool, - pub mode_list: Vec, } impl Cursor { @@ -57,7 +56,6 @@ impl Cursor { blinkon: None, blinkoff: None, enabled: true, - mode_list: Vec::new(), } } @@ -85,28 +83,27 @@ impl Cursor { } } - pub fn change_mode(&mut self, mode_index: u64, styles: &HashMap>) { - if let Some(CursorMode { + pub fn change_mode(&mut self, cursor_mode: &CursorMode, styles: &HashMap>) { + let CursorMode { shape, style_id, cell_percentage, blinkwait, blinkon, blinkoff, - }) = self.mode_list.get(mode_index as usize) - { - if let Some(shape) = shape { - self.shape = shape.clone(); - } + } = cursor_mode; - if let Some(style_id) = style_id { - self.style = styles.get(style_id).cloned(); - } + if let Some(shape) = shape { + self.shape = shape.clone(); + } - self.cell_percentage = *cell_percentage; - self.blinkwait = *blinkwait; - self.blinkon = *blinkon; - self.blinkoff = *blinkoff; + if let Some(style_id) = style_id { + self.style = styles.get(style_id).cloned(); } + + self.cell_percentage = *cell_percentage; + self.blinkwait = *blinkwait; + self.blinkon = *blinkon; + self.blinkoff = *blinkoff; } } diff --git a/src/editor/mod.rs b/src/editor/mod.rs index db8f523..f1439be 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -10,7 +10,7 @@ use parking_lot::Mutex; use skulpin::skia_safe::colors; use unicode_segmentation::UnicodeSegmentation; -use crate::bridge::{GridLineCell, GuiOption, RedrawEvent}; +use crate::bridge::{EditorMode, GridLineCell, GuiOption, RedrawEvent}; use crate::redraw_scheduler::REDRAW_SCHEDULER; use crate::window::window_geometry_or_default; pub use cursor::{Cursor, CursorMode, CursorShape}; @@ -39,6 +39,8 @@ pub struct Editor { pub default_style: Arc