You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
powerlevel10k/internal/parse.zsh

187 lines
3.5 KiB
Bash

5 years ago
typeset -gA _p9k_skip_token=(
'}' ''
'|' ''
'||' ''
'&' ''
'&&' ''
'|&' ''
'&!' ''
'&|' ''
')' ''
'(' ''
'{' ''
'()' ''
'!' ''
';' ''
'if' ''
'fi' ''
'elif' ''
'else' ''
5 years ago
'then' ''
'while' ''
'until' ''
'do' ''
'coproc' ''
'nocorrect' ''
'time' ''
'-' ''
'builtin' ''
'[[' '\]\]'
'((' '\)\)'
'case' '\)|esac'
';;' '\)|esac'
';&' '\)|esac'
';|' '\)|esac'
'&>' '*'
'>' '*'
'>&' '*'
'<' '*'
'<&' '*'
'<>' '*'
'&>|' '*'
'>|' '*'
'&>>' '*'
'>>' '*'
'>>&' '*'
'&>>|' '*'
'>>|' '*'
)
typeset -gA _p9k_redirect=(
'&>' ''
'>' ''
'>&' ''
'<' ''
'<&' ''
'<>' ''
'&>|' ''
'>|' ''
'&>>' ''
'>>' ''
'>>&' ''
'&>>|' ''
'>>|' ''
)
typeset -gA _p9k_term=(
'|' ''
'||' ''
';' ''
'&' ''
'&&' ''
'|&' ''
'&!' ''
'&|' ''
';;' ''
';&' ''
';|' ''
')' ''
)
5 years ago
function _p9k_extract_commands() {
local rcquotes
[[ -o rcquotes ]] && rcquotes=(-o rcquotes)
emulate -L zsh -o extended_glob -o no_nomatch $rcquotes
typeset -ga _p9k_commands=()
5 years ago
local -i e
local id='$(<->|[[:alpha:]_][[:IDENT:]]#)'
local skip n s r var="\$$id|\${$id}|\"\$$id\"|\"\${$id}\""
local -a aln alp alf v commands match mbegin mend
5 years ago
5 years ago
[[ -o interactive_comments ]] && local tokens=(${(Z+C+)1}) || local tokens=(${(z)1})
5 years ago
while (( $#tokens )); do
if (( $#tokens == aln[-1] )); then
aln[-1]=()
alp[-1]=()
if (( $#tokens == alf[-1] )); then
alf[-1]=()
e=0
else
e=$#skip
fi
else
e=$#skip
fi
while (( $#tokens )) || break; do
token=$tokens[1]
shift 1 tokens
if (( $+galiases[$token] )); then
(( $aln[(eI)p$token] )) && break
n=p$token
s=$galiases[$token]
elif (( e )); then
break
elif (( $+aliases[$token] )); then
(( $aln[(eI)p$token] )) && break
n=p$token
s=$aliases[$token]
elif [[ $token == (#b)?*.(?*) ]] && (( $+saliases[$match[1]] )); then
(( $aln[(eI)s$match[1]] )) && break
n=s$match[1]
s=${saliases[$match[1]]%% #}
else
break
fi
aln+=$n
alp+=$#tokens
[[ $s == *' ' ]] && alf+=$#tokens
[[ -o interactive_comments ]] && tokens[1,0]=(${(Z+C+)s}) || tokens[1,0]=(${(z)s})
done
if [[ -n $skip ]]; then
if [[ $token == $~skip ]]; then
skip=
if (( $+_p9k_term[$token] )); then
[[ $token == ';'[';&|'] ]] && skip='\)|esac'
_p9k_commands+=($commands)
commands=()
elif [[ $token == '()' ]]; then
commands=()
fi
5 years ago
fi
continue
fi
r=${token#<0-255>}
5 years ago
if (( $+_p9k_skip_token[$r] )); then
if (( $+_p9k_skip_token[$token] )); then
5 years ago
skip=$_p9k_skip_token[$token]
5 years ago
continue
fi
if (( $+_p9k_redirect[$r] )); then
5 years ago
skip='*'
5 years ago
continue
fi
fi
if [[ $token == *=* ]]; then
5 years ago
v=${(S)token/#(<->|([[:alpha:]_][[:IDENT:]]#(|'['*[^\\](\\\\)#']')))(|'+')=}
5 years ago
if (( $#v < $#token )); then
5 years ago
[[ $v == '(' ]] && skip='\)'
5 years ago
continue
fi
fi
if [[ $token == *'$'* ]]; then
5 years ago
if [[ $token == $~id ]]; then
n=${${token##[^[:IDENT:]]}%%[^[:IDENT:]]}
[[ $token == *'"' ]] && v=("${(@P)n}") || v=(${(P)name})
5 years ago
tokens[1,0]=(${(qq)v})
continue
fi
fi
commands+=${:-${(Q)${~token}}}
5 years ago
# '|' '||' ';' '&' '&&' '|&' '&!' '&|' ';;' ';&' ';|' ')' '()'
skip='\||\|\||;|&|&&|\|&|&!|&\||;;|;&|;\||\)|}|\(\)'
5 years ago
done
_p9k_commands+=($commands)
5 years ago
}