1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-12-27 09:01:58 +00:00
powerlevel10k/internal/parse.zsh

284 lines
5.9 KiB
Bash
Raw Normal View History

2020-01-11 17:41:36 +00:00
typeset -gA __p9k_pb_cmd_skip=(
'}' ''
'|' ''
'||' ''
'&' ''
'&&' ''
'|&' ''
'&!' ''
'&|' ''
')' ''
'(' ''
'{' ''
'()' ''
'!' ''
';' ''
'if' ''
'fi' ''
'elif' ''
'else' ''
'then' ''
'while' ''
'until' ''
'do' ''
'done' ''
'esac' ''
'end' ''
'coproc' ''
2020-01-10 20:03:04 +00:00
'nocorrect' ''
2020-01-11 17:41:36 +00:00
'noglob' ''
'time' ''
'[[' '\]\]'
'((' '\)\)'
'case' '\)|esac'
';;' '\)|esac'
';&' '\)|esac'
';|' '\)|esac'
'foreach' '\(*\)'
2020-01-10 20:03:04 +00:00
)
2020-01-11 17:41:36 +00:00
typeset -gA __p9k_pb_precommand=(
'-' ''
'builtin' ''
'command' ''
'exec' '-[^a]#[a]'
'nohup' ''
'setsid' ''
2020-01-11 15:34:28 +00:00
'eatmydata' ''
'catchsegv' ''
2020-01-11 17:41:36 +00: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 15:34:28 +00:00
)
2020-01-11 17:41:36 +00:00
typeset -gA __p9k_pb_redirect=(
'&>' ''
'>' ''
'>&' ''
'<' ''
'<&' ''
'<>' ''
'&>|' ''
'>|' ''
'&>>' ''
'>>' ''
'>>&' ''
2020-01-10 20:03:04 +00:00
'&>>|' ''
2020-01-11 17:41:36 +00:00
'>>|' ''
'<<<' ''
2020-01-10 20:03:04 +00:00
)
2020-01-11 17:41:36 +00:00
typeset -gA __p9k_pb_term=(
'|' ''
'||' ''
2020-01-11 17:41:36 +00:00
';' ''
'&' ''
'&&' ''
'|&' ''
'&!' ''
'&|' ''
';;' ''
';&' ''
';|' ''
2020-01-11 17:41:36 +00:00
'(' ''
')' ''
'{' ''
'}' ''
2020-01-11 10:13:13 +00:00
'()' ''
)
2020-01-11 17:41:36 +00:00
typeset -gA __p9k_pb_term_skip=(
'()' ''
'(' '\)'
2020-01-11 11:55:25 +00:00
';;' '\)|esac'
';&' '\)|esac'
';|' '\)|esac'
)
2020-01-11 10:45:23 +00:00
# False positives:
#
# {} always {}
#
2020-01-11 12:18:26 +00:00
# False negatives:
#
2020-01-11 13:45:33 +00:00
# ---------------
2020-01-11 12:18:26 +00:00
# : $(x)
2020-01-11 13:45:33 +00:00
# ---------------
2020-01-11 12:18:26 +00:00
# : `x`
2020-01-11 13:45:33 +00:00
# ---------------
2020-01-11 12:18:26 +00:00
#
2020-01-11 13:45:33 +00:00
# Broken:
2020-01-11 10:45:23 +00:00
#
2020-01-11 13:45:33 +00:00
# ---------------
2020-01-11 12:18:26 +00:00
# ${x/}
2020-01-11 13:45:33 +00:00
# ---------------
2020-01-11 17:23:48 +00:00
# - -- x
# ---------------
# command -p -p x
# ---------------
2020-01-11 12:18:26 +00:00
# *
2020-01-11 13:45:33 +00:00
# ---------------
2020-01-11 12:18:26 +00:00
# x=$y; $x
2020-01-11 13:45:33 +00:00
# ---------------
# x <<END
# ; END
# END
# ---------------
2020-01-11 10:45:23 +00:00
# Setup:
# setopt interactive_comments
# alias x='#'
# Punchline:
# x; y
2020-01-11 13:45:33 +00:00
# ---------------
2020-01-11 17:41:36 +00:00
#
# More brokenness with non-standard options (ignore_braces, ignore_close_braces, etc.).
function _p9k_parse_buffer() {
2020-01-10 20:03:04 +00:00
local rcquotes
[[ -o rcquotes ]] && rcquotes=(-o rcquotes)
emulate -L zsh -o extended_glob -o no_nomatch $rcquotes
2020-01-11 17:41:36 +00:00
typeset -ga _p9k_buffer_commands=()
2020-01-10 23:10:33 +00:00
2020-01-11 17:07:00 +00:00
local -r id='(<->|[[:alpha:]_][[:IDENT:]]#)'
2020-01-11 10:13:13 +00:00
local -r var="\$$id|\${$id}|\"\$$id\"|\"\${$id}\""
2020-01-11 17:07:00 +00:00
local -i e c=1024
local skip n s r state
local -a aln alp alf v commands match mbegin mend
2020-01-10 23:10:33 +00:00
2020-01-10 20:03:04 +00:00
[[ -o interactive_comments ]] && local tokens=(${(Z+C+)1}) || local tokens=(${(z)1})
() {
while (( $#tokens )); do
if (( $#tokens == aln[-1] )); then
aln[-1]=()
alp[-1]=()
if (( $#tokens == alf[-1] )); then
alf[-1]=()
(( e = 0 ))
else
2020-01-11 17:07:00 +00:00
(( e = $#state ))
fi
2020-01-10 23:10:33 +00:00
else
2020-01-11 17:07:00 +00:00
(( e = $#state ))
2020-01-10 23:10:33 +00:00
fi
while (( c-- > 0 )) || return; do
token=$tokens[1]
tokens[1]=()
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})
2020-01-11 13:45:33 +00:00
done
2020-01-11 17:07:00 +00:00
case $state in
2020-01-11 17:23:48 +00:00
t|p*)
2020-01-11 17:41:36 +00:00
if (( $+__p9k_pb_term[$token] )); then
skip=$__p9k_pb_term_skip[$token]
2020-01-11 17:07:00 +00:00
state=${skip:+s}
2020-01-11 17:41:36 +00:00
[[ $token == '()' ]] || _p9k_buffer_commands+=($commands)
commands=()
2020-01-11 17:23:48 +00:00
continue
elif [[ $state == t ]]; then
continue
fi;;
2020-01-11 17:07:00 +00:00
s)
if [[ $token == $~skip ]]; then
state=
fi
continue;;
*r)
state[1]=
continue;;
h)
skip=${(b)token}
state=s
continue;;
esac
2020-01-11 17:07:00 +00:00
if [[ $token == '<<'(|-) ]]; then
state=h
continue
2020-01-10 20:03:04 +00:00
fi
2020-01-11 17:41:36 +00:00
if (( $+__p9k_pb_redirect[${token#<0-255>}] )); then
2020-01-11 17:07:00 +00:00
state+=r
continue
2020-01-10 20:03:04 +00:00
fi
if [[ $token == *'$'* ]]; then
2020-01-11 17:07:00 +00:00
if [[ $token == $~var ]]; then
n=${${token##[^[:IDENT:]]}%%[^[:IDENT:]]}
2020-01-11 17:07:00 +00:00
[[ $token == *'"' ]] && v=("${(@P)n}") || v=(${(P)n})
tokens[1,0]=(${(qq)v})
continue
fi
2020-01-10 20:03:04 +00:00
fi
2020-01-11 17:07:00 +00:00
case $state in
'')
2020-01-11 17:41:36 +00:00
if (( $+__p9k_pb_cmd_skip[$token] )); then
skip=$__p9k_pb_cmd_skip[$token]
2020-01-11 17:07:00 +00: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
: ${token::=${(Q)${~token}}};;
p)
: ${token::=${(Q)${~token}}}
case $token in
[^-]*) ;;
--) state=p1; continue;;
$~skip) state=p2; continue;;
*) continue;;
esac;;
p1) ;;
p2)
state=p
continue;;
esac
commands+=$token
2020-01-11 17:41:36 +00:00
if (( $+__p9k_pb_precommand[$commands[-1]] )); then
2020-01-11 17:07:00 +00:00
state=p
2020-01-11 17:41:36 +00:00
skip=$__p9k_pb_precommand[$commands[-1]]
2020-01-11 17:07:00 +00:00
else
state=t
fi
done
}
2020-01-11 17:41:36 +00:00
_p9k_buffer_commands+=($commands)
_p9k_buffer_commands=(${(u)_p9k_buffer_commands:#('(('*'))'|'`'*'`'|'$'*)})
2020-01-10 20:03:04 +00:00
}