From 42d07b0621f034049ba3872122144ea9e69092eb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= <blaz@mxxn.io>
Date: Fri, 19 Mar 2021 18:01:08 +0900
Subject: [PATCH] Implement replace command (r<key>).

---
 helix-term/src/commands.rs | 23 +++++++++++++++++++++++
 helix-term/src/keymap.rs   |  2 ++
 2 files changed, 25 insertions(+)

diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 3c337c50..fc98f712 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -380,6 +380,29 @@ pub fn extend_prev_char(cx: &mut Context) {
     )
 }
 
+pub fn replace(cx: &mut Context) {
+    // need to wait for next key
+    cx.on_next_key(move |cx, event| {
+        if let KeyEvent {
+            code: KeyCode::Char(ch),
+            ..
+        } = event
+        {
+            let text = Tendril::from_char(ch);
+
+            let mut doc = cx.doc();
+
+            let transaction =
+                Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
+                    (range.from(), range.to() + 1, Some(text.clone()))
+                });
+
+            doc.apply(&transaction);
+            doc.append_changes_to_history();
+        }
+    })
+}
+
 fn scroll(view: &mut View, offset: usize, direction: Direction) {
     use Direction::*;
     // we use short lived borrows since view's methods read from doc too
diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs
index d9fe348f..5a39b774 100644
--- a/helix-term/src/keymap.rs
+++ b/helix-term/src/keymap.rs
@@ -141,6 +141,8 @@ pub fn default() -> Keymaps {
         shift!('T') => commands::till_prev_char,
         shift!('F') => commands::find_prev_char,
         // and matching set for select mode (extend)
+        //
+        key!('r') => commands::replace,
 
         key!('0') => commands::move_line_start,
         key!('$') => commands::move_line_end,