Skip to content

Commit

Permalink
Do not escape commandline token when inserting completion
Browse files Browse the repository at this point in the history
Fixes #7526
  • Loading branch information
krobelus committed Feb 25, 2021
1 parent 4998c88 commit 807cb57
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
28 changes: 27 additions & 1 deletion src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ bool is_windows_subsystem_for_linux() {
debug_shared(msg_level, L"Backtrace:\n" + join_strings(bt, L'\n') + L'\n');
}

#else // HAVE_BACKTRACE_SYMBOLS
#else // HAVE_BACKTRACE_SYMBOLS

[[gnu::noinline]] void show_stackframe(const wchar_t msg_level, int, int) {
debug_shared(msg_level, L"Sorry, but your system does not support backtraces");
Expand Down Expand Up @@ -1124,6 +1124,32 @@ wcstring escape_string(const wchar_t *in, escape_flags_t flags, escape_string_st
return result;
}

wcstring escape_string(const wchar_t *in, size_t len, escape_flags_t flags,
escape_string_style_t style) {
wcstring result;

switch (style) {
case STRING_STYLE_SCRIPT: {
escape_string_script(in, len, result, flags);
break;
}
case STRING_STYLE_URL: {
escape_string_url(in, result);
break;
}
case STRING_STYLE_VAR: {
escape_string_var(in, result);
break;
}
case STRING_STYLE_REGEX: {
result = escape_string_pcre2(in);
break;
}
}

return result;
}

wcstring escape_string(const wcstring &in, escape_flags_t flags, escape_string_style_t style) {
wcstring result;

Expand Down
2 changes: 2 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ ssize_t read_loop(int fd, void *buff, size_t count);
/// \return The escaped string
wcstring escape_string(const wchar_t *in, escape_flags_t flags,
escape_string_style_t style = STRING_STYLE_SCRIPT);
wcstring escape_string(const wchar_t *in, size_t len, escape_flags_t flags,
escape_string_style_t style = STRING_STYLE_SCRIPT);
wcstring escape_string(const wcstring &in, escape_flags_t flags,
escape_string_style_t style = STRING_STYLE_SCRIPT);

Expand Down
4 changes: 1 addition & 3 deletions src/pager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,7 @@ static comp_info_list_t process_completions_into_infos(const completion_list_t &
comp_info->prefix_len = prefix_len;

// Perhaps ellipsize the prefix.
// FIXME: The escaping mucks with the length here; we may color the wrong number of
// characters. Prefix should be based on width not length anyways.
wcstring comp_str = escape_string(comp.completion, ESCAPE_NO_QUOTED);
wcstring comp_str = comp.completion;
if (prefix_len > PREFIX_MAX_LEN && comp_str.size() > PREFIX_MAX_LEN) {
// Discard the prefix, except for the last PREFIX_MAX_LEN.
// Then ellipsize the first char.
Expand Down
14 changes: 13 additions & 1 deletion src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,8 @@ bool reader_data_t::handle_completions(const completion_list_t &comp, size_t tok
auto best_rank = get_best_rank(comp);
completion_list_t surviving_completions;
bool all_matches_exact_or_prefix = true;
wcstring tok_escaped = escape_string(tok, ESCAPE_ALL | ESCAPE_NO_QUOTED);
bool tok_needs_escaping = tok != tok_escaped;
for (const completion_t &c : comp) {
// Ignore completions with a less suitable match rank than the best.
if (c.rank() > best_rank) continue;
Expand All @@ -1967,7 +1969,17 @@ bool reader_data_t::handle_completions(const completion_list_t &comp, size_t tok
if (completion_replace_token && !reader_can_replace(tok, c.flags)) continue;

// This completion survived.
surviving_completions.push_back(c);
if (c.match.type == string_fuzzy_match_t::contain_type_t::prefix && tok_needs_escaping) {
completion_t cc = c;
cc.flags |= COMPLETE_DONT_ESCAPE;
cc.completion =
cc.completion.substr(0, tok.size()) +
escape_string(cc.completion.c_str() + tok.size(), cc.completion.size() - tok.size(),
ESCAPE_ALL | ESCAPE_NO_QUOTED);
surviving_completions.push_back(cc);
} else {
surviving_completions.push_back(c);
}
all_matches_exact_or_prefix = all_matches_exact_or_prefix && c.match.is_exact_or_prefix();
}

Expand Down

0 comments on commit 807cb57

Please sign in to comment.