aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorath32021-11-01 19:52:47 +0000
committerBlaž Hrastnik2021-11-04 05:03:03 +0000
commit78c68fae91579ccda6f65e55f79316b01c5b654a (patch)
tree0286f2a51b94d0ca10ef087b7f9b88a612f128c1
parente2560f427ef5e75155071e39da342628f5d5896a (diff)
Implement "Goto next buffer / Goto previous buffer" commands
-rw-r--r--book/src/keymap.md2
-rw-r--r--helix-term/src/commands.rs29
-rw-r--r--helix-term/src/keymap.rs2
3 files changed, 33 insertions, 0 deletions
diff --git a/book/src/keymap.md b/book/src/keymap.md
index 4a6f80bb..6bcd09bc 100644
--- a/book/src/keymap.md
+++ b/book/src/keymap.md
@@ -161,6 +161,8 @@ Jumps to various locations.
| `r` | Go to references | `goto_reference` |
| `i` | Go to implementation | `goto_implementation` |
| `a` | Go to the last accessed/alternate file | `goto_last_accessed_file` |
+| `n` | Go to next buffer | `goto_next_buffer` |
+| `p` | Go to previous buffer | `goto_previous_buffer` |
#### Match mode
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 547a1d75..c1891baf 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -262,6 +262,8 @@ impl Command {
goto_prev_diag, "Goto previous diagnostic",
goto_line_start, "Goto line start",
goto_line_end, "Goto line end",
+ goto_next_buffer, "Goto next buffer",
+ goto_previous_buffer, "Goto previous buffer",
// TODO: different description ?
goto_line_end_newline, "Goto line end",
goto_first_nonwhitespace, "Goto first non-blank in line",
@@ -519,6 +521,33 @@ fn goto_line_start(cx: &mut Context) {
)
}
+fn goto_next_buffer(cx: &mut Context) {
+ goto_buffer(cx, Direction::Forward);
+}
+
+fn goto_previous_buffer(cx: &mut Context) {
+ goto_buffer(cx, Direction::Backward);
+}
+
+fn goto_buffer(cx: &mut Context, direction: Direction) {
+ let buf_cur = current!(cx.editor).1.id();
+
+ if let Some(pos) = cx.editor.documents.iter().position(|(id, _)| id == buf_cur) {
+ let goto_id = if direction == Direction::Forward {
+ if pos < cx.editor.documents.iter().count() - 1 {
+ cx.editor.documents.iter().nth(pos + 1).unwrap().0
+ } else {
+ cx.editor.documents.iter().next().unwrap().0
+ }
+ } else if pos > 0 {
+ cx.editor.documents.iter().nth(pos - 1).unwrap().0
+ } else {
+ cx.editor.documents.iter().last().unwrap().0
+ };
+ cx.editor.switch(goto_id, Action::Replace);
+ }
+}
+
fn extend_to_line_start(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
goto_line_start_impl(view, doc, Movement::Extend)
diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs
index 93f64fa4..b48eea14 100644
--- a/helix-term/src/keymap.rs
+++ b/helix-term/src/keymap.rs
@@ -453,6 +453,8 @@ impl Default for Keymaps {
"m" => goto_window_middle,
"b" => goto_window_bottom,
"a" => goto_last_accessed_file,
+ "n" => goto_next_buffer,
+ "p" => goto_previous_buffer,
},
":" => command_mode,