Merge remote-tracking branch 'ekie/next_buffer'

This commit is contained in:
Maxime Coste 2015-04-21 13:28:09 +01:00
commit 6e5e831ce6
2 changed files with 59 additions and 29 deletions

View File

@ -134,6 +134,7 @@ void edit(const ParametersParser& parser, Context& context)
: context.buffer().name(); : context.buffer().name();
Buffer* buffer = nullptr; Buffer* buffer = nullptr;
Buffer* oldbuf = &context.buffer();
if (not force_reload) if (not force_reload)
buffer = BufferManager::instance().get_buffer_ifp(name); buffer = BufferManager::instance().get_buffer_ifp(name);
if (not buffer) if (not buffer)
@ -159,7 +160,7 @@ void edit(const ParametersParser& parser, Context& context)
} }
} }
BufferManager::instance().set_last_used_buffer(*buffer); BufferManager::instance().set_last_used_buffer(*oldbuf);
const size_t param_count = parser.positional_count(); const size_t param_count = parser.positional_count();
if (buffer != &context.buffer() or param_count > 1) if (buffer != &context.buffer() or param_count > 1)
@ -364,45 +365,71 @@ const CommandDesc buffer_cmd = {
buffer_completer, buffer_completer,
[](const ParametersParser& parser, Context& context) [](const ParametersParser& parser, Context& context)
{ {
Buffer* oldbuf = &context.buffer();
Buffer& buffer = BufferManager::instance().get_buffer(parser[0]); Buffer& buffer = BufferManager::instance().get_buffer(parser[0]);
BufferManager::instance().set_last_used_buffer(buffer);
if (&buffer != &context.buffer()) if (&buffer != oldbuf)
{ {
BufferManager::instance().set_last_used_buffer(*oldbuf);
context.push_jump(); context.push_jump();
context.change_buffer(buffer); context.change_buffer(buffer);
} }
} }
}; };
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>
{ };
const 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,
BufferManager::instance().set_last_used_buffer(*newbuf); CommandFlags::None,
CommandHelper{},
if (newbuf != oldbuf) CommandCompleter{},
{ cycle_buffer<false>
context.push_jump();
context.change_buffer(*newbuf);
}
}
}; };
template<bool force> template<bool force>
@ -1561,7 +1588,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);

View File

@ -219,7 +219,8 @@ void goto_commands(Context& context, NormalParams params)
if (buffer != &context.buffer()) if (buffer != &context.buffer())
{ {
BufferManager::instance().set_last_used_buffer(*buffer); Buffer* oldbuf = &context.buffer();
BufferManager::instance().set_last_used_buffer(*oldbuf);
context.push_jump(); context.push_jump();
context.change_buffer(*buffer); context.change_buffer(*buffer);
} }
@ -1096,9 +1097,10 @@ void jump(Context& context, NormalParams)
auto jump = (direction == Forward) ? auto jump = (direction == Forward) ?
context.jump_forward() : context.jump_backward(); context.jump_forward() : context.jump_backward();
Buffer* oldbuf = &context.buffer();
Buffer& buffer = const_cast<Buffer&>(jump.buffer()); Buffer& buffer = const_cast<Buffer&>(jump.buffer());
BufferManager::instance().set_last_used_buffer(buffer); BufferManager::instance().set_last_used_buffer(buffer);
if (&buffer != &context.buffer()) if (&buffer != oldbuf)
context.change_buffer(buffer); context.change_buffer(buffer);
context.selections_write_only() = jump; context.selections_write_only() = jump;
} }