Skip to content

Commit

Permalink
feat,docs,test(abbr): quotation marks are preserved [#10]
Browse files Browse the repository at this point in the history
  • Loading branch information
olets committed Mar 22, 2020
1 parent 0723d6b commit 5a3b905
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 64 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
104 changes: 70 additions & 34 deletions tests/tests.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 17 additions & 28 deletions zsh-abbr.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -308,7 +308,7 @@ _zsh_abbr() {
expansion=$(_zsh_abbr_global_expansion "$1")
fi

echo - "$expansion"
echo - "${(Q)expansion}"
}

function export_aliases() {
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -644,7 +636,7 @@ _zsh_abbr() {

result=$abbreviation
if (( $include_expansion )); then
result+="=\"$expansion\""
result+="=${(qqq)${(Q)expansion}}"
fi

if (( $include_cmd )); then
Expand All @@ -655,28 +647,25 @@ _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"
chmod 600 "$user_updated"

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"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 5a3b905

Please sign in to comment.