-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathzpwrExpandApi.zsh
More file actions
executable file
·202 lines (173 loc) · 7.77 KB
/
Copy pathzpwrExpandApi.zsh
File metadata and controls
executable file
·202 lines (173 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#lang!/usr/bin/env zsh
#{{{ MARK:Header
#**************************************************************
##### Author: MenkeTechnologies
##### GitHub: https://github.com/MenkeTechnologies
##### Date: Fri Aug 14 15:12:03 EDT 2020
##### Purpose: zsh script to hold expand lib fns
##### Notes:
# Usage for external service like fzf. Must have ZPWR_VARS set
#
# zpwrExpandParseWords
# zpwrExpandLastWordAtCommandPosAndExpand
#
# if $ZPWR_VARS[LAST_WORD_WAS_AT_COMMAND] == true; then
# echo $ZPWR_VARS[ORIGINAL_LAST_COMMAND]
# fi
#
#
#}}}***********************************************************
function zpwrExpandParseWords(){
local i lastword_partition firstIndex lastIndex finalWord tmp
local -a mywordsleft mywordsright mywordsall lbufAry lpartAry lastWordAry partitionAry
local -i isArgPosition=0
local -a tailWords=()
# loop through words to get first and last words in partition
tmp=${1}
tmp=( ${(z)tmp} )
# arithmetic expansion $(()) is not a command position so replace with junk value
tmp[-1]=${tmp[-1]//[\$]\(\(/no_cmd_$RANDOM}
# change <(, =(, $( to ; for word splitting
tmp[-1]=${tmp[-1]//[\<\=\$]\(/;}
# change ` to ; for word splitting
tmp[-1]=${tmp[-1]:gs/\`/;/}
mywordsleft=( ${(Az)tmp} )
#zpwrLogDebug "my words left = $mywordsleft"
# we must find the first index of the partition
firstIndex=0
for (( i = $#mywordsleft; i >= 1; i-- )); do
# ;; ; | || && are statement separating chars
# we will split the command line and get the statement of the cursor
# regular aliases are valid in the first position of this statement
case $mywordsleft[$i] in
';;' | \; | \| | '||' | '&&' | '(' | '{')
firstIndex=$((i + 1))
break
;;
'>'* | '<'* | '&>'*)
# remove redirection operator (same index again removes target after shift)
mywordsleft[$i]=()
mywordsleft[$i]=()
;;
*)
;;
esac
done
ZPWR_EXPAND_WORDS_LPARTITION=( $mywordsleft[$firstIndex,$#mywordsleft] )
# use parser to find command position — strips assignments contextually,
# consumes shell keywords and command wrappers with their flags
ZPWR_VARS[cachedRegexMatch]=""
ZPWR_VARS[cachedRegexMatched]=false
if (( $#ZPWR_EXPAND_WORDS_LPARTITION > 1 )); then
zpwrExpandParserFindCommandPosition
if [[ -n $ZPWR_VARS[cachedRegexMatch] || $ZPWR_VARS[cachedRegexMatched] == true ]]; then
ZPWR_VARS[cachedRegexMatched]=true
tailWords=( ${(z)ZPWR_VARS[cachedRegexMatch]} )
(( $#tailWords > 1 )) && isArgPosition=1
else
isArgPosition=1
fi
fi
if (( isArgPosition )); then
if [[ $ZPWR_EXPAND_QUOTE_DOUBLE == true ]]; then
ZPWR_EXPAND_WORDS_LPARTITION[-1]=${ZPWR_EXPAND_WORDS_LPARTITION[-1]:gs/\"//}
fi
if [[ $ZPWR_EXPAND_QUOTE_SINGLE == true ]]; then
ZPWR_EXPAND_WORDS_LPARTITION[-1]=${ZPWR_EXPAND_WORDS_LPARTITION[-1]:gs/\'//}
fi
fi
#zpwrLogDebug "lpartition = '$ZPWR_EXPAND_WORDS_LPARTITION'"
# Avoid join+(z) on the whole partition: only the last word is modified for
# quote stripping in argument position, so only that token may need (z) re-split.
# Use (@) so [1,-2] stays an array slice — a plain "${arr[1,-2]}" joins into one string.
if (( isArgPosition )); then
lpartAry=( "${(@)ZPWR_EXPAND_WORDS_LPARTITION[1,-2]}" )
lpartAry+=( ${(z)ZPWR_EXPAND_WORDS_LPARTITION[-1]} )
else
lpartAry=( "${ZPWR_EXPAND_WORDS_LPARTITION[@]}" )
fi
ZPWR_VARS[firstword_partition]=${lpartAry[1]}
# skip words containing = (assignments and --flag=value) for lastword.
# native glob filter — one expansion, no per-element loop or appends
local -a _noeq
_noeq=("${(@)lpartAry:#*=*}")
ZPWR_VARS[lastword_lbuffer]=${_noeq[-1]:-''}
#zpwrLogDebug "first word partition = ...$ZPWR_VARS[firstword_partition]..."
#zpwrLogDebug "last word lbuf before no dbl quotes and [-1] = ...$ZPWR_VARS[lastword_lbuffer]..."
lbufAry=( ${(z)${ZPWR_VARS[lastword_lbuffer]}} )
if (( $#lbufAry )); then
ZPWR_VARS[lastword_lbuffer]=${lbufAry[-1]}
fi
#zpwrLogDebug "last word lbuf after no dbl quotes and [-1] = ...$ZPWR_VARS[lastword_lbuffer]..."
#zpwrLogDebug "first word partition before spelling = ...$ZPWR_VARS[firstword_partition]..."
#zpwrLogDebug "last word lbuf before spelling = ...$ZPWR_VARS[lastword_lbuffer]..."
lastWordAry=( ${(Az)${ZPWR_VARS[lastword_lbuffer]//[\[\]\{\}\(\)]/}} )
finalWord=${lastWordAry[-1]}
ZPWR_VARS[lastword_remove_special]=$finalWord
#zpwrLogDebug "last word no special chars...${ZPWR_VARS[lastword_remove_special]}..."
}
function zpwrExpandLastWordAtCommandPosAndExpand(){
local cursorAction=$1
local caller=$2
local triggerKey=$3
if (( $#ZPWR_EXPAND_WORDS_LPARTITION == 1 )); then
if [[ $caller == zle ]]; then
zpwrExpandGetAliasValue
words=(${(z)ZPWR_VARS[EXPANDED]})
if [[ ${words[1]} == "$ZPWR_VARS[lastword_lbuffer]" ]];then
# escape the expanded form because its first word is an alias itself
zpwrExpandAliasEscape
else
if [[ $ZPWR_TRACE == true ]]; then
set -x
fi
zpwrExpandAlias
fi
if [[ $ZPWR_VARS[WAS_EXPANDED] == true && $cursorAction == moveCursor ]]; then
zpwrExpandGoToTabStopOrEndOfLBuffer
fi
fi
ZPWR_VARS[LAST_WORD_WAS_AT_COMMAND]=true
ZPWR_VARS[ORIGINAL_LAST_COMMAND]=$ZPWR_VARS[lastword_lbuffer]
elif (( $#ZPWR_EXPAND_WORDS_LPARTITION >= 2 )); then
# regular alias expansion after sudo
if [[ $ZPWR_EXPAND_SECOND_POSITION == true ]]; then
if [[ -n "$ZPWR_EXPAND_PRE_EXPAND" ]]; then
#zpwrLogDebug "${ZPWR_EXPAND_PRE_EXPAND[@]}"
if [[ $triggerKey == "${ZPWR_VARS[ENTER_KEY]}" && $ZPWR_EXPAND_PRE_EXEC_SECOND_POSITION != true ]]; then
return
fi
if (( $#ZPWR_EXPAND_PRE_EXPAND == 1)) && [[ -n $ZPWR_EXPAND_PRE_EXPAND[1] ]]; then
if [[ $caller == zle ]]; then
zpwrExpandGetAliasValue
zpwrExpandAlias
if [[ $ZPWR_VARS[WAS_EXPANDED] == true && $cursorAction == moveCursor && $triggerKey != "${ZPWR_VARS[ENTER_KEY]}" ]]; then
zpwrExpandGoToTabStopOrEndOfLBuffer
fi
fi
ZPWR_VARS[LAST_WORD_WAS_AT_COMMAND]=true
ZPWR_VARS[ORIGINAL_LAST_COMMAND]=$ZPWR_VARS[lastword_lbuffer]
else
ZPWR_VARS[NEED_TO_ADD_SPACECHAR]=true
fi
else
# here if not called by supernatural space fn
if zpwrExpandRegexMatchOnCommandPosition; then
if (( $#ZPWR_EXPAND_PRE_EXPAND == 1)) && [[ -n $ZPWR_EXPAND_PRE_EXPAND[1] ]]; then
if [[ $caller == zle ]]; then
zpwrExpandGetAliasValue
fi
ZPWR_VARS[LAST_WORD_WAS_AT_COMMAND]=true
ZPWR_VARS[ORIGINAL_LAST_COMMAND]=$ZPWR_VARS[lastword_lbuffer]
else
ZPWR_VARS[NEED_TO_ADD_SPACECHAR]=true
fi
else
ZPWR_VARS[NEED_TO_ADD_SPACECHAR]=true
#zpwrLogDebug "no match ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION] '$ZPWR_VARS[ZPWR_EXPAND_WORDS_LPARTITION]'"
return
fi
fi
fi
fi
}