mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-12-28 15:14:51 +08:00
Fix backslash escaping problems with echo -E
.
This commit is contained in:
parent
2a5791710a
commit
ddb7284852
|
@ -281,6 +281,43 @@ testWidgetClear() {
|
||||||
"stub_called _zsh_autosuggest_highlight_apply"
|
"stub_called _zsh_autosuggest_highlight_apply"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testEscapeCommandPrefix() {
|
||||||
|
assertEquals \
|
||||||
|
"Did not escape single backslash" \
|
||||||
|
"\\\\" \
|
||||||
|
"$(_zsh_autosuggest_escape_command_prefix "\\")"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"Did not escape two backslashes" \
|
||||||
|
"\\\\\\\\" \
|
||||||
|
"$(_zsh_autosuggest_escape_command_prefix "\\\\")"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"Did not escape parentheses" \
|
||||||
|
"\\(\\)" \
|
||||||
|
"$(_zsh_autosuggest_escape_command_prefix "()")"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"Did not escape square brackets" \
|
||||||
|
"\\[\\]" \
|
||||||
|
"$(_zsh_autosuggest_escape_command_prefix "[]")"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"Did not escape pipe" \
|
||||||
|
"\\|" \
|
||||||
|
"$(_zsh_autosuggest_escape_command_prefix "|")"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"Did not escape star" \
|
||||||
|
"\\*" \
|
||||||
|
"$(_zsh_autosuggest_escape_command_prefix "*")"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"Did not escape question mark" \
|
||||||
|
"\\?" \
|
||||||
|
"$(_zsh_autosuggest_escape_command_prefix "?")"
|
||||||
|
}
|
||||||
|
|
||||||
# For zsh compatibility
|
# For zsh compatibility
|
||||||
setopt shwordsplit
|
setopt shwordsplit
|
||||||
SHUNIT_PARENT=$0
|
SHUNIT_PARENT=$0
|
||||||
|
|
|
@ -5,15 +5,19 @@
|
||||||
|
|
||||||
# Get a suggestion from history that matches a given prefix
|
# Get a suggestion from history that matches a given prefix
|
||||||
_zsh_autosuggest_suggestion() {
|
_zsh_autosuggest_suggestion() {
|
||||||
setopt localoptions extendedglob
|
local prefix=$(_zsh_autosuggest_escape_command_prefix "$1")
|
||||||
|
|
||||||
# Escape the prefix (requires EXTENDED_GLOB)
|
|
||||||
local prefix="${1//(#m)[\][()|\\*?#<>~^]/\\$MATCH}"
|
|
||||||
|
|
||||||
# Get all history items (reversed) that match pattern $prefix*
|
# Get all history items (reversed) that match pattern $prefix*
|
||||||
local history_matches
|
local history_matches
|
||||||
history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
|
history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
|
||||||
|
|
||||||
# Echo the first item that matches
|
# Echo the first item that matches
|
||||||
echo "$history_matches[1]"
|
echo -E "$history_matches[1]"
|
||||||
|
}
|
||||||
|
|
||||||
|
_zsh_autosuggest_escape_command_prefix() {
|
||||||
|
setopt localoptions EXTENDED_GLOB
|
||||||
|
|
||||||
|
# Escape special chars in the string (requires EXTENDED_GLOB)
|
||||||
|
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
|
||||||
}
|
}
|
||||||
|
|
|
@ -291,17 +291,21 @@ zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
||||||
|
|
||||||
# Get a suggestion from history that matches a given prefix
|
# Get a suggestion from history that matches a given prefix
|
||||||
_zsh_autosuggest_suggestion() {
|
_zsh_autosuggest_suggestion() {
|
||||||
setopt localoptions extendedglob
|
local prefix=$(_zsh_autosuggest_escape_command_prefix "$1")
|
||||||
|
|
||||||
# Escape the prefix (requires EXTENDED_GLOB)
|
|
||||||
local prefix="${1//(#m)[\][()|\\*?#<>~^]/\\$MATCH}"
|
|
||||||
|
|
||||||
# Get all history items (reversed) that match pattern $prefix*
|
# Get all history items (reversed) that match pattern $prefix*
|
||||||
local history_matches
|
local history_matches
|
||||||
history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
|
history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
|
||||||
|
|
||||||
# Echo the first item that matches
|
# Echo the first item that matches
|
||||||
echo "$history_matches[1]"
|
echo -E "$history_matches[1]"
|
||||||
|
}
|
||||||
|
|
||||||
|
_zsh_autosuggest_escape_command_prefix() {
|
||||||
|
setopt localoptions EXTENDED_GLOB
|
||||||
|
|
||||||
|
# Escape special chars in the string (requires EXTENDED_GLOB)
|
||||||
|
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
|
||||||
}
|
}
|
||||||
|
|
||||||
#--------------------------------------------------------------------#
|
#--------------------------------------------------------------------#
|
||||||
|
|
Loading…
Reference in New Issue
Block a user