diff --git a/README.md b/README.md index 4ebcd804..0f31bec5 100644 --- a/README.md +++ b/README.md @@ -212,8 +212,6 @@ abbr a=b\;c # allowed abbr a="b|c" # allowed ``` -**Note:** when saved for cross-session use, user abbreviations' EXPANSIONs are wrapped in double quotes (`"`), _even if single quotes (`'`) were originally used._ For this reason, double quotation marks should always be escaped. See issue #10. - ```shell abbr a='a"b"c' # bad - will turn into `"a"b"c"`, which will error abbr a='a\"b\"c' # good diff --git a/tests/tests.plugin.zsh b/tests/tests.plugin.zsh index 6933ee3a..981f369d 100644 --- a/tests/tests.plugin.zsh +++ b/tests/tests.plugin.zsh @@ -3,101 +3,137 @@ source ${0:A:h}/../zsh-abbr.zsh test_abbr_abbreviation="zsh_abbr_test" test_abbr_expansion="zsh abbr test" -test_abbr="zsh_abbr_test=\"$test_abbr_expansion\"" +test_abbr="$test_abbr_abbreviation=$test_abbr_expansion" -message="abbr --add && abbr -s " +message="abbr --add && abbr --list-commands " abbr --add $test_abbr -if [[ $(abbr -s) == "abbr $test_abbr" ]]; then +if [[ $(abbr --list-commands) == "abbr $test_abbr_abbreviation=${(qqq)test_abbr_expansion}" ]]; then message+="passed" else message+="failed" fi echo $message -message="abbr --show " -if [[ $(abbr -s) == $(abbr --show) ]]; then +message="abbr --erase " +abbr --erase $test_abbr_abbreviation +if [[ $(abbr --list-commands) == "" ]]; then message+="passed" else message+="failed" fi echo $message -message="abbr -e " -abbr -e $test_abbr_abbreviation -if [[ $(abbr -s) == "" ]]; then +message="abbr " +abbr $test_abbr +if [[ $(abbr --list-commands) == "abbr $test_abbr_abbreviation=${(qqq)test_abbr_expansion}" ]]; then message+="passed" else message+="failed" fi echo $message +abbr -e $test_abbr_abbreviation -message="abbr -a " -abbr -a $test_abbr -if [[ $(abbr -s) == "abbr $test_abbr" ]]; then +message="abbr --clear-session " +abbr -S $test_abbr +abbr --clear-session +if [[ $(abbr --list-commands) == "" ]]; then message+="passed" else message+="failed" fi echo $message -message="abbr --erase " -abbr -e $test_abbr_abbreviation -if [[ $(abbr -s) == "" ]]; then +message="abbr --expand " +abbr $test_abbr +if [[ $(abbr --expand $test_abbr_abbreviation) == $test_abbr_expansion ]]; then message+="passed" else message+="failed" fi echo $message +abbr -e $test_abbr_abbreviation -message="abbr " +message="abbr --rename " abbr $test_abbr -if [[ $(abbr -s) == "abbr $test_abbr" ]]; then +abbr --rename $test_abbr_abbreviation ${test_abbr_abbreviation}_new +if [[ $(abbr -x ${test_abbr_abbreviation}_new) == $test_abbr_expansion ]]; then message+="passed" else message+="failed" fi echo $message -abbr -e $test_abbr_abbreviation +abbr -e ${test_abbr_abbreviation}_new -message="abbr -c " -abbr -S $test_abbr -abbr -c -if [[ $(abbr -s) == "" ]]; then +abbreviation=a +expansion="b'c'd" +message="abbr a=$expansion " +abbr $abbreviation=$expansion +if [[ $(abbr --expand $abbreviation) == $(echo $expansion) ]]; then message+="passed" else message+="failed" fi echo $message +abbr -e $abbreviation -message="abbr --clear-session " -abbr -S $test_abbr -abbr --clear-session -if [[ $(abbr -s) == "" ]]; then +abbreviation=a +expansion='b"c"d' +message="abbr a=$expansion " +abbr $abbreviation=$expansion +if [[ $(abbr --expand $abbreviation) == $(echo $expansion) ]]; then message+="passed" else message+="failed" fi echo $message +abbr -e $abbreviation -message="abbr -x " -abbr $test_abbr -if [[ $(abbr -x $test_abbr_abbreviation) == $test_abbr_expansion ]]; then +abbreviation=a +expansion='b'cd +message="abbr a='b'cd " +abbr $abbreviation=$expansion +if [[ $(abbr --expand $abbreviation) == $(echo $expansion) ]]; then message+="passed" else message+="failed" fi echo $message -abbr -e $test_abbr_abbreviation +abbr -e $abbreviation -message="abbr -R " -abbr $test_abbr -abbr -R $test_abbr_abbreviation ${test_abbr_abbreviation}_new -if [[ $(abbr -x ${test_abbr_abbreviation}_new) == $test_abbr_expansion ]]; then +abbreviation=a +expansion=b'c'd +message="abbr a=b'c'd " +abbr $abbreviation=$expansion +if [[ $(abbr --expand $abbreviation) == $(echo $expansion) ]]; then message+="passed" else message+="failed" fi echo $message -abbr -e ${test_abbr_abbreviation}_new +abbr -e $abbreviation + +abbreviation=a +expansion="b"cd +message="abbr a=\"b\"cd " +abbr $abbreviation=$expansion +if [[ $(abbr --expand $abbreviation) == $(echo $expansion) ]]; then + message+="passed" +else + message+="failed" +fi +echo $message +abbr -e $abbreviation + +abbreviation=a +expansion=b"c"d +message="abbr a=b\"c\"d " +abbr $abbreviation=$expansion +if [[ $(abbr --expand $abbreviation) == $(echo $expansion) ]]; then + message+="passed" +else + message+="failed" +fi +echo $message +abbr -e $abbreviation rm $ZSH_ABBR_USER_PATH diff --git a/zsh-abbr.zsh b/zsh-abbr.zsh index c97b45eb..90e987a0 100755 --- a/zsh-abbr.zsh +++ b/zsh-abbr.zsh @@ -227,8 +227,8 @@ _zsh_abbr() { return fi - abbreviation="${1%%=*}" - expansion="${1#*=}" + abbreviation="${(q)1%%=*}" + expansion="${(q)1#*=}" if [[ -z $abbreviation || -z $expansion || $abbreviation == $1 ]]; then util_error " add: Requires abbreviation and expansion" @@ -308,7 +308,7 @@ _zsh_abbr() { expansion=$(_zsh_abbr_global_expansion "$1") fi - echo - "$expansion" + echo - "${(Q)expansion}" } function export_aliases() { @@ -492,19 +492,11 @@ _zsh_abbr() { function util_add() { local abbreviation - local abbreviation_last_word local expansion - local quote local success=false abbreviation=$1 expansion=$2 - quote="\"" - - if [[ "${expansion:0:1}" == "${expansion: -1}" && "${expansion:0:1}" == [\'\"] ]]; then - quote=${expansion:0:1} - expansion="${expansion:1:-1}" - fi if [[ ${(w)#abbreviation} > 1 ]]; then util_error " add: ABBREVIATION ('$abbreviation') must be only one word" @@ -517,17 +509,17 @@ _zsh_abbr() { if $opt_scope_session; then if $opt_type_global; then - if ! (( ${+ZSH_ABBR_SESSION_GLOBALS[$1]} )); then + if ! (( ${+ZSH_ABBR_SESSION_GLOBALS[$abbreviation]} )); then if $opt_dry_run; then - echo "abbr -S -g $abbreviation=${quote}${expansion}${quote}" + echo "abbr -S -g $abbreviation=${(Q)expansion}" else ZSH_ABBR_SESSION_GLOBALS[$abbreviation]=$expansion fi success=true fi - elif ! (( ${+ZSH_ABBR_SESSION_COMMANDS[$1]} )); then + elif ! (( ${+ZSH_ABBR_SESSION_COMMANDS[$abbreviation]} )); then if $opt_dry_run; then - echo "abbr -S $abbreviation=${quote}${expansion}${quote}" + echo "abbr -S $abbreviation=${(Q)expansion}" else ZSH_ABBR_SESSION_COMMANDS[$abbreviation]=$expansion fi @@ -537,24 +529,24 @@ _zsh_abbr() { if $opt_type_global; then source "${TMPDIR:-/tmp}/zsh-user-global-abbreviations" - if ! (( ${+ZSH_ABBR_USER_GLOBALS[$1]} )); then + if ! (( ${+ZSH_ABBR_USER_GLOBALS[$abbreviation]} )); then if $opt_dry_run; then - echo "abbr -g $abbreviation=${quote}${expansion}${quote}" + echo "abbr -g $abbreviation=${(Q)expansion}" else ZSH_ABBR_USER_GLOBALS[$abbreviation]=$expansion - util_sync_user $quote + util_sync_user fi success=true fi else source "${TMPDIR:-/tmp}/zsh-user-abbreviations" - if ! (( ${+ZSH_ABBR_USER_COMMANDS[$1]} )); then + if ! (( ${+ZSH_ABBR_USER_COMMANDS[$abbreviation]} )); then if $opt_dry_run; then - echo "abbr $abbreviation=${quote}${expansion}${quote}" + echo "abbr ${(Q)abbreviation}=${(Q)expansion}" else ZSH_ABBR_USER_COMMANDS[$abbreviation]=$expansion - util_sync_user $quote + util_sync_user fi success=true fi @@ -644,7 +636,7 @@ _zsh_abbr() { result=$abbreviation if (( $include_expansion )); then - result+="=\"$expansion\"" + result+="=${(qqq)${(Q)expansion}}" fi if (( $include_cmd )); then @@ -655,15 +647,12 @@ _zsh_abbr() { } function util_sync_user() { - local quote local user_updated if [[ -n "$ZSH_ABBR_NO_SYNC_USER" ]]; then return fi - quote="${1:-\"}" - user_updated="${TMPDIR:-/tmp}/zsh-user-abbreviations"_updated rm "$user_updated" 2> /dev/null touch "$user_updated" @@ -671,12 +660,12 @@ _zsh_abbr() { typeset -p ZSH_ABBR_USER_GLOBALS > "${TMPDIR:-/tmp}/zsh-user-global-abbreviations" for abbreviation expansion in ${(kv)ZSH_ABBR_USER_GLOBALS}; do - echo "abbr -g ${abbreviation}=${quote}$expansion${quote}" >> "$user_updated" + echo "abbr -g ${abbreviation}=${(qqq)${(Q)expansion}}" >> "$user_updated" done typeset -p ZSH_ABBR_USER_COMMANDS > "${TMPDIR:-/tmp}/zsh-user-abbreviations" for abbreviation expansion in ${(kv)ZSH_ABBR_USER_COMMANDS}; do - echo "abbr ${abbreviation}=${quote}$expansion${quote}" >> "$user_updated" + echo "abbr ${abbreviation}=${(qqq)${(Q)expansion}}" >> "$user_updated" done mv "$user_updated" "$ZSH_ABBR_USER_PATH" @@ -1042,7 +1031,7 @@ _zsh_abbr_expand_widget() { if [[ -n "$expansion" ]]; then local preceding_lbuffer preceding_lbuffer="${LBUFFER%%$word}" - LBUFFER="$preceding_lbuffer$expansion" + LBUFFER="$preceding_lbuffer${(Q)expansion}" fi }