support renaming buffers

Changing buffer name allows to save a *make* or *grep* buffer when
launching another one for exemple.
This commit is contained in:
Maxime Coste 2013-04-22 13:48:18 +02:00
parent 362e346728
commit 681f30dd28
4 changed files with 28 additions and 2 deletions

View File

@ -254,7 +254,8 @@ Commands are entered using +:+.
* +source <filename>+: execute commands in <filename> * +source <filename>+: execute commands in <filename>
* +runtime <filename>+: execute commands in <filename>, <filename> * +runtime <filename>+: execute commands in <filename>, <filename>
is relative to kak executable path. is relative to kak executable path.
* +name <name>+: set current client name * +nameclient <name>+: set current client name
* +namebuf <name>+: set current buffer name
* +exec [-client <name>] <keys>+: execute <keys> as if pressed in normal mode. * +exec [-client <name>] <keys>+: execute <keys> as if pressed in normal mode.
if client if specified, exec keys in the named client context. if client if specified, exec keys in the named client context.
* +eval [-client <name>] <command>+: execute <command> as if entered in command line * +eval [-client <name>] <command>+: execute <command> as if entered in command line

View File

@ -72,6 +72,20 @@ String Buffer::display_name() const
return m_name; return m_name;
} }
bool Buffer::set_name(String name)
{
Buffer* other = BufferManager::instance().get_buffer_ifp(name);
if (other == nullptr or other == this)
{
if (m_flags & Flags::File)
m_name = real_path(name);
else
m_name = std::move(name);
return true;
}
return false;
}
BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column, BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column,
bool avoid_eol) const bool avoid_eol) const
{ {

View File

@ -109,6 +109,8 @@ public:
Flags flags() const { return m_flags; } Flags flags() const { return m_flags; }
Flags& flags() { return m_flags; } Flags& flags() { return m_flags; }
bool set_name(String name);
void insert(BufferIterator pos, String content); void insert(BufferIterator pos, String content);
void erase(BufferIterator begin, BufferIterator end); void erase(BufferIterator begin, BufferIterator end);

View File

@ -240,6 +240,14 @@ void delete_buffer(const CommandParameters& params, Context& context)
manager.delete_buffer(buffer); manager.delete_buffer(buffer);
} }
void set_buffer_name(const CommandParameters& params, Context& context)
{
ParametersParser parser(params, OptionMap{},
ParametersParser::Flags::None, 1, 1);
if (not context.buffer().set_name(parser[0]))
throw runtime_error("unable to change buffer name to " + parser[0]);
}
template<typename Group> template<typename Group>
Group& get_group(Group& root, const String& group_path) Group& get_group(Group& root, const String& group_path)
{ {
@ -900,6 +908,7 @@ void register_commands()
cm.register_commands({ "b", "buffer" }, show_buffer, buffer_completer); cm.register_commands({ "b", "buffer" }, show_buffer, buffer_completer);
cm.register_commands({ "db", "delbuf" }, delete_buffer<false>, buffer_completer); cm.register_commands({ "db", "delbuf" }, delete_buffer<false>, buffer_completer);
cm.register_commands({ "db!", "delbuf!" }, delete_buffer<true>, buffer_completer); cm.register_commands({ "db!", "delbuf!" }, delete_buffer<true>, buffer_completer);
cm.register_commands({"nb", "namebuf"}, set_buffer_name);
auto get_highlighters = [](const Context& c) -> HighlighterGroup& { return c.window().highlighters(); }; auto get_highlighters = [](const Context& c) -> HighlighterGroup& { return c.window().highlighters(); };
auto get_filters = [](const Context& c) -> FilterGroup& { return c.window().filters(); }; auto get_filters = [](const Context& c) -> FilterGroup& { return c.window().filters(); };
@ -943,7 +952,7 @@ void register_commands()
})); }));
cm.register_commands({"ca", "colalias"}, define_color_alias); cm.register_commands({"ca", "colalias"}, define_color_alias);
cm.register_commands({"name"}, set_client_name); cm.register_commands({"nc", "nameclient"}, set_client_name);
cm.register_command("cd", change_working_directory, filename_completer); cm.register_command("cd", change_working_directory, filename_completer);
} }