powerlevel10k/internal/parse.zsh

323 lines
6.7 KiB
Bash
Raw Normal View History

2020-01-12 08:42:38 +01:00
typeset -gA __pb_cmd_skip=(
2020-01-11 18:41:36 +01:00
'}' ''
2020-01-12 11:26:48 +01:00
'{' ''
2020-01-11 18:41:36 +01:00
'|' ''
'||' ''
'&' ''
'&&' ''
'|&' ''
'&!' ''
'&|' ''
')' ''
'(' ''
'()' ''
'!' ''
';' ''
'if' ''
'fi' ''
'elif' ''
'else' ''
'then' ''
'while' ''
'until' ''
'do' ''
'done' ''
'esac' ''
'end' ''
'coproc' ''
2020-01-10 21:03:04 +01:00
'nocorrect' ''
2020-01-11 18:41:36 +01:00
'noglob' ''
'time' ''
'[[' '\]\]'
'((' '\)\)'
'case' '\)|esac'
';;' '\)|esac'
';&' '\)|esac'
';|' '\)|esac'
'foreach' '\(*\)'
2020-01-10 21:03:04 +01:00
)
2020-01-12 08:42:38 +01:00
typeset -gA __pb_precommand=(
2020-01-11 18:41:36 +01:00
'-' ''
'builtin' ''
'command' ''
'exec' '-[^a]#[a]'
'nohup' ''
'setsid' ''
2020-01-11 16:34:28 +01:00
'eatmydata' ''
'catchsegv' ''
2020-01-11 18:41:36 +01:00
'pkexec' '--user'
'doas' '-[^aCu]#[acU]'
'nice' '-[^n]#[n]|--adjustment'
'stdbuf' '-[^ioe]#[ioe]|--(input|output|error)'
'sudo' '-[^aghpuUCcrtT]#[aghpuUCcrtT]|--(close-from|group|host|prompt|role|type|other-user|command-timeout|user)'
2020-01-11 16:34:28 +01:00
)
2020-01-12 08:42:38 +01:00
typeset -gA __pb_redirect=(
2020-01-11 18:41:36 +01:00
'&>' ''
'>' ''
'>&' ''
'<' ''
'<&' ''
'<>' ''
'&>|' ''
'>|' ''
'&>>' ''
'>>' ''
'>>&' ''
2020-01-10 21:03:04 +01:00
'&>>|' ''
2020-01-11 18:41:36 +01:00
'>>|' ''
'<<<' ''
2020-01-10 21:03:04 +01:00
)
2020-01-12 08:42:38 +01:00
typeset -gA __pb_term=(
2020-01-11 18:41:36 +01:00
'|' ''
'||' ''
2020-01-11 18:41:36 +01:00
';' ''
'&' ''
'&&' ''
'|&' ''
'&!' ''
'&|' ''
';;' ''
';&' ''
';|' ''
2020-01-11 18:41:36 +01:00
'(' ''
')' ''
2020-01-11 11:13:13 +01:00
'()' ''
2020-01-12 11:26:48 +01:00
'}' ''
)
2020-01-12 08:42:38 +01:00
typeset -gA __pb_term_skip=(
2020-01-11 18:41:36 +01:00
'(' '\)'
2020-01-11 12:55:25 +01:00
';;' '\)|esac'
';&' '\)|esac'
';|' '\)|esac'
)
2020-01-12 11:26:48 +01:00
# Broken:
2020-01-11 13:18:26 +01:00
#
2020-01-11 14:45:33 +01:00
# ---------------
2020-01-11 13:18:26 +01:00
# : $(x)
2020-01-11 14:45:33 +01:00
# ---------------
2020-01-11 13:18:26 +01:00
# : `x`
2020-01-11 14:45:33 +01:00
# ---------------
2020-01-11 13:18:26 +01:00
# ${x/}
2020-01-11 14:45:33 +01:00
# ---------------
2020-01-11 18:23:48 +01:00
# - -- x
# ---------------
# command -p -p x
# ---------------
2020-01-11 13:18:26 +01:00
# *
2020-01-11 14:45:33 +01:00
# ---------------
2020-01-11 13:18:26 +01:00
# x=$y; $x
2020-01-11 14:45:33 +01:00
# ---------------
2020-01-12 11:26:48 +01:00
# alias x=y; y
# ---------------
2020-01-11 14:45:33 +01:00
# x <<END
# ; END
# END
# ---------------
2020-01-11 11:45:23 +01:00
# Setup:
# setopt interactive_comments
# alias x='#'
# Punchline:
# x; y
2020-01-11 14:45:33 +01:00
# ---------------
2020-01-11 18:41:36 +01:00
#
# More brokenness with non-standard options (ignore_braces, ignore_close_braces, etc.).
2020-01-12 08:42:38 +01:00
function _parse_buffer() {
2020-01-12 11:26:48 +01:00
[[ ${2:-0} == <-> ]] || return
2020-01-10 21:03:04 +01:00
local rcquotes
[[ -o rcquotes ]] && rcquotes=(-o rcquotes)
emulate -L zsh -o extended_glob -o no_nomatch $rcquotes
2020-01-12 08:42:38 +01:00
typeset -ga _buffer_commands=()
2020-01-11 18:42:05 +01:00
2020-01-11 18:07:00 +01:00
local -r id='(<->|[[:alpha:]_][[:IDENT:]]#)'
2020-01-11 11:13:13 +01:00
local -r var="\$$id|\${$id}|\"\$$id\"|\"\${$id}\""
2020-01-12 11:26:48 +01:00
local -i e ic c=${2:-'1 << 62'}
2020-01-11 18:07:00 +01:00
local skip n s r state
2020-01-12 09:43:42 +01:00
local -a aln alp alf v commands
2020-01-11 00:10:33 +01:00
2020-01-12 09:43:42 +01:00
if [[ -o interactive_comments ]]; then
ic=1
local tokens=(${(Z+C+)1})
else
local tokens=(${(z)1})
fi
2020-01-10 21:03:04 +01:00
2020-01-12 11:26:48 +01:00
{
while (( $#tokens )); do
2020-01-12 11:26:48 +01:00
(( e = $#state ))
if (( $#alp && $#tokens == alp[-1] )); then
aln[-1]=()
alp[-1]=()
if (( $#tokens == alf[-1] )); then
alf[-1]=()
(( e = 0 ))
fi
2020-01-11 00:10:33 +01:00
fi
while (( c-- > 0 )) || return; do
token=$tokens[1]
tokens[1]=()
if (( $+galiases[$token] )); then
(( $aln[(eI)p$token] )) && break
s=$galiases[$token]
2020-01-12 09:43:42 +01:00
n=p$token
elif (( e )); then
break
elif (( $+aliases[$token] )); then
(( $aln[(eI)p$token] )) && break
s=$aliases[$token]
2020-01-12 09:43:42 +01:00
n=p$token
elif [[ $token == ?*.?* ]] && (( $+saliases[${token##*.}] )); then
r=${token##*.}
(( $aln[(eI)s$r] )) && break
s=${saliases[$r]%% #}
n=s$r
else
break
fi
aln+=$n
alp+=$#tokens
[[ $s == *' ' ]] && alf+=$#tokens
2020-01-12 09:43:42 +01:00
(( ic )) && tokens[1,0]=(${(Z+C+)s}) || tokens[1,0]=(${(z)s})
2020-01-11 14:45:33 +01:00
done
2020-01-12 09:59:48 +01:00
if [[ $token == '<<'(|-) ]]; then
state=h
continue
fi
2020-01-11 18:07:00 +01:00
case $state in
2020-01-12 11:26:48 +01:00
a)
if [[ $token == $skip ]]; then
if [[ $token == '{' ]]; then
_buffer_commands+=($commands)
commands=()
state=
else
skip='{'
fi
continue
else
state=t
fi
;& # fall through
2020-01-11 18:23:48 +01:00
t|p*)
2020-01-12 08:42:38 +01:00
if (( $+__pb_term[$token] )); then
2020-01-12 11:26:48 +01:00
if [[ $token == '()' ]]; then
state=
else
_buffer_commands+=($commands)
if [[ $token == '}' ]]; then
state=a
skip=always
else
skip=$__pb_term_skip[$token]
state=${skip:+s}
fi
fi
commands=()
2020-01-11 18:23:48 +01:00
continue
elif [[ $state == t ]]; then
continue
2020-01-12 11:26:48 +01:00
fi
;;
2020-01-11 18:07:00 +01:00
s)
if [[ $token == $~skip ]]; then
state=
fi
2020-01-12 11:26:48 +01:00
continue
;;
2020-01-11 18:07:00 +01:00
*r)
state[1]=
2020-01-12 11:26:48 +01:00
continue
;;
2020-01-11 18:07:00 +01:00
h)
2020-01-12 09:59:48 +01:00
while (( $#tokens )); do
(( e = ${tokens[(i)$token]} ))
if [[ $tokens[e-1] == ';' && $tokens[e+1] == ';' ]]; then
tokens[1,e]=()
break
else
tokens[1,e]=()
fi
done
while (( $#alp && alp[-1] >= $#tokens )); do
aln[-1]=()
alp[-1]=()
done
state=t
2020-01-12 11:26:48 +01:00
continue
;;
2020-01-11 18:07:00 +01:00
esac
2020-01-12 08:42:38 +01:00
if (( $+__pb_redirect[${token#<0-255>}] )); then
2020-01-11 18:07:00 +01:00
state+=r
continue
2020-01-10 21:03:04 +01:00
fi
if [[ $token == *'$'* ]]; then
2020-01-11 18:07:00 +01:00
if [[ $token == $~var ]]; then
n=${${token##[^[:IDENT:]]}%%[^[:IDENT:]]}
2020-01-11 19:26:04 +01:00
[[ $token == *'"' ]] && v=("${(P)n}") || v=(${(P)n})
tokens[1,0]=(${(qq)v})
continue
fi
2020-01-10 21:03:04 +01:00
fi
2020-01-11 18:07:00 +01:00
case $state in
'')
2020-01-12 08:42:38 +01:00
if (( $+__pb_cmd_skip[$token] )); then
skip=$__pb_cmd_skip[$token]
2020-01-11 18:07:00 +01:00
state=${skip:+s}
continue
fi
if [[ $token == *=* ]]; then
v=${(S)token/#(<->|([[:alpha:]_][[:IDENT:]]#(|'['*[^\\](\\\\)#']')))(|'+')=}
if (( $#v < $#token )); then
if [[ $v == '(' ]]; then
state=s
skip='\)'
fi
continue
fi
fi
2020-01-12 11:26:48 +01:00
: ${token::=${(Q)${~token}}}
;;
2020-01-11 18:07:00 +01:00
p)
: ${token::=${(Q)${~token}}}
case $token in
[^-]*) ;;
--) state=p1; continue;;
$~skip) state=p2; continue;;
*) continue;;
2020-01-12 11:26:48 +01:00
esac
;;
2020-01-11 18:07:00 +01:00
p2)
state=p
2020-01-12 11:26:48 +01:00
continue
;;
2020-01-11 18:07:00 +01:00
esac
commands+=$token
2020-01-12 08:42:38 +01:00
if (( $+__pb_precommand[$commands[-1]] )); then
2020-01-11 18:07:00 +01:00
state=p
2020-01-12 08:42:38 +01:00
skip=$__pb_precommand[$commands[-1]]
2020-01-11 18:07:00 +01:00
else
state=t
fi
done
2020-01-12 11:26:48 +01:00
} always {
_buffer_commands+=($commands)
_buffer_commands=(${(u)_buffer_commands:#('(('*'))'|'`'*'`'|'$'*)})
}
2020-01-10 21:03:04 +01:00
}