Merge remote-tracking branch 'occivink/arrange-buffers'

This commit is contained in:
Maxime Coste 2020-03-15 12:18:04 +11:00
commit 09375edf54
4 changed files with 55 additions and 2 deletions

View File

@ -14,6 +14,12 @@ command *q!* has to be used). Aliases are mentionned below each commands.
For the following *write* commands, the *-sync* switch forces the synchronization For the following *write* commands, the *-sync* switch forces the synchronization
of the file onto the filesystem of the file onto the filesystem
*arrange-buffers* <buffer>...::
Reorder the buffers in the buffers list.
The named buffers will be moved to the front of the buffer list, in the order
given. Buffers that do not appear in the parameters will remain at the
end of the list, keeping their current order.
*change-directory* [<directory>]:: *change-directory* [<directory>]::
*alias* cd + *alias* cd +
change the current directory to *directory*, or the home directory if change the current directory to *directory*, or the home directory if
@ -338,7 +344,7 @@ but not really useful in that context.
*-title* <text>::: *-title* <text>:::
set the title of the message box set the title of the message box
*-markup*::: *-markup*:::
parse markup in both title (if provided) and text. (See parse markup in both title (if provided) and text. (See
<<faces#markup-strings,`:doc faces markup-strings`>>) <<faces#markup-strings,`:doc faces markup-strings`>>)
@ -369,7 +375,7 @@ but not really useful in that context.
*select* [<switches>] <anchor_line>.<anchor_column>,<cursor_line>.<cursor_column>...:: *select* [<switches>] <anchor_line>.<anchor_column>,<cursor_line>.<cursor_column>...::
replace the current selections with the ones described in the arguments replace the current selections with the ones described in the arguments
*-timestamp* <timestamp>::: *-timestamp* <timestamp>:::
specify which buffer timestamp those coordinates apply to. Uses current specify which buffer timestamp those coordinates apply to. Uses current
buffer timestamp if not specified. buffer timestamp if not specified.

View File

@ -105,4 +105,29 @@ void BufferManager::clear_buffer_trash()
m_buffer_trash.clear(); m_buffer_trash.clear();
} }
void BufferManager::arrange_buffers(ConstArrayView<String> first_ones)
{
Vector<size_t> indices;
for (const auto& name : first_ones)
{
auto it = find_if(m_buffers, [&](auto& buf) { return buf->name() == name or buf->display_name() == name; });
if (it == m_buffers.end())
throw runtime_error{format("no such buffer '{}'", name)};
size_t index = it - m_buffers.begin();
if (contains(indices, index))
throw runtime_error{format("buffer '{}' appears more than once", name)};
indices.push_back(index);
}
BufferList res;
for (size_t index : indices)
res.push_back(std::move(m_buffers[index]));
for (auto& buf : m_buffers)
{
if (buf)
res.push_back(std::move(buf));
}
m_buffers = std::move(res);
}
} }

View File

@ -30,6 +30,8 @@ public:
Buffer* get_buffer_ifp(StringView name); Buffer* get_buffer_ifp(StringView name);
Buffer& get_buffer(StringView name); Buffer& get_buffer(StringView name);
void arrange_buffers(ConstArrayView<String> first_ones);
Buffer& get_first_buffer(); Buffer& get_first_buffer();
void backup_modified_buffers(); void backup_modified_buffers();

View File

@ -925,6 +925,25 @@ static void redraw_relevant_clients(Context& context, StringView highlighter_pat
} }
} }
const CommandDesc arrange_buffers_cmd = {
"arrange-buffers",
nullptr,
"arrange-buffers <buffer>...: reorder the buffers in the buffers list\n"
" the named buffers will be moved to the front of the buffer list, in the order given\n"
" buffers that do not appear in the parameters will remain at the end of the list, keeping their current order",
ParameterDesc{{}, ParameterDesc::Flags::None, 1},
CommandFlags::None,
CommandHelper{},
[](const Context& context, CompletionFlags flags, CommandParameters params, size_t, ByteCount cursor_pos)
{
return complete_buffer_name<false>(context, flags, params.back(), cursor_pos);
},
[](const ParametersParser& parser, Context&, const ShellContext&)
{
BufferManager::instance().arrange_buffers(parser.positionals_from(0));
}
};
const CommandDesc add_highlighter_cmd = { const CommandDesc add_highlighter_cmd = {
"add-highlighter", "add-highlighter",
"addhl", "addhl",
@ -2608,6 +2627,7 @@ void register_commands()
register_command(delete_buffer_cmd); register_command(delete_buffer_cmd);
register_command(force_delete_buffer_cmd); register_command(force_delete_buffer_cmd);
register_command(rename_buffer_cmd); register_command(rename_buffer_cmd);
register_command(arrange_buffers_cmd);
register_command(add_highlighter_cmd); register_command(add_highlighter_cmd);
register_command(remove_highlighter_cmd); register_command(remove_highlighter_cmd);
register_command(add_hook_cmd); register_command(add_hook_cmd);