on master help sign in
Add typed commands buffer-next and buffer-previous (#1940)
9782204Dr. David A. Kunz
book/src/generated/typable-cmd.md+2
@@ -9,6 +9,8 @@
9| `:buffer-close-others!`, `:bco!`, `:bcloseother!` | Close all buffers but the currently focused one. |
9| `:buffer-close-others!`, `:bco!`, `:bcloseother!` | Close all buffers but the currently focused one. |
10| `:buffer-close-all`, `:bca`, `:bcloseall` | Close all buffers, without quiting. |
10| `:buffer-close-all`, `:bca`, `:bcloseall` | Close all buffers, without quiting. |
11| `:buffer-close-all!`, `:bca!`, `:bcloseall!` | Close all buffers forcefully (ignoring unsaved changes), without quiting. |
11| `:buffer-close-all!`, `:bca!`, `:bcloseall!` | Close all buffers forcefully (ignoring unsaved changes), without quiting. |
12+| `:buffer-next`, `:bn`, `:bnext` | Go to next buffer. |
13+| `:buffer-previous`, `:bp`, `:bprev` | Go to previous buffer. |
12| `:write`, `:w` | Write changes to disk. Accepts an optional path (:write some/path.txt) |
14| `:write`, `:w` | Write changes to disk. Accepts an optional path (:write some/path.txt) |
13| `:new`, `:n` | Create a new scratch buffer. |
15| `:new`, `:n` | Create a new scratch buffer. |
14| `:format`, `:fmt` | Format the file using the LSP formatter. |
16| `:format`, `:fmt` | Format the file using the LSP formatter. |
helix-term/src/commands.rs+9−10
@@ -637,36 +637,35 @@ fn goto_line_start(cx: &mut Context) {
637}
637}
638
638
639fn goto_next_buffer(cx: &mut Context) {
639fn goto_next_buffer(cx: &mut Context) {
640− goto_buffer(cx, Direction::Forward);
640+ goto_buffer(cx.editor, Direction::Forward);
641}
641}
642
642
643fn goto_previous_buffer(cx: &mut Context) {
643fn goto_previous_buffer(cx: &mut Context) {
644− goto_buffer(cx, Direction::Backward);
644+ goto_buffer(cx.editor, Direction::Backward);
645}
645}
646
646
647−fn goto_buffer(cx: &mut Context, direction: Direction) {
647+fn goto_buffer(editor: &mut Editor, direction: Direction) {
648− let current = view!(cx.editor).doc;
648+ let current = view!(editor).doc;
649
649
650 let id = match direction {
650 let id = match direction {
651 Direction::Forward => {
651 Direction::Forward => {
652− let iter = cx.editor.documents.keys();
652+ let iter = editor.documents.keys();
653 let mut iter = iter.skip_while(|id| *id != &current);
653 let mut iter = iter.skip_while(|id| *id != &current);
654 iter.next(); // skip current item
654 iter.next(); // skip current item
655− iter.next().or_else(|| cx.editor.documents.keys().next())
655+ iter.next().or_else(|| editor.documents.keys().next())
656 }
656 }
657 Direction::Backward => {
657 Direction::Backward => {
658− let iter = cx.editor.documents.keys();
658+ let iter = editor.documents.keys();
659 let mut iter = iter.rev().skip_while(|id| *id != &current);
659 let mut iter = iter.rev().skip_while(|id| *id != &current);
660 iter.next(); // skip current item
660 iter.next(); // skip current item
661− iter.next()
662− .or_else(|| cx.editor.documents.keys().rev().next())
661+ iter.next().or_else(|| editor.documents.keys().rev().next())
663 }
662 }
664 }
663 }
665 .unwrap();
664 .unwrap();
666
665
667 let id = *id;
666 let id = *id;
668
667
669− cx.editor.switch(id, Action::Replace);
668+ editor.switch(id, Action::Replace);
670}
669}
671
670
672fn extend_to_line_start(cx: &mut Context) {
671fn extend_to_line_start(cx: &mut Context) {
helix-term/src/commands/typed.rs+32
@@ -172,6 +172,24 @@ fn force_buffer_close_all(
172 buffer_close_by_ids_impl(cx.editor, &document_ids, true)
172 buffer_close_by_ids_impl(cx.editor, &document_ids, true)
173}
173}
174
174
175+fn buffer_next(
176+ cx: &mut compositor::Context,
177+ _args: &[Cow<str>],
178+ _event: PromptEvent,
179+) -> anyhow::Result<()> {
180+ goto_buffer(cx.editor, Direction::Forward);
181+ Ok(())
182+}
183+
184+fn buffer_previous(
185+ cx: &mut compositor::Context,
186+ _args: &[Cow<str>],
187+ _event: PromptEvent,
188+) -> anyhow::Result<()> {
189+ goto_buffer(cx.editor, Direction::Backward);
190+ Ok(())
191+}
192+
175fn write_impl(cx: &mut compositor::Context, path: Option<&Cow<str>>) -> anyhow::Result<()> {
193fn write_impl(cx: &mut compositor::Context, path: Option<&Cow<str>>) -> anyhow::Result<()> {
176 let jobs = &mut cx.jobs;
194 let jobs = &mut cx.jobs;
177 let doc = doc_mut!(cx.editor);
195 let doc = doc_mut!(cx.editor);
@@ -1082,6 +1100,20 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
1082 fun: force_buffer_close_all,
1100 fun: force_buffer_close_all,
1083 completer: None,
1101 completer: None,
1084 },
1102 },
1103+ TypableCommand {
1104+ name: "buffer-next",
1105+ aliases: &["bn", "bnext"],
1106+ doc: "Go to next buffer.",
1107+ fun: buffer_next,
1108+ completer: None,
1109+ },
1110+ TypableCommand {
1111+ name: "buffer-previous",
1112+ aliases: &["bp", "bprev"],
1113+ doc: "Go to previous buffer.",
1114+ fun: buffer_previous,
1115+ completer: None,
1116+ },
1085 TypableCommand {
1117 TypableCommand {
1086 name: "write",
1118 name: "write",
1087 aliases: &["w"],
1119 aliases: &["w"],