From 4d22454386d52d14f626b209016a71f119cc1cbf Mon Sep 17 00:00:00 2001
From: Bob <QiBaobin@users.noreply.github.com>
Date: Thu, 11 Nov 2021 10:32:23 +0800
Subject: [PATCH] add wonly -- window only (#1057)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* add wonly

* Update book/src/keymap.md

Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>

* add `wonly` to space w mode too

* remove the TODO

Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>
---
 book/src/keymap.md         |  1 +
 helix-term/src/commands.rs | 15 +++++++++++++++
 helix-term/src/keymap.rs   |  2 ++
 3 files changed, 18 insertions(+)

diff --git a/book/src/keymap.md b/book/src/keymap.md
index 212ed496..901c8471 100644
--- a/book/src/keymap.md
+++ b/book/src/keymap.md
@@ -194,6 +194,7 @@ This layer is similar to vim keybindings as kakoune does not support window.
 | `k`, `Ctrl-k`, `up`    | Move to split above            | `jump_view_up`    |
 | `l`, `Ctrl-l`, `right` | Move to right split            | `jump_view_right` |
 | `q`, `Ctrl-q`          | Close current window           | `wclose`          |
+| `o`, `Ctrl-o`          | Only keep the current window, closing all the others            | `wonly`           |
 
 #### Space mode
 
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 489308d8..089f92f1 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -320,6 +320,7 @@ impl Command {
         hsplit, "Horizontal bottom split",
         vsplit, "Vertical right split",
         wclose, "Close window",
+        wonly, "Current window only",
         select_register, "Select register",
         align_view_middle, "Align view middle",
         align_view_top, "Align view top",
@@ -4723,6 +4724,20 @@ fn wclose(cx: &mut Context) {
     cx.editor.close(view_id, /* close_buffer */ false);
 }
 
+fn wonly(cx: &mut Context) {
+    let views = cx
+        .editor
+        .tree
+        .views()
+        .map(|(v, focus)| (v.id, focus))
+        .collect::<Vec<_>>();
+    for (view_id, focus) in views {
+        if !focus {
+            cx.editor.close(view_id, /* close_buffer */ false);
+        }
+    }
+}
+
 fn select_register(cx: &mut Context) {
     cx.on_next_key(move |cx, event| {
         if let Some(ch) = event.char() {
diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs
index 7bed3ddb..d7040b88 100644
--- a/helix-term/src/keymap.rs
+++ b/helix-term/src/keymap.rs
@@ -583,6 +583,7 @@ impl Default for Keymaps {
                 "C-s" | "s" => hsplit,
                 "C-v" | "v" => vsplit,
                 "C-q" | "q" => wclose,
+                "C-o" | "o" => wonly,
                 "C-h" | "h" | "left" => jump_view_left,
                 "C-j" | "j" | "down" => jump_view_down,
                 "C-k" | "k" | "up" => jump_view_up,
@@ -609,6 +610,7 @@ impl Default for Keymaps {
                     "C-s" | "s" => hsplit,
                     "C-v" | "v" => vsplit,
                     "C-q" | "q" => wclose,
+                    "C-o" | "o" => wonly,
                     "C-h" | "h" | "left" => jump_view_left,
                     "C-j" | "j" | "down" => jump_view_down,
                     "C-k" | "k" | "up" => jump_view_up,