Skip to content

Commit

Permalink
remove indirect ref antipattern due to lack of nested arrays in zsh
Browse files Browse the repository at this point in the history
  • Loading branch information
MenkeTechnologies committed Dec 11, 2021
1 parent c336917 commit b6f6713
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Expansion on history, globs, parameters and mispellings occurs when cursor is on
There is optional expansion of unexpanded line into history on accepting current line with enter key. Expansion occurs for aliases not spelling correction. Globs and other expansions can be optionally enabled on accepting line. See [below](#optional-expansion-of-unexpanded-line-into-history-on-enter).

## Demo
[![asciicast](https://asciinema.org/a/wRe4hwlGNdEbeHk7gZ25sRq5U.svg)](https://asciinema.org/a/wRe4hwlGNdEbeHk7gZ25sRq5U)
[![asciicast](https://asciinema.org/a/FbFsuMCSLtooqkB5ZZbBZeiEg.svg)](https://asciinema.org/a/FbFsuMCSLtooqkB5ZZbBZeiEg)

## Comparison to other expanding abbrevation libraries
### [zsh-abbr](https://github.com/olets/zsh-abbr)
Expand All @@ -37,9 +37,9 @@ There is optional expansion of unexpanded line into history on accepting current
- zsh-expand expands incorrect spellings and phrases, globs, command/process substitution, =command expansion, history expansion and $parameters

## Bypassing expansion
Expansion can be temporarily bypassed with control-space. The variable `ZPWR_EXPAND_BLACKLIST` which should be an array of blacklisted items will blacklist items permanently.
Expansion can be temporarily bypassed with control-space. The variable `ZPWR_EXPAND_BLACKLIST` can be set to an array of regular or global aliases which will not be expanded.

You can override the following environment variables to control expansion.
You can override the following variables to control expansion.
```sh
# ignore expansion of these regular/global aliases
export ZPWR_EXPAND_BLACKLIST=(g gco)
Expand Down
12 changes: 5 additions & 7 deletions zpwrExpandApi.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ function zpwrExpandParseWords(){

ZPWR_EXPAND_WORDS_LPARTITION=( $mywordsleft[$firstIndex,$#mywordsleft] )

ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION]=ZPWR_EXPAND_WORDS_LPARTITION
#zpwrLogDebug "lpartition = '$ZPWR_EXPAND_WORDS_LPARTITION'"

#zpwrLogDebug "lpartition = '${(P)ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION]}'"

lpartAry=( ${(z)${(P)ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION]}} )
lpartAry=( ${(z)${ZPWR_EXPAND_WORDS_LPARTITION}} )

ZPWR_VARS[firstword_partition]=${lpartAry[1]}

Expand Down Expand Up @@ -98,7 +96,7 @@ function zpwrExpandLastWordAtCommandPosAndExpand(){
local caller=$2
local triggerKey=$3

if (( ${(P)#ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION]} == 1 )); then
if (( $#ZPWR_EXPAND_WORDS_LPARTITION == 1 )); then
if [[ $caller == zle ]]; then
zpwrExpandGetAliasValue
words=(${(z)ZPWR_VARS[EXPANDED]})
Expand All @@ -118,7 +116,7 @@ function zpwrExpandLastWordAtCommandPosAndExpand(){
ZPWR_VARS[LAST_WORD_WAS_AT_COMMAND]=true
ZPWR_VARS[ORIGINAL_LAST_COMMAND]=$ZPWR_VARS[lastword_lbuffer]

elif (( ${(P)#ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION]} >= 2 )); then
elif (( $#ZPWR_EXPAND_WORDS_LPARTITION >= 2 )); then
# regular alias expansion after sudo
if [[ $ZPWR_EXPAND_SECOND_POSITION == true ]]; then

Expand All @@ -144,7 +142,7 @@ function zpwrExpandLastWordAtCommandPosAndExpand(){
fi
else
# here if not called by supernatural space fn
if [[ "${(P)ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION]}" =~ "$ZPWR_VARS[continueFirstPositionRegex]" ]];then
if [[ "$ZPWR_EXPAND_WORDS_LPARTITION" =~ "$ZPWR_VARS[continueFirstPositionRegex]" ]];then
ZPWR_EXPAND_PRE_EXPAND=("${(z)match[-1]}")

if (( $#ZPWR_EXPAND_PRE_EXPAND == 1)); then
Expand Down
12 changes: 6 additions & 6 deletions zpwrExpandLib.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ function zpwrExpandCorrectWord(){
return
fi

if (( ${(P)#ZPWR_VARS[ZPWR_EXPAND_WORDS_PARTITION]} == 1)); then
if (( $#ZPWR_EXPAND_WORDS_PARTITION == 1)); then
if type -a $ZPWR_VARS[firstword_partition] &>/dev/null; then
#zpwrLogDebug "No correction from 1 word => '"'$ZPWR_VARS[firstword_partition]'"'_____ = ""'$ZPWR_VARS[firstword_partition]'"
# git<space>
return
fi
else

if [[ "${(P)ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION]}" =~ "$ZPWR_VARS[continueFirstPositionRegexNoZpwr]" ]];then
if [[ "$ZPWR_EXPAND_WORDS_LPARTITION" =~ "$ZPWR_VARS[continueFirstPositionRegexNoZpwr]" ]];then
ZPWR_EXPAND_PRE_CORRECT=("${(z)match[-1]}")

#zpwrLogDebug "${match[@]}"
Expand All @@ -84,7 +84,7 @@ function zpwrExpandCorrectWord(){
fi

else
#zpwrLogDebug "no match ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION] '$ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION]'"
#zpwrLogDebug "no match ZPWR_EXPAND_WORDS_LPARTITION '$ZPWR_EXPAND_WORDS_LPARTITION'"
zpwrLogConsoleErr zpwr expand should not reach here
return
fi
Expand Down Expand Up @@ -245,13 +245,13 @@ function zpwrExpandSupernaturalSpace() {

if [[ $ZPWR_VARS[foundIncorrect] = true && $ZPWR_CORRECT_EXPAND = true ]]; then
#zpwrLogDebug "RE-EXPAND after incorrect spelling"
ZPWR_EXPAND_PRE_EXPAND=("${ZPWR_EXPAND_POST_CORRECT[@]}")
ZPWR_EXPAND_PRE_EXPAND=( "${ZPWR_EXPAND_POST_CORRECT[@]}" )
zpwrExpandParseWords "$LBUFFER"
else
ZPWR_EXPAND_PRE_EXPAND=("${ZPWR_EXPAND_PRE_CORRECT[@]}")
ZPWR_EXPAND_PRE_EXPAND=( "${ZPWR_EXPAND_PRE_CORRECT[@]}" )
fi
else
ZPWR_EXPAND_PRE_EXPAND=("${ZPWR_EXPAND_PRE_CORRECT[@]}")
ZPWR_EXPAND_PRE_EXPAND=( "${ZPWR_EXPAND_PRE_CORRECT[@]}" )
fi

#dont expand =word because that is zle expand-word
Expand Down

0 comments on commit b6f6713

Please sign in to comment.