Skip to content

Commit

Permalink
all functions in zpwr namespace and separate library file
Browse files Browse the repository at this point in the history
  • Loading branch information
MenkeTechnologies committed Sep 5, 2020
1 parent 328be3c commit c8aac11
Show file tree
Hide file tree
Showing 3 changed files with 585 additions and 572 deletions.
31 changes: 15 additions & 16 deletions expand-api.zsh → zpwrExpandApi.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
##### Notes:
# Usage for external service like fzf. Must have BUFFER, LBUFFER, RBUFFER set like ZLE does.
#
# parseWords
# isLastWordLastCommand
# zpwrExpandParseWords
# zpwrExpandIsLastWordLastCommand
#
# if $ZPWR_VARS[LAST_WORD_WAS_LAST_COMMAND] == true; then
# echo $ZPWR_VARS[ORIGINAL_LAST_COMMAND]
Expand All @@ -18,7 +18,7 @@
#
#}}}***********************************************************

function parseWords(){
function zpwrExpandParseWords(){

local i lastword_partition firstIndex lastIndex finalWord
local -a mywordsleft mywordsright mywordsall lbufAry lpartAry lastWordAry partitionAry
Expand Down Expand Up @@ -105,7 +105,7 @@ function parseWords(){
loggDebug "last word no special chars...${ZPWR_VARS[lastword_remove_special]}..."
}

function isLastWordLastCommand(){
function zpwrExpandIsLastWordLastCommand(){

local moveCursor=$1
local expand=$2
Expand All @@ -117,18 +117,17 @@ function isLastWordLastCommand(){
LBUFFER="${LBUFFER:0:-1}"
fi
if [[ $expand == expand ]]; then
commonParameterExpansion
zpwrExpandCommonParameterExpansion
words=(${(z)ZPWR_VARS[EXPANDED]})
if [[ ${words[1]} == "$ZPWR_VARS[lastword_lbuffer]" ]];then
# escape the expanded form because its first word is an alias itsef
zshExpandAliasEscape
goToTabStopOrEndOfLBuffer
zpwrExpandAliasEscape
zpwrExpandGoToTabStopOrEndOfLBuffer
else
zshExpandAlias
zpwrExpandAlias
fi
LBUFFER=${LBUFFER//$ZPWR_VARS[subForAtSign]/@}
if [[ $moveCursor == moveCursor ]]; then
goToTabStopOrEndOfLBuffer
zpwrExpandGoToTabStopOrEndOfLBuffer
fi
fi
ZPWR_VARS[LAST_WORD_WAS_LAST_COMMAND]=true
Expand All @@ -140,10 +139,10 @@ function isLastWordLastCommand(){
if [[ "$ZPWR_VARS[firstword_partition]" =~ $ZPWR_VARS[continueFirstPositionRegex] ]];then
loggDebug "matched $ZPWR_VARS[firstword_partition] with $ZPWR_VARS[continueFirstPositionRegex] with 2 == ${(P)#ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION]}"
if [[ $expand == expand ]]; then
commonParameterExpansion
zshExpandAlias
zpwrExpandCommonParameterExpansion
zpwrExpandAlias
if [[ $moveCursor == moveCursor ]]; then
goToTabStopOrEndOfLBuffer
zpwrExpandGoToTabStopOrEndOfLBuffer
fi
fi
ZPWR_VARS[LAST_WORD_WAS_LAST_COMMAND]=true
Expand Down Expand Up @@ -180,11 +179,11 @@ function isLastWordLastCommand(){
done
if [[ $shouldStopExpansionDueToFailedRegex == false ]]; then
if [[ $expand == expand ]]; then
commonParameterExpansion
zshExpandAlias
zpwrExpandCommonParameterExpansion
zpwrExpandAlias

if [[ $moveCursor == moveCursor ]]; then
goToTabStopOrEndOfLBuffer
zpwrExpandGoToTabStopOrEndOfLBuffer
fi
fi
ZPWR_VARS[LAST_WORD_WAS_LAST_COMMAND]=true
Expand Down
279 changes: 279 additions & 0 deletions zpwrExpandLib.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
#!/usr/bin/env zsh
#{{{ MARK:Header
#**************************************************************
##### Author: MenkeTechnologies
##### GitHub: https://github.com/MenkeTechnologies
##### Date: Sat Sep 5 18:51:48 EDT 2020
##### Purpose: zsh script to expand
##### Notes:
#}}}***********************************************************

#{{{ MARK:accessory functions
#**************************************************************
# dummy function if plugin used outside of zpwr
if ! type -- "loggDebug" &>/dev/null;then
function loggDebug(){
:
}
fi

function zpwrExpandGoToTabStopOrEndOfLBuffer(){

local lenToFirstTS

lenToFirstTS=${#LBUFFER%%$ZPWR_TABSTOP*}

if (( $lenToFirstTS < ${#LBUFFER} )); then
loggDebug "this is a tabstop"
# check if numbered tabstop
# if num tabstop then set flag in ZPWR_VARS with value to all indexes
# sub out all num tabstops
# insert key fn will send key to all indexes apply any modifications
# keep track of num keys to have activated region
ZPWR_VARS[indexTS$num]=$lenToFirstTS

CURSOR=$lenToFirstTS
RBUFFER=${RBUFFER:$#ZPWR_TABSTOP}
ZPWR_VARS[NEED_TO_ADD_SPACECHAR]=false
else
ZPWR_VARS[NEED_TO_ADD_SPACECHAR]=true
fi
}

function zpwrExpandNonFileExpansion(){
:
#DNS lookups
#type -a "$lastWord" &> /dev/null || {
#print -r -- $lastWord | command grep -qE \
#'^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}\.?$'\
#&& {
##DNS lookup
#A_Record=$(host $lastWord) 2>/dev/null \
#&& {
#A_Record=$(print -r -- $A_Record | command grep ' address' | head -1 | awk '{print $4}')
#} || A_Record=bad
#[[ $A_Record != bad ]] && \
#LBUFFER="$(print -r -- "$LBUFFER" | sed -E "s@\\b$lastWord@$A_Record@g")"
#} || {
#print -r -- $lastWord | command grep -qE \
#'\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' && {
##reverse DNS lookup
#PTR_Record=$(nslookup $lastWord) 2>/dev/null && {
#PTR_Record=$(print -r -- $PTR_Record | command grep 'name = ' | tail -1 | awk '{print $4}')
#} || PTR_Record=bad
#[[ $PTR_Record != bad ]] && \
#LBUFFER="$(print -r -- "$LBUFFER" | sed -E "s@\\b$lastWord\\b@${PTR_Record:0:-1}@g")"
#}
#}
#}
}

function zpwrExpandCorrectWord(){

local word nextWord badWords misspelling key res1

if (( ${(P)#ZPWR_VARS[ZPWR_EXPAND_WORDS_PARTITION]} == 1)); then
if type -a $ZPWR_VARS[firstword_partition] &>/dev/null; then
loggDebug "No correction from 1 word => '"'$ZPWR_VARS[firstword_partition]'"'_____ = ""'$ZPWR_VARS[firstword_partition]'"
return
fi
else
if [[ $ZPWR_VARS[firstword_partition] =~ $ZPWR_VARS[continueFirstPositionRegex] ]];then
for (( i = 2; i <= ${(P)#ZPWR_VARS[ZPWR_EXPAND_WORDS_PARTITION]}; ++i )); do

word=${(P)ZPWR_VARS[ZPWR_EXPAND_WORDS_PARTITION][$i]}
nextWord=${(P)ZPWR_VARS[ZPWR_EXPAND_WORDS_PARTITION][$i+1]}

if [[ "$word $nextWord" =~ $ZPWR_VARS[continueOptionSpaceArgSecondAndOnwardsPositionRegex] ]]; then

loggDebug "matched grep -Eqv '$ZPWR_VARS[continueOptionSpaceArgSecondAndOnwardsPositionRegex]' for word:'$word $nextWord'"
if (( (i + 1) < ${(P)#ZPWR_VARS[ZPWR_EXPAND_WORDS_PARTITION]} )); then
((++i))
continue
fi
fi

if ((i == ${(P)#ZPWR_VARS[ZPWR_EXPAND_WORDS_PARTITION]} )); then

if type -a $word &>/dev/null; then
loggDebug "No correction from >= 2 words => '"'$word'"'_____ = ""'$word'"
return
else
break
fi
elif ! [[ $word =~ $ZPWR_VARS[continueSecondAndOnwardsPositionRegex] ]]; then
break
fi
done
fi
fi

loggDebug "______'"'attempt correction'"'_____ = ""'$ZPWR_VARS[lastword_remove_special]'"

for key in ${(k)ZPWR_EXPAND_CORRECT_WORDS[@]}; do

badWords=("${(z)ZPWR_EXPAND_CORRECT_WORDS[$key]}")
for misspelling in $badWords[@];do

if [[ ${ZPWR_VARS[lastword_remove_special]} == $misspelling ]]; then

# expand
[[ $LBUFFER == (#b)(*[[:space:]]#)($misspelling) ]];
res1=${match[1]}
# expand
LBUFFER="$res1${key:gs/_/ /}"

# ZPWR_VARS[finished]=true
ZPWR_VARS[foundIncorrect]=true
CURSOR=$#LBUFFER
break
fi
done
if [[ $ZPWR_VARS[finished] == true ]];then
zle self-insert
return 0
fi
done
}

function zpwrExpandCommonParameterExpansion(){

ZPWR_VARS[EXPANDED]="${$(alias -r $ZPWR_VARS[lastword_lbuffer])#*=}"
# deal with ansi quotes $'
[[ $ZPWR_VARS[EXPANDED][1] == \$ ]] && ZPWR_VARS[EXPANDED]=${ZPWR_VARS[EXPANDED]:1}
ZPWR_VARS[EXPANDED]=${(Q)ZPWR_VARS[EXPANDED]}
}

function zpwrExpandAliasEscape(){

local res1 result

[[ $LBUFFER == (#b)(*[[:space:]]#)($ZPWR_VARS[lastword_lbuffer]) ]]
res1=${match[1]}
# expand
LBUFFER="$res1\\$ZPWR_VARS[EXPANDED]"

}

function zpwrExpandAlias(){

local res1 result

[[ $LBUFFER == (#b)(*[[:space:]]#)($ZPWR_VARS[lastword_lbuffer]) ]]
res1=${match[1]}
# expand
LBUFFER="$res1$ZPWR_VARS[EXPANDED]"

}

function zpwrExpandGlobalAliases() {

local res1 result

ZPWR_VARS[lastword_lbuffer]="$1"
result=$galiases[$ZPWR_VARS[lastword_lbuffer]]
[[ $LBUFFER == (#b)(*[[:space:]]#)($ZPWR_VARS[lastword_lbuffer]) ]]

res1=${match[1]}
# expand
LBUFFER="$res1$result"

zpwrExpandGoToTabStopOrEndOfLBuffer
}

#}}}***********************************************************

#{{{ MARK:main fn
#**************************************************************
function zpwrExpandSupernaturalSpace() {

if [[ $ZPWR_TRACE == true ]]; then
set -x
fi

local tempBuffer mywords badWords word nextWord i shouldStopExpansionDueToFailedRegex words ary res1 aliasOut
ZPWR_VARS[finished]=false

zpwrExpandParseWords

if [[ $ZPWR_CORRECT == true ]]; then
zpwrExpandCorrectWord
fi

if [[ $ZPWR_VARS[foundIncorrect] = true && $ZPWR_CORRECT_EXPAND = true ]]; then
loggDebug "RE-EXPAND after incorrect spelling"
zpwrExpandParseWords
fi

ZPWR_VARS[NEED_TO_ADD_SPACECHAR]=true
ZPWR_VARS[LAST_WORD_WAS_LAST_COMMAND]=false

#dont expand =word because that is zle expand-word
if [[ ${ZPWR_VARS[lastword_lbuffer]:0:1} != '=' ]] && (( $#ZPWR_VARS[lastword_lbuffer] > 0 ));then
aliasOut=$(alias -r -- $ZPWR_VARS[lastword_lbuffer])
if [[ -n $aliasOut ]] && ! [[ $aliasOut =~ $ZPWR_VARS[blacklistFirstPosRegex] ]];then

loggDebug "regular=>'$ZPWR_VARS[lastword_lbuffer]'"

zpwrExpandIsLastWordLastCommand moveCursor expand
else
loggDebug "NOT regular=>'$ZPWR_VARS[lastword_lbuffer]'"
# remove space from menuselect spacebar
if [[ ${LBUFFER: -1} == " " && ZPWR_VARS[NEED_TO_ADD_SPACECHAR] == true ]]; then
loggDebug "removing space menu select"
LBUFFER="${LBUFFER:0:-1}"
fi
if [[ "$ZPWR_VARS[lastword_lbuffer]" =~ '"' ]]; then
# expand on last word of "string" for global aliases only
ZPWR_VARS[lastword_lbuffer]=${ZPWR_VARS[lastword_lbuffer]:gs/\"//}
ary=(${(z)ZPWR_VARS[lastword_lbuffer]})
ZPWR_VARS[lastword_lbuffer]=$ary[-1]
fi
if [[ $(alias -g -- $ZPWR_VARS[lastword_lbuffer]) =~ "." ]]; then
# global alias expansion
if [[ ${LBUFFER: -1} == " " ]]; then
loggDebug "removing space global alias menu select"
LBUFFER="${LBUFFER:0:-1}"
fi
loggDebug "global=>'$ZPWR_VARS[lastword_lbuffer]'"
zpwrExpandGlobalAliases "$ZPWR_VARS[lastword_lbuffer]"
ZPWR_VARS[LAST_WORD_WAS_LAST_COMMAND]=true
ZPWR_VARS[ORIGINAL_LAST_COMMAND]=$ZPWR_VARS[lastword_lbuffer]
fi
fi
if [[ ! -f "$ZPWR_VARS[lastword_lbuffer]" ]]; then
zpwrExpandNonFileExpansion
else
:
fi
fi
if [[ $ZPWR_VARS[LAST_WORD_WAS_LAST_COMMAND] != true ]]; then
# expand file globs, history expansions, command expansion, parameter expansion and =command
if [[ $ZPWR_EXPAND_NATIVE == true ]]; then
zle expand-word
fi
fi

loggDebug "ZPWR_VARS[NEED_TO_ADD_SPACECHAR] = $ZPWR_VARS[NEED_TO_ADD_SPACECHAR]"
loggDebug "ZPWR_VARS[LAST_WORD_WAS_LAST_COMMAND] = $ZPWR_VARS[LAST_WORD_WAS_LAST_COMMAND]"

if [[ $ZPWR_VARS[NEED_TO_ADD_SPACECHAR] == true ]];then
# insert the space char
if [[ $LBUFFER[-1] != ' ' ]]; then
zle self-insert
else
if [[ $ZPWR_VARS[LAST_WORD_WAS_LAST_COMMAND] != true ]]; then
zle self-insert
fi
fi
fi
if [[ $ZPWR_TRACE == true ]]; then
set +x
fi
}

zpwrExpandTerminateSpace(){

LBUFFER+=" "
}
#}}}***********************************************************
Loading

0 comments on commit c8aac11

Please sign in to comment.