From da7fc3311fba98bde4fbc3457fdbf2503fd2b4d0 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 22 Oct 2015 13:59:23 +0100 Subject: [PATCH] Pass ShellContext to commands Fix #427 --- src/command_manager.cc | 7 ++-- src/command_manager.hh | 10 ++++- src/commands.cc | 94 +++++++++++++++++++++--------------------- 3 files changed, 59 insertions(+), 52 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index 8c3a6454..0c8613f2 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -405,6 +405,7 @@ CommandManager::find_command(const Context& context, const String& name) const void CommandManager::execute_single_command(CommandParameters params, Context& context, + const ShellContext& shell_context, CharCoord pos) const { if (params.empty()) @@ -419,7 +420,7 @@ void CommandManager::execute_single_command(CommandParameters params, { ParametersParser parameter_parser(param_view, command_it->second.param_desc); - command_it->second.command(parameter_parser, context); + command_it->second.command(parameter_parser, context, shell_context); } catch (runtime_error& error) { @@ -444,7 +445,7 @@ void CommandManager::execute(StringView command_line, if (it->type() == Token::Type::CommandSeparator) { - execute_single_command(params, context, command_coord); + execute_single_command(params, context, shell_context, command_coord); params.clear(); } // Shell expand are retokenized @@ -464,7 +465,7 @@ void CommandManager::execute(StringView command_line, else params.push_back(expand_token(*it, context, shell_context)); } - execute_single_command(params, context, command_coord); + execute_single_command(params, context, shell_context, command_coord); } CommandInfo CommandManager::command_info(const Context& context, StringView command_line) const diff --git a/src/command_manager.hh b/src/command_manager.hh index d47dbdbc..f2d49761 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -19,11 +19,15 @@ namespace Kakoune class Context; using CommandParameters = ConstArrayView; -using Command = std::function; +using Command = std::function; + using CommandCompleter = std::function; + using CommandHelper = std::function; enum class CommandFlags @@ -84,7 +88,9 @@ public: private: void execute_single_command(CommandParameters params, - Context& context, CharCoord pos) const; + Context& context, + const ShellContext& shell_context, + CharCoord pos) const; struct CommandDescriptor { diff --git a/src/commands.cc b/src/commands.cc index 863170ee..f4627bc6 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -120,11 +120,11 @@ struct CommandDesc CommandFlags flags; CommandHelper helper; CommandCompleter completer; - void (*func)(const ParametersParser&, Context&); + void (*func)(const ParametersParser&, Context&, const ShellContext&); }; template -void edit(const ParametersParser& parser, Context& context) +void edit(const ParametersParser& parser, Context& context, const ShellContext&) { if (parser.positional_count() == 0 and not force_reload) throw wrong_argument_count(); @@ -215,7 +215,7 @@ const CommandDesc force_edit_cmd = { edit }; -void write_buffer(const ParametersParser& parser, Context& context) +void write_buffer(const ParametersParser& parser, Context& context, const ShellContext&) { Buffer& buffer = context.buffer(); @@ -256,7 +256,7 @@ const CommandDesc writeall_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser&, Context&){ write_all_buffers(); } + [](const ParametersParser&, Context&, const ShellContext&){ write_all_buffers(); } }; const CommandDesc kill_cmd = { @@ -267,7 +267,7 @@ const CommandDesc kill_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser&, Context&){ throw kill_session{}; } + [](const ParametersParser&, Context&, const ShellContext&){ throw kill_session{}; } }; template @@ -307,7 +307,7 @@ const CommandDesc quit_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser&, Context&){ quit(); } + [](const ParametersParser&, Context&, const ShellContext&){ quit(); } }; const CommandDesc force_quit_cmd = { @@ -320,7 +320,7 @@ const CommandDesc force_quit_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser&, Context&){ quit(); } + [](const ParametersParser&, Context&, const ShellContext&){ quit(); } }; const CommandDesc write_quit_cmd = { @@ -331,9 +331,9 @@ const CommandDesc write_quit_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext& shell_context) { - write_buffer(parser, context); + write_buffer(parser, context, shell_context); quit(); } }; @@ -347,9 +347,9 @@ const CommandDesc force_write_quit_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext& shell_context) { - write_buffer(parser, context); + write_buffer(parser, context, shell_context); quit(); } }; @@ -362,7 +362,7 @@ const CommandDesc writeall_quit_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { write_all_buffers(); quit(); @@ -377,7 +377,7 @@ const CommandDesc buffer_cmd = { CommandFlags::None, CommandHelper{}, buffer_completer, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { Buffer* oldbuf = &context.buffer(); Buffer& buffer = BufferManager::instance().get_buffer(parser[0]); @@ -392,7 +392,7 @@ const CommandDesc buffer_cmd = { }; template -void cycle_buffer(const ParametersParser& parser, Context& context) +void cycle_buffer(const ParametersParser& parser, Context& context, const ShellContext&) { Buffer* oldbuf = &context.buffer(); auto it = find_if(BufferManager::instance(), @@ -446,7 +446,7 @@ const CommandDesc bufferprev_cmd = { }; template -void delete_buffer(const ParametersParser& parser, Context& context) +void delete_buffer(const ParametersParser& parser, Context& context, const ShellContext&) { BufferManager& manager = BufferManager::instance(); Buffer& buffer = parser.positional_count() == 0 ? context.buffer() : manager.get_buffer(parser[0]); @@ -490,7 +490,7 @@ const CommandDesc namebuf_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { if (not context.buffer().set_name(parser[0])) throw runtime_error(format("unable to change buffer name to '{}'", parser[0])); @@ -585,7 +585,7 @@ const CommandDesc add_highlighter_cmd = { return ""; }, add_highlighter_completer, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { HighlighterRegistry& registry = HighlighterRegistry::instance(); @@ -616,7 +616,7 @@ const CommandDesc rm_highlighter_cmd = { CommandFlags::None, CommandHelper{}, rm_highlighter_completer, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { StringView path = parser[0]; auto sep_it = find(reversed(path), '/'); @@ -661,7 +661,7 @@ const CommandDesc add_hook_cmd = { } return {}; }, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { Regex regex(parser[2].begin(), parser[2].end(), Regex::optimize | Regex::nosubs | Regex::ECMAScript); @@ -705,7 +705,7 @@ const CommandDesc rm_hook_cmd = { } return {}; }, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { get_scope(parser[0], context).hooks().remove_hooks(parser[1]); } @@ -719,7 +719,7 @@ Vector params_to_shell(const ParametersParser& parser) return vars; } -void define_command(const ParametersParser& parser, Context& context) +void define_command(const ParametersParser& parser, Context& context, const ShellContext&) { const String& cmd_name = parser[0]; auto& cm = CommandManager::instance(); @@ -737,14 +737,14 @@ void define_command(const ParametersParser& parser, Context& context) if (parser.get_switch("shell-params")) { desc = ParameterDesc{ {}, ParameterDesc::Flags::SwitchesAsPositional }; - cmd = [=](const ParametersParser& parser, Context& context) { + cmd = [=](const ParametersParser& parser, Context& context, const ShellContext&) { CommandManager::instance().execute(commands, context, { params_to_shell(parser) }); }; } else { desc = ParameterDesc{ {}, ParameterDesc::Flags::SwitchesAsPositional, 0, 0 }; - cmd = [=](const ParametersParser& parser, Context& context) { + cmd = [=](const ParametersParser& parser, Context& context, const ShellContext&) { CommandManager::instance().execute(commands, context); }; } @@ -854,7 +854,7 @@ const CommandDesc alias_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { if (not CommandManager::instance().command_defined(parser[2])) throw runtime_error(format("Command '{}' does not exist", parser[2])); @@ -873,7 +873,7 @@ const CommandDesc unalias_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { AliasRegistry& aliases = get_scope(parser[0], context).aliases(); if (parser.positional_count() == 3 and @@ -896,7 +896,7 @@ const CommandDesc echo_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { String message = join(parser, ' ', false); if (parser.get_switch("debug")) @@ -926,7 +926,7 @@ const CommandDesc debug_cmd = { auto c = {"info", "buffers", "options", "memory", "shared-strings"}; return { 0_byte, cursor_pos, complete(prefix, cursor_pos, c) }; } }), - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { if (parser[0] == "info") { @@ -977,7 +977,7 @@ const CommandDesc source_cmd = { CommandFlags::None, CommandHelper{}, filename_completer, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { String file_content = read_file(parse_filename(parser[0]), true); try @@ -1037,7 +1037,7 @@ const CommandDesc set_option_cmd = { } return Completions{}; }, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { Option& opt = get_scope(parser[0], context).options().get_local_option(parser[1]); if (parser.get_switch("add")) @@ -1068,7 +1068,7 @@ const CommandDesc unset_option_cmd = { GlobalScope::instance().option_registry().complete_option_name(params[1], pos_in_token) }; return Completions{}; }, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { if (parser[0] == "global") throw runtime_error("Cannot unset options in global scope"); @@ -1098,7 +1098,7 @@ const CommandDesc declare_option_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { Option* opt = nullptr; @@ -1176,7 +1176,7 @@ const CommandDesc map_key_cmd = { } return {}; }, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { KeymapManager& keymaps = get_scope(parser[0], context).keymaps(); KeymapMode keymap_mode = parse_keymap_mode(parser[1]); @@ -1316,7 +1316,7 @@ const CommandDesc exec_string_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { context_wrap(parser, context, [](const ParametersParser& parser, Context& context) { KeyList keys; @@ -1338,7 +1338,7 @@ const CommandDesc eval_string_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { context_wrap(parser, context, [](const ParametersParser& parser, Context& context) { String command = join(parser, ' ', false); @@ -1359,7 +1359,7 @@ const CommandDesc prompt_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& params, Context& context) + [](const ParametersParser& params, Context& context, const ShellContext&) { if (params[1].length() != 1) throw runtime_error("register name should be a single character"); @@ -1393,7 +1393,7 @@ const CommandDesc menu_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { const bool with_select_cmds = (bool)parser.get_switch("select-cmds"); const bool markup = (bool)parser.get_switch("markup"); @@ -1439,7 +1439,7 @@ const CommandDesc onkey_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { String reg = parser[0]; String command = parser[1]; @@ -1464,7 +1464,7 @@ const CommandDesc info_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { context.ui().info_hide(); if (parser.positional_count() > 0) @@ -1506,7 +1506,7 @@ const CommandDesc try_catch_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext& shell_context) { if (parser.positional_count() == 2) throw wrong_argument_count(); @@ -1518,12 +1518,12 @@ const CommandDesc try_catch_cmd = { CommandManager& command_manager = CommandManager::instance(); try { - command_manager.execute(parser[0], context); + command_manager.execute(parser[0], context, shell_context); } catch (Kakoune::runtime_error& e) { if (do_catch) - command_manager.execute(parser[2], context); + command_manager.execute(parser[2], context, shell_context); } } }; @@ -1549,7 +1549,7 @@ const CommandDesc face_cmd = { CommandFlags::None, CommandHelper{}, PerArgumentCommandCompleter({ complete_face, complete_face }), - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { FaceRegistry::instance().register_alias(parser[0], parser[1], true); @@ -1566,7 +1566,7 @@ const CommandDesc set_client_name_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { if (ClientManager::instance().validate_client_name(parser[0])) context.set_name(parser[0]); @@ -1583,7 +1583,7 @@ const CommandDesc set_register_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { RegisterManager::instance()[parser[0]] = ConstArrayView(parser[1]); } @@ -1597,7 +1597,7 @@ const CommandDesc select_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser& parser, Context& context) + [](const ParametersParser& parser, Context& context, const ShellContext&) { context.selections_write_only() = selection_list_from_string(context.buffer(), parser[0]); } @@ -1611,7 +1611,7 @@ const CommandDesc change_working_directory_cmd = { CommandFlags::None, CommandHelper{}, filename_completer, - [](const ParametersParser& parser, Context&) + [](const ParametersParser& parser, Context&, const ShellContext&) { if (chdir(parse_filename(parser[0]).c_str()) != 0) throw runtime_error(format("cannot change to directory '{}'", parser[0])); @@ -1652,7 +1652,7 @@ void exec_keys(ConstArrayView keys, Context& context) void register_commands() { CommandManager& cm = CommandManager::instance(); - cm.register_command("nop", [](const ParametersParser&, Context&){}, "do nothing", {}); + cm.register_command("nop", [](const ParametersParser&, Context&, const ShellContext&){}, "do nothing", {}); auto register_command = [&](const CommandDesc& c) {