Refactor nextbuffer_cmd

This commit is contained in:
Eike Plack 2015-04-15 02:43:16 +02:00
parent 0f9b210e3f
commit 2cf45ddb21

View File

@ -377,34 +377,59 @@ const CommandDesc buffer_cmd = {
} }
}; };
const CommandDesc nextbuffer_cmd = { template<bool next>
"nextbuffer", void cycle_buffer(const ParametersParser& parser, Context& context)
"nb", {
"nextbuffer: move to the next buffer in the list", Buffer* oldbuf = &context.buffer();
auto it = find_if(BufferManager::instance(),
[oldbuf](const SafePtr<Buffer>& lhs)
{ return lhs.get() == oldbuf; });
kak_assert(it != BufferManager::instance().end());
if (not next)
{
if (it == BufferManager::instance().begin())
it = BufferManager::instance().end();
it = std::prev(it,1);
}
else
{
if (++it == BufferManager::instance().end())
it = BufferManager::instance().begin();
}
Buffer* newbuf = it->get();
if (newbuf != oldbuf)
{
BufferManager::instance().set_last_used_buffer(*oldbuf);
context.push_jump();
context.change_buffer(*newbuf);
}
}
const CommandDesc buffernext_cmd = {
"buffernext",
"bn",
"buffernext: move to the next buffer in the list",
no_params, no_params,
CommandFlags::None, CommandFlags::None,
CommandHelper{}, CommandHelper{},
CommandCompleter{}, CommandCompleter{},
[](const ParametersParser& parser, Context& context) cycle_buffer<true>
{ };
Buffer* oldbuf = &context.buffer();
auto it = find_if(BufferManager::instance(),
[oldbuf](const SafePtr<Buffer>& lhs)
{ return lhs.get() == oldbuf; });
kak_assert(it != BufferManager::instance().end()); const CommandDesc bufferprev_cmd = {
if (++it == BufferManager::instance().end()) "bufferprev",
it = BufferManager::instance().begin(); "bp",
"bufferprev: move to the previous buffer in the list",
Buffer* newbuf = it->get(); no_params,
CommandFlags::None,
if (newbuf != oldbuf) CommandHelper{},
{ CommandCompleter{},
BufferManager::instance().set_last_used_buffer(*oldbuf); cycle_buffer<false>
context.push_jump();
context.change_buffer(*newbuf);
}
}
}; };
template<bool force> template<bool force>
@ -1549,7 +1574,8 @@ void register_commands()
register_command(write_quit_cmd); register_command(write_quit_cmd);
register_command(force_write_quit_cmd); register_command(force_write_quit_cmd);
register_command(buffer_cmd); register_command(buffer_cmd);
register_command(nextbuffer_cmd); register_command(buffernext_cmd);
register_command(bufferprev_cmd);
register_command(delbuf_cmd); register_command(delbuf_cmd);
register_command(force_delbuf_cmd); register_command(force_delbuf_cmd);
register_command(namebuf_cmd); register_command(namebuf_cmd);