From a0cf2a7edd32893b29b5d7206bf617ae28deb4fc Mon Sep 17 00:00:00 2001 From: pagedmov Date: Fri, 20 Feb 2026 12:33:18 -0500 Subject: [PATCH] Cursor now moved to end of the line while scrolling history, original position in pending command still remembered -m Undo now moves the cursor to it's original position in normal mode --- src/prompt/readline/history.rs | 7 ++++++- src/prompt/readline/linebuf.rs | 11 +++++++---- src/prompt/readline/mod.rs | 8 +++++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/prompt/readline/history.rs b/src/prompt/readline/history.rs index 81abb0b..c7cc473 100644 --- a/src/prompt/readline/history.rs +++ b/src/prompt/readline/history.rs @@ -271,13 +271,18 @@ impl History { } pub fn update_pending_cmd(&mut self, buf: (&str, usize)) { + let cursor_pos = if let Some(pending) = &self.pending { + pending.1 + } else { + buf.1 + }; let cmd = buf.0.to_string(); let constraint = SearchConstraint { kind: SearchKind::Prefix, term: cmd.clone(), }; - self.pending = Some((cmd, buf.1)); + self.pending = Some((cmd, cursor_pos)); self.constrain_entries(constraint); } diff --git a/src/prompt/readline/linebuf.rs b/src/prompt/readline/linebuf.rs index 2a8b0f2..4d38ee1 100644 --- a/src/prompt/readline/linebuf.rs +++ b/src/prompt/readline/linebuf.rs @@ -384,6 +384,12 @@ impl LineBuf { pub fn set_cursor_clamp(&mut self, yn: bool) { self.cursor.exclusive = yn; } + pub fn move_cursor_to_end(&mut self) { + self.move_cursor(MotionKind::To(self.grapheme_indices().len())) + } + pub fn move_cursor_to_start(&mut self) { + self.move_cursor(MotionKind::To(0)) + } pub fn cursor_byte_pos(&mut self) -> usize { self.index_byte_pos(self.cursor.get()) } @@ -2555,11 +2561,8 @@ impl LineBuf { self.buffer.replace_range(pos..pos + new.len(), &old); let new_cursor_pos = self.cursor.get(); - let in_insert_mode = !self.cursor.exclusive; - if in_insert_mode { - self.cursor.set(cursor_pos) - } + self.cursor.set(cursor_pos); let new_edit = Edit { pos, cursor_pos: new_cursor_pos, diff --git a/src/prompt/readline/mod.rs b/src/prompt/readline/mod.rs index 5362fda..c753517 100644 --- a/src/prompt/readline/mod.rs +++ b/src/prompt/readline/mod.rs @@ -316,15 +316,17 @@ impl FernVi { let entry = self.history.scroll(count); log::info!("Scrolled history, got entry: {:?}", entry.as_ref()); if let Some(entry) = entry { - log::info!("Setting buffer to history entry: {}", entry.command()); + let cursor_pos = self.editor.cursor.get(); + log::info!("Saving pending command to history: {:?} at cursor pos {}", self.editor.as_str(), cursor_pos); let pending = self.editor.take_buf(); self.editor.set_buffer(entry.command().to_string()); if self.history.pending.is_none() { - self.history.pending = Some((pending, self.editor.cursor.get())); + self.history.pending = Some((pending, cursor_pos)); } self.editor.set_hint(None); + self.editor.move_cursor_to_end(); } else if let Some(pending) = self.history.pending.take() { - log::info!("Setting buffer to pending command: {}", &pending.0); + log::info!("Setting buffer to pending command: {:?}", &pending); self.editor.set_buffer(pending.0); self.editor.cursor.set(pending.1); self.editor.set_hint(None);