diff --git a/src/command_manager.cc b/src/command_manager.cc index e1cbdfdd..e4cde2ba 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -17,60 +17,110 @@ bool CommandManager::command_defined(const String& command_name) const void CommandManager::register_command(const String& command_name, Command command, - unsigned flags, const CommandCompleter& completer) { - m_commands[command_name] = CommandDescriptor { command, flags, completer }; + m_commands[command_name] = CommandDescriptor { command, completer }; } void CommandManager::register_commands(const memoryview& command_names, Command command, - unsigned flags, const CommandCompleter& completer) { for (auto command_name : command_names) - register_command(command_name, command, flags, completer); + register_command(command_name, command, completer); } -static bool is_blank(char c) +static bool is_horizontal_blank(char c) { - return c == ' ' or c == '\t' or c == '\n'; + return c == ' ' or c == '\t'; } using TokenList = std::vector; using TokenPosList = std::vector>; +static bool is_command_separator(Character c) +{ + return c == ';' or c == '\n'; +} + static TokenList parse(const String& line, TokenPosList* opt_token_pos_info = NULL) { TokenList result; + size_t length = line.length(); size_t pos = 0; - while (pos < line.length()) + while (pos < length) { - while(is_blank(line[pos]) and pos != line.length()) - ++pos; + while (pos != length) + { + if (is_horizontal_blank(line[pos])) + ++pos; + else if (line[pos] == '\\' and pos+1 < length and line[pos+1] == '\n') + pos += 2; + else + break; + } size_t token_start = pos; Token::Type type = Token::Type::Raw; - if (line[pos] == '"' or line[pos] == '\'' or line[pos] == '`') + if (line[pos] == '"' or line[pos] == '\'') { char delimiter = line[pos]; - if (delimiter == '`') - type = Token::Type::ShellExpand; - token_start = ++pos; while ((line[pos] != delimiter or line[pos-1] == '\\') and - pos != line.length()) + pos != length) ++pos; + } + else if (line[pos] == '%') + { + size_t type_start = ++pos; + while (isalpha(line[pos])) + ++pos; + String type_name = line.substr(type_start, pos - type_start); + if (type_name == "sh") + type = Token::Type::ShellExpand; + + static const std::unordered_map matching_delimiters = { + { '(', ')' }, { '[', ']' }, { '{', '}' }, { '<', '>' } + }; + + Character opening_delimiter = line[pos]; + token_start = ++pos; + + auto delim_it = matching_delimiters.find(opening_delimiter); + if (delim_it != matching_delimiters.end()) + { + Character closing_delimiter = delim_it->second; + int level = 0; + while (pos != length) + { + if (line[pos-1] != '\\' and line[pos] == opening_delimiter) + ++level; + if (line[pos-1] != '\\' and line[pos] == closing_delimiter) + { + if (level > 0) + --level; + else + break; + } + ++pos; + } + } + else + { + while ((line[pos] != opening_delimiter or line[pos-1] == '\\') and + pos != length) + ++pos; + } } else - while (not is_blank(line[pos]) and pos != line.length() and - (line[pos] != ';' or line[pos-1] == '\\')) + while (pos != length and not is_horizontal_blank(line[pos]) and + (not is_command_separator(line[pos]) or line[pos-1] == '\\')) ++pos; if (token_start != pos) @@ -80,7 +130,7 @@ static TokenList parse(const String& line, result.push_back({type, line.substr(token_start, pos - token_start)}); } - if (line[pos] == ';') + if (is_command_separator(line[pos])) { if (opt_token_pos_info) opt_token_pos_info->push_back({pos, pos+1}); @@ -140,24 +190,16 @@ void CommandManager::execute(const CommandParameters& params, if (command_it == m_commands.end()) throw command_not_found(begin->content()); - if (command_it->second.flags & IgnoreSemiColons) - end = params.end(); - - if (command_it->second.flags & DeferredShellEval) - command_it->second.command(CommandParameters(begin + 1, end), context); - else + TokenList expanded_tokens; + for (auto param = begin+1; param != end; ++param) { - TokenList expanded_tokens; - for (auto param = begin+1; param != end; ++param) - { - if (param->type() == Token::Type::ShellExpand) - shell_eval(expanded_tokens, param->content(), - context, env_vars); - else - expanded_tokens.push_back(*param); - } - command_it->second.command(expanded_tokens, context); + if (param->type() == Token::Type::ShellExpand) + shell_eval(expanded_tokens, param->content(), + context, env_vars); + else + expanded_tokens.push_back(*param); } + command_it->second.command(expanded_tokens, context); } if (end == params.end()) diff --git a/src/command_manager.hh b/src/command_manager.hh index 134dbd48..a0609c5e 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -72,13 +72,6 @@ private: class CommandManager : public Singleton { public: - enum Flags - { - None = 0, - IgnoreSemiColons = 1, - DeferredShellEval = 2, - }; - void execute(const String& command_line, const Context& context, const EnvVarMap& env_vars = EnvVarMap()); void execute(const CommandParameters& params, const Context& context, @@ -90,19 +83,16 @@ public: void register_command(const String& command_name, Command command, - unsigned flags = None, const CommandCompleter& completer = CommandCompleter()); void register_commands(const memoryview& command_names, Command command, - unsigned flags = None, const CommandCompleter& completer = CommandCompleter()); private: struct CommandDescriptor { Command command; - unsigned flags; CommandCompleter completer; }; std::unordered_map m_commands; diff --git a/src/commands.cc b/src/commands.cc index 5111bc25..1d83bde2 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -440,16 +440,16 @@ void rm_filter(const CommandParameters& params, const Context& context) void add_hook(const CommandParameters& params, const Context& context) { - if (params.size() < 4) + if (params.size() != 4) throw wrong_argument_count(); + // copy so that the lambda gets a copy as well String regex = params[2].content(); - std::vector hook_params(params.begin()+3, params.end()); - + String command = params[3].content(); auto hook_func = [=](const String& param, const Context& context) { if (boost::regex_match(param.begin(), param.end(), Regex(regex.begin(), regex.end()))) - CommandManager::instance().execute(hook_params, context); + CommandManager::instance().execute(command, context); }; const String& scope = params[0].content(); @@ -484,7 +484,7 @@ void define_command(const CommandParameters& params, const Context& context) { "allow-override", false }, { "shell-completion", true } }); - if (parser.positional_count() < 2) + if (parser.positional_count() != 2) throw wrong_argument_count(); auto begin = parser.begin(); @@ -494,30 +494,21 @@ void define_command(const CommandParameters& params, const Context& context) not parser.has_option("allow-override")) throw runtime_error("command '" + cmd_name + "' already defined"); - std::vector cmd_params(++begin, parser.end()); + String commands = parser[1].content(); Command cmd; if (parser.has_option("env-params")) { cmd = [=](const CommandParameters& params, const Context& context) { - CommandManager::instance().execute(cmd_params, context, + CommandManager::instance().execute(commands, context, params_to_env_var_map(params)); }; } - else if (parser.has_option("append-params")) - { - cmd = [=](const CommandParameters& params, const Context& context) { - std::vector merged_params = cmd_params; - for (auto& param : params) - merged_params.push_back(param); - CommandManager::instance().execute(merged_params, context); - }; - } else { cmd = [=](const CommandParameters& params, const Context& context) { if (not params.empty()) throw wrong_argument_count(); - CommandManager::instance().execute(cmd_params, context); + CommandManager::instance().execute(commands, context); }; } @@ -533,9 +524,7 @@ void define_command(const CommandParameters& params, const Context& context) String output = ShellManager::instance().eval(shell_cmd, context, vars); return split(output, '\n'); }; - CommandManager::instance().register_command(cmd_name, cmd, - CommandManager::None, - completer); + CommandManager::instance().register_command(cmd_name, cmd, completer); } else CommandManager::instance().register_command(cmd_name, cmd); @@ -556,61 +545,7 @@ void exec_commands_in_file(const CommandParameters& params, throw wrong_argument_count(); String file_content = read_file(parse_filename(params[0].content())); - CommandManager& cmd_manager = CommandManager::instance(); - - size_t pos = 0; - size_t length = file_content.length(); - bool cat_with_previous = false; - String command_line; - while (true) - { - if (not cat_with_previous) - command_line = String(); - - size_t end_pos = pos; - - while (file_content[end_pos] != '\n' and end_pos != length) - { - if (file_content[end_pos] == '"' or file_content[end_pos] == '\'' or - file_content[end_pos] == '`') - { - char delimiter = file_content[end_pos]; - ++end_pos; - while ((file_content[end_pos] != delimiter or - file_content[end_pos-1] == '\\') and end_pos != length) - ++end_pos; - - if (end_pos == length) - throw(String("unterminated '") + delimiter + "' string"); - - ++end_pos; - } - - if (end_pos != length) - ++end_pos; - } - if (end_pos != pos and end_pos != length and - file_content[end_pos - 1] == '\\') - { - command_line += file_content.substr(pos, end_pos - pos - 1); - cat_with_previous = true; - } - else - { - command_line += file_content.substr(pos, end_pos - pos); - cmd_manager.execute(command_line, context); - cat_with_previous = false; - } - if (end_pos == length) - { - if (cat_with_previous) - throw runtime_error("while executing commands in \"" + - params[0].content() + - "\": last command not complete"); - break; - } - pos = end_pos + 1; - } + CommandManager::instance().execute(file_content, context); } void exec_commands_in_runtime_file(const CommandParameters& params, @@ -803,24 +738,19 @@ void menu(const CommandParameters& params, void try_catch(const CommandParameters& params, const Context& context) { - size_t i = 0; - for (; i < params.size(); ++i) - { - if (params[i].content() == "catch") - break; - } - - if (i == 0 or i == params.size()) + if (params.size() != 3) throw wrong_argument_count(); + if (params[1].content() != "catch") + throw runtime_error("try needs a catch"); CommandManager& command_manager = CommandManager::instance(); try { - command_manager.execute(params.subrange(0, i), context); + command_manager.execute(params[0].content(), context); } catch (Kakoune::runtime_error& e) { - command_manager.execute(params.subrange(i+1, params.size() - i - 1), context); + command_manager.execute(params[2].content(), context); } } @@ -831,12 +761,9 @@ void register_commands() CommandManager& cm = CommandManager::instance(); PerArgumentCommandCompleter filename_completer({ complete_filename }); - cm.register_commands({ "e", "edit" }, edit, - CommandManager::None, filename_completer); - cm.register_commands({ "e!", "edit!" }, edit, - CommandManager::None, filename_completer); - cm.register_commands({ "w", "write" }, write_buffer, - CommandManager::None, filename_completer); + cm.register_commands({ "e", "edit" }, edit, filename_completer); + cm.register_commands({ "e!", "edit!" }, edit, filename_completer); + cm.register_commands({ "w", "write" }, write_buffer, filename_completer); cm.register_commands({ "q", "quit" }, quit); cm.register_commands({ "q!", "quit!" }, quit); cm.register_command("wq", write_and_quit); @@ -846,13 +773,10 @@ void register_commands() [](const String& prefix, size_t cursor_pos) { return BufferManager::instance().complete_buffername(prefix, cursor_pos); } }); - cm.register_commands({ "b", "buffer" }, show_buffer, - CommandManager::None, buffer_completer); - cm.register_commands({ "db", "delbuf" }, delete_buffer, - CommandManager::None, buffer_completer); + cm.register_commands({ "b", "buffer" }, show_buffer, buffer_completer); + cm.register_commands({ "db", "delbuf" }, delete_buffer, buffer_completer); cm.register_commands({ "ah", "addhl" }, add_highlighter, - CommandManager::None, [](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token) { Window& w = main_context.window(); @@ -866,7 +790,6 @@ void register_commands() return CandidateList(); }); cm.register_commands({ "rh", "rmhl" }, rm_highlighter, - CommandManager::None, [](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token) { Window& w = main_context.window(); @@ -880,7 +803,6 @@ void register_commands() return w.highlighters().complete_id(arg, pos_in_token); }); cm.register_commands({ "af", "addfilter" }, add_filter, - CommandManager::None, [](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token) { Window& w = main_context.window(); @@ -894,7 +816,6 @@ void register_commands() return CandidateList(); }); cm.register_commands({ "rf", "rmfilter" }, rm_filter, - CommandManager::None, [](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token) { Window& w = main_context.window(); @@ -908,24 +829,22 @@ void register_commands() return w.filters().complete_id(arg, pos_in_token); }); - cm.register_command("hook", add_hook, CommandManager::IgnoreSemiColons | CommandManager::DeferredShellEval); + cm.register_command("hook", add_hook); - cm.register_command("source", exec_commands_in_file, - CommandManager::None, filename_completer); + cm.register_command("source", exec_commands_in_file, filename_completer); cm.register_command("runtime", exec_commands_in_runtime_file); cm.register_command("exec", exec_string); cm.register_command("eval", eval_string); cm.register_command("menu", menu); - cm.register_command("try", try_catch, CommandManager::IgnoreSemiColons | CommandManager::DeferredShellEval); + cm.register_command("try", try_catch); - cm.register_command("def", define_command, CommandManager::IgnoreSemiColons | CommandManager::DeferredShellEval); + cm.register_command("def", define_command); cm.register_command("echo", echo_message); cm.register_commands({ "setg", "setglobal" }, [](const CommandParameters& params, const Context& context) { set_option(GlobalOptionManager::instance(), params, context); }, - CommandManager::None, PerArgumentCommandCompleter({ [](const String& prefix, size_t cursor_pos) { return GlobalOptionManager::instance().complete_option_name(prefix, cursor_pos); } @@ -933,7 +852,6 @@ void register_commands() cm.register_commands({ "setb", "setbuffer" }, [](const CommandParameters& params, const Context& context) { set_option(context.buffer().option_manager(), params, context); }, - CommandManager::None, PerArgumentCommandCompleter({ [](const String& prefix, size_t cursor_pos) { return main_context.buffer().option_manager().complete_option_name(prefix, cursor_pos); } @@ -941,7 +859,6 @@ void register_commands() cm.register_commands({ "setw", "setwindow" }, [](const CommandParameters& params, const Context& context) { set_option(context.window().option_manager(), params, context); }, - CommandManager::None, PerArgumentCommandCompleter({ [](const String& prefix, size_t cursor_pos) { return main_context.window().option_manager().complete_option_name(prefix, cursor_pos); } diff --git a/src/kakrc b/src/kakrc index 4fc8cd37..27e4e289 100644 --- a/src/kakrc +++ b/src/kakrc @@ -1,5 +1,5 @@ -hook global WinCreate .* addhl regex \h+(?=\n) default red -hook global WinCreate .* addhl number_lines +hook global WinCreate .* %{ addhl regex \h+(?=\n) default red } +hook global WinCreate .* %{ addhl number_lines } runtime rc/cpp.kak runtime rc/kakrc.kak diff --git a/src/rc/cpp.kak b/src/rc/cpp.kak index 82b9e9bf..f3e4dec9 100644 --- a/src/rc/cpp.kak +++ b/src/rc/cpp.kak @@ -1,29 +1,33 @@ -hook global BufCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) \ +hook global BufCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) %{ setb filetype cpp +} -hook global WinSetOption filetype=cpp \ - addhl group cpp-highlight; \ - addhl -group cpp-highlight regex "\<(this|true|false|NULL|nullptr|)\>|\<-?\d+[fdiu]?|'((\\.)?|[^'\\])'" red default; \ - addhl -group cpp-highlight regex "\<(void|int|char|unsigned|float|bool|size_t)\>" yellow default; \ - addhl -group cpp-highlight regex "\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw|new|delete|and|or|not)\>" blue default; \ - addhl -group cpp-highlight regex "\<(const|auto|namespace|inline|static|volatile|class|struct|enum|union|public|protected|private|template|typedef|virtual|friend|extern|typename)\>" green default; \ - addhl -group cpp-highlight regex "(?d +hook global WinSetOption filetype=cpp %{ + addhl group cpp-highlight; + addhl -group cpp-highlight regex "\<(this|true|false|NULL|nullptr|)\>|\<-?\d+[fdiu]?|'((\\.)?|[^'\\])'" red default; + addhl -group cpp-highlight regex "\<(void|int|char|unsigned|float|bool|size_t)\>" yellow default; + addhl -group cpp-highlight regex "\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw|new|delete|and|or|not)\>" blue default; + addhl -group cpp-highlight regex "\<(const|auto|namespace|inline|static|volatile|class|struct|enum|union|public|protected|private|template|typedef|virtual|friend|extern|typename)\>" green default; + addhl -group cpp-highlight regex "(?d } +} -hook global WinSetOption filetype=(?!cpp).* \ - rmhl cpp-highlight; \ +hook global WinSetOption filetype=(?!cpp).* %{ + rmhl cpp-highlight; rmfilter cpp-filters +} -hook global BufNew .*\.(h|hh|hpp|hxx|H) \ +hook global BufNew .*\.(h|hh|hpp|hxx|H) %{ exec ggi%ggxs\.c_A_INCLUDEDxyppI#ifndefjI#definejI#endif//O +} -def alt edit \ - `case ${kak_bufname} in +def alt %{ edit %sh{ + case ${kak_bufname} in *.c) echo ${kak_bufname/%c/h} ;; *.cc) echo ${kak_bufname/%cc/hh} ;; *.cpp) echo ${kak_bufname/%cpp/hpp} ;; @@ -34,4 +38,5 @@ def alt edit \ *.hpp) echo ${kak_bufname/%hpp/cpp} ;; *.hxx) echo ${kak_bufname/%hxx/cxx} ;; *.H) echo ${kak_bufname/%H/C} ;; - esac` \ No newline at end of file + esac +}} \ No newline at end of file diff --git a/src/rc/diff.kak b/src/rc/diff.kak index 8a9a390f..434026ab 100644 --- a/src/rc/diff.kak +++ b/src/rc/diff.kak @@ -1,11 +1,14 @@ -hook global BufCreate .*\.(diff|patch) \ +hook global BufCreate .*\.(diff|patch) %{ setb filetype diff +} -hook global WinSetOption filetype=diff \ - addhl group diff-highlight; \ - addhl -group diff-highlight regex "^\+[^\n]*\n" green default; \ - addhl -group diff-highlight regex "^-[^\n]*\n" red default; \ - addhl -group diff-highlight regex "^@@[^\n]*@@" cyan default; +hook global WinSetOption filetype=diff %{ + addhl group diff-highlight + addhl -group diff-highlight regex "^\+[^\n]*\n" green default + addhl -group diff-highlight regex "^-[^\n]*\n" red default + addhl -group diff-highlight regex "^@@[^\n]*@@" cyan default +} -hook global WinSetOption filetype=(?!diff).* \ +hook global WinSetOption filetype=(?!diff).* %{ rmhl diff-highlight +} diff --git a/src/rc/git.kak b/src/rc/git.kak index 8b391ce5..14fa37c3 100644 --- a/src/rc/git.kak +++ b/src/rc/git.kak @@ -1,23 +1,29 @@ -hook global BufCreate .*COMMIT_EDITMSG \ +hook global BufCreate .*COMMIT_EDITMSG %{ setb filetype git-commit +} -hook global WinSetOption filetype=git-commit \ - addhl group git-commit-highlight; \ - addhl -group git-commit-highlight regex "#[^\n]*\n" cyan default; \ - addhl -group git-commit-highlight regex "\<(modified|deleted|new file):[^\n]*\n" magenta default; \ - addhl -group git-commit-highlight regex "\<(modified|deleted|new file):" red default; +hook global WinSetOption filetype=git-commit %{ + addhl group git-commit-highlight + addhl -group git-commit-highlight regex "#[^\n]*\n" cyan default + addhl -group git-commit-highlight regex "\<(modified|deleted|new file):[^\n]*\n" magenta default + addhl -group git-commit-highlight regex "\<(modified|deleted|new file):" red default +} -hook global WinSetOption filetype=(?!git-commit).* \ +hook global WinSetOption filetype=(?!git-commit).* %{ rmhl git-commit-highlight +} -hook global BufCreate .*git-rebase-todo \ +hook global BufCreate .*git-rebase-todo %{ setb filetype git-rebase +} -hook global WinSetOption filetype=git-rebase \ - addhl group git-rebase-highlight; \ - addhl -group git-rebase-highlight regex "#[^\n]*\n" cyan default; \ - addhl -group git-rebase-highlight regex "^(pick|edit|reword|squash|fixup|exec|[persfx]) \w+" magenta default; \ - addhl -group git-rebase-highlight regex "^(pick|edit|reword|squash|fixup|exec|[persfx])" green default; +hook global WinSetOption filetype=git-rebase %{ + addhl group git-rebase-highlight + addhl -group git-rebase-highlight regex "#[^\n]*\n" cyan default + addhl -group git-rebase-highlight regex "^(pick|edit|reword|squash|fixup|exec|[persfx]) \w+" magenta default + addhl -group git-rebase-highlight regex "^(pick|edit|reword|squash|fixup|exec|[persfx])" green default +} -hook global WinSetOption filetype=(?!git-rebase).* \ +hook global WinSetOption filetype=(?!git-rebase).* %{ rmhl git-rebase-highlight +} diff --git a/src/rc/global.kak b/src/rc/global.kak index 4f8da202..983a420b 100644 --- a/src/rc/global.kak +++ b/src/rc/global.kak @@ -1,14 +1,15 @@ def -env-params \ - -shell-completion 'global -c ${kak_param0}' \ - tag eval \ - `if [[ ${kak_param0} != "" ]]; then + -shell-completion %{ global -c ${kak_param0} } \ + tag %{ eval %sh{ + if [[ ${kak_param0} != "" ]]; then tagname=${kak_param0} else tagname=${kak_selection} fi - params=$(global --result grep ${tagname} | sed "s/\([^:]*\):\([0-9]*\):\(.*\)/'\1:\2 \3' 'edit \1 \2; try exec \"20k41Xs\\\\Q\3\" catch echo \"could not find [\3] near \1:\2\"; exec \2g'/") + params=$(global --result grep ${tagname} | sed "s/\([^:]*\):\([0-9]*\):\(.*\)/'\1:\2 \3' 'edit \1 \2; try %{ exec \"20k41Xs\\\\Q\3\" } catch %{ echo \"could not find [\3] near \1:\2\"; exec \2g }'/") if [[ ${params} != "" ]]; then - echo "menu -auto-single $params" + echo "menu -auto-single ${params//$'\n'/ }" else echo echo tag ${tagname} not found - fi` + fi +}} diff --git a/src/rc/kakrc.kak b/src/rc/kakrc.kak index a2f3c668..a7ab8b31 100644 --- a/src/rc/kakrc.kak +++ b/src/rc/kakrc.kak @@ -1,17 +1,20 @@ -hook global BufCreate (.*/)?(kakrc|.*.kak) \ +hook global BufCreate (.*/)?(kakrc|.*.kak) %{ setb filetype kak +} -hook global WinSetOption filetype=kak \ - addhl group kak-highlight; \ - addhl -group kak-highlight regex \<(hook|addhl|rmhl|addfilter|rmfilter|exec|source|runtime|def|echo|edit|set[gbw])\> green default; \ - addhl -group kak-highlight regex \<(default|black|red|green|yellow|blue|magenta|cyan|white)\> yellow default; \ - addhl -group kak-highlight regex (?<=\ green default + addhl -group kak-highlight regex \<(default|black|red|green|yellow|blue|magenta|cyan|white)\> yellow default + addhl -group kak-highlight regex (?<=\