From ba59033935d643f4a7f50da35c7cd033c5790252 Mon Sep 17 00:00:00 2001 From: Jimmy Thrasher Date: Thu, 9 Apr 2015 10:14:28 -0400 Subject: [PATCH] Add nextbuffer command Wraps around at the end. If only 1 buffer loaded, does nothing. --- src/commands.cc | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/commands.cc b/src/commands.cc index e1cfcb10..1ba4a3a2 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -375,6 +375,54 @@ const CommandDesc buffer_cmd = { } }; +Buffer& next_buffer(Context& context) { + auto& buffer_manager = BufferManager::instance(); + if (buffer_manager.end()->get() == &context.buffer()) + { + return **buffer_manager.begin(); + } + else + { + bool found_current = false; + Buffer& current_buffer = context.buffer(); + for (auto& it : buffer_manager) + { + Buffer& buffer = *it; + if (&buffer == ¤t_buffer) + { + found_current = true; + } + else if (found_current) + { + return buffer; + } + } + + return **buffer_manager.begin(); + } +} + +const CommandDesc nextbuffer_cmd = { + "nextbuffer", + "nb", + "nextbuffer: move to the next buffer in the list", + no_params, + CommandFlags::None, + CommandHelper{}, + CommandCompleter{}, + [](const ParametersParser& parser, Context& context) + { + Buffer& nb = next_buffer(context); + BufferManager::instance().set_last_used_buffer(nb); + + if (&nb != &context.buffer()) + { + context.push_jump(); + context.change_buffer(nb); + } + } +}; + template void delete_buffer(const ParametersParser& parser, Context& context) { @@ -1497,6 +1545,7 @@ void register_commands() register_command(write_quit_cmd); register_command(force_write_quit_cmd); register_command(buffer_cmd); + register_command(nextbuffer_cmd); register_command(delbuf_cmd); register_command(force_delbuf_cmd); register_command(namebuf_cmd);