summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOlivier Perret <Olivier.Perret@mailbox.org>2020-02-24 09:41:46 +0100
committerOlivier Perret <Olivier.Perret@mailbox.org>2020-02-24 09:41:46 +0100
commit5c116ab850cc13c9b8bbed1d590022dd97a05cf3 (patch)
treee1fa008cfe5bb33ffac9e6470de7d7e63432d37c /src
parent93a889bd44ceb5d3d4656d251ddbbd4a88b3fe1b (diff)
Add a new 'arrange-buffers' to let users change the order of the buflist
Diffstat (limited to 'src')
-rw-r--r--src/buffer_manager.cc25
-rw-r--r--src/buffer_manager.hh2
-rw-r--r--src/commands.cc20
3 files changed, 47 insertions, 0 deletions
diff --git a/src/buffer_manager.cc b/src/buffer_manager.cc
index d8d4da26..8d17cc76 100644
--- a/src/buffer_manager.cc
+++ b/src/buffer_manager.cc
@@ -105,4 +105,29 @@ void BufferManager::clear_buffer_trash()
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);
+}
+
}
diff --git a/src/buffer_manager.hh b/src/buffer_manager.hh
index 0abb6415..e89fa183 100644
--- a/src/buffer_manager.hh
+++ b/src/buffer_manager.hh
@@ -30,6 +30,8 @@ public:
Buffer* get_buffer_ifp(StringView name);
Buffer& get_buffer(StringView name);
+ void arrange_buffers(ConstArrayView<String> first_ones);
+
Buffer& get_first_buffer();
void backup_modified_buffers();
diff --git a/src/commands.cc b/src/commands.cc
index ee45bcde..980b3ec8 100644
--- a/src/commands.cc
+++ b/src/commands.cc
@@ -923,6 +923,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 = {
"add-highlighter",
"addhl",
@@ -2606,6 +2625,7 @@ void register_commands()
register_command(delete_buffer_cmd);
register_command(force_delete_buffer_cmd);
register_command(rename_buffer_cmd);
+ register_command(arrange_buffers_cmd);
register_command(add_highlighter_cmd);
register_command(remove_highlighter_cmd);
register_command(add_hook_cmd);