diff options
-rw-r--r-- | internal/p10k.zsh | 550 | ||||
-rw-r--r-- | internal/parse.zsh | 322 | ||||
-rw-r--r-- | notes.txt | 23 |
3 files changed, 791 insertions, 104 deletions
diff --git a/internal/p10k.zsh b/internal/p10k.zsh index d6a9a6b4..33c6547c 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -3905,7 +3905,7 @@ _p9k_set_instant_prompt() { RPROMPT=$saved_rprompt } -typeset -gri __p9k_instant_prompt_version=14 +typeset -gri __p9k_instant_prompt_version=15 _p9k_dump_instant_prompt() { local user=${(%):-%n} @@ -4011,12 +4011,46 @@ _p9k_dump_instant_prompt() { if (( _POWERLEVEL9K_SHOW_RULER )); then >&$fd print -r -- '[[ $P9K_TTY == old ]] && { unset _p9k__ruler_i; _p9k__display_v[4]=print }' fi + if (( $+functions[p10k-on-init] )); then + >&$fd print -r -- ' + p10k-on-init() { '$functions[p10k-on-init]' }' + fi + if (( $+functions[p10k-on-pre-prompt] )); then + >&$fd print -r -- ' + p10k-on-pre-prompt() { '$functions[p10k-on-pre-prompt]' }' + fi + if (( $+functions[p10k-on-post-prompt] )); then + >&$fd print -r -- ' + p10k-on-post-prompt() { '$functions[p10k-on-post-prompt]' }' + fi + if (( $+functions[p10k-on-post-widget] )); then + >&$fd print -r -- ' + p10k-on-post-widget() { '$functions[p10k-on-post-widget]' }' + fi + if (( $+functions[p10k-on-init] )); then + >&$fd print -r -- ' + p10k-on-init' + fi + if (( $+functions[p10k-on-pre-prompt] )); then + >&$fd print -r -- ' + p10k-on-pre-prompt' + fi + if (( $+functions[p10k-on-init] )); then + >&$fd print -r -- ' + unfunction p10k-on-init' + fi if (( $+functions[p10k-on-pre-prompt] )); then >&$fd print -r -- ' - p10k-on-pre-prompt() { '$functions[p10k-on-pre-prompt]' } - p10k-on-pre-prompt unfunction p10k-on-pre-prompt' fi + if (( $+functions[p10k-on-post-prompt] )); then + >&$fd print -r -- ' + unfunction p10k-on-post-prompt' + fi + if (( $+functions[p10k-on-post-widget] )); then + >&$fd print -r -- ' + unfunction p10k-on-post-widget' + fi >&$fd print -r -- ' trap "unset -m _p9k__\*; unfunction p10k" EXIT local -a _p9k_t=("${(@ps:$us:)${tail%%$rs*}}")' @@ -4434,7 +4468,6 @@ function _p9k_on_expand() { fi fi - _p9k__last_tty=$P9K_TTY __p9k_reset_state=1 if (( _POWERLEVEL9K_PROMPT_ADD_NEWLINE )); then @@ -4463,13 +4496,15 @@ function _p9k_on_expand() { fi fi - typeset -g P9K_PROMPT=regular - - if (( $+functions[p10k-on-pre-prompt] )); then - p10k-on-pre-prompt + if [[ -z $_p9k__last_tty ]]; then + _p9k_wrap_widgets + (( $+functions[p10k-on-init] )) && p10k-on-init fi + (( $+functions[p10k-on-pre-prompt] )) && p10k-on-pre-prompt + __p9k_reset_state=0 + _p9k__last_tty=$P9K_TTY P9K_TTY=old if ! zle; then @@ -4582,7 +4617,7 @@ _p9k_trapint() { if (( __p9k_enabled )); then emulate -L zsh setopt no_hist_expand extended_glob no_prompt_bang prompt_{percent,subst} - zle && _p9k_zle_line_finish int + zle && _p9k_on_widget_zle-line-finish int fi return 0 } @@ -4614,14 +4649,6 @@ function _p9k_reset_prompt() { fi } -function _p9k_zle_keymap_select() { - _p9k_reset_prompt -} - -function _p9k_zle_state_changed() { - _p9k_reset_prompt -} - _p9k_deinit_async_pump() { if (( _p9k__async_pump_lock_fd )); then zsystem flock -u $_p9k__async_pump_lock_fd @@ -4805,6 +4832,8 @@ typeset -g _p9k__param_pat typeset -g _p9k__param_sig _p9k_init_vars() { + typeset -g _p9k__last_buffer + typeset -ga _p9k__last_commands typeset -g _p9k__last_tty typeset -gi _p9k__must_restore_prompt typeset -gi _p9k__restore_prompt_fd @@ -4928,6 +4957,7 @@ _p9k_init_vars() { typeset -g P9K_VISUAL_IDENTIFIER typeset -g P9K_CONTENT typeset -g P9K_GAP + typeset -g P9K_PROMPT=regular } _p9k_init_params() { @@ -4948,6 +4978,7 @@ _p9k_init_params() { _p9k_declare -s POWERLEVEL9K_TRANSIENT_PROMPT off [[ $_POWERLEVEL9K_TRANSIENT_PROMPT == (off|always|same-dir) ]] || _POWERLEVEL9K_TRANSIENT_PROMPT=off + _p9k_declare -a POWERLEVEL9K_HOOK_WIDGETS -- _p9k_declare -b POWERLEVEL9K_TODO_HIDE_ZERO_TOTAL 0 _p9k_declare -b POWERLEVEL9K_TODO_HIDE_ZERO_FILTERED 0 _p9k_declare -b POWERLEVEL9K_DISABLE_HOT_RELOAD 0 @@ -5236,92 +5267,327 @@ _p9k_init_params() { done } -typeset -ga __p9k_wrapped_zle_widgets +typeset -grA __p9k_pb_cmd_skip=( + '}' '' + '|' '' + '||' '' + '&' '' + '&&' '' + '|&' '' + '&!' '' + '&|' '' + ')' '' + '(' '' + '{' '' + '()' '' + '!' '' + ';' '' + 'if' '' + 'fi' '' + 'elif' '' + 'else' '' + 'then' '' + 'while' '' + 'until' '' + 'do' '' + 'done' '' + 'esac' '' + 'end' '' + 'coproc' '' + 'nocorrect' '' + 'noglob' '' + 'time' '' + '[[' '\]\]' + '((' '\)\)' + 'case' '\)|esac' + ';;' '\)|esac' + ';&' '\)|esac' + ';|' '\)|esac' + 'foreach' '\(*\)' +) -# _p9k_wrap_zle_widget zle-keymap-select _p9k_zle_keymap_select -_p9k_wrap_zle_widget() { - local widget=$1 - local hook=$2 - (( __p9k_wrapped_zle_widgets[(I)$widget:$hook] )) && return - __p9k_wrapped_zle_widgets+=$widget:$hook - local orig=p9k-orig-$widget - case $widgets[$widget] in - user:*) - zle -N $orig ${widgets[$widget]#user:} - ;; - builtin) - functions[_p9k_orig_$widget]="zle .${(q)widget}" - zle -N $orig _p9k_orig_$widget - ;; - esac +typeset -grA __p9k_pb_precommand=( + '-' '' + 'builtin' '' + 'command' '' + 'exec' '-[^a]#[a]' + 'nohup' '' + 'setsid' '' + 'eatmydata' '' + 'catchsegv' '' + '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)' +) - local wrapper=_p9k_wrapper_$widget_$hook - functions[$wrapper]=" - emulate -L zsh - setopt no_hist_expand extended_glob no_prompt_bang prompt_{percent,subst} - (( __p9k_enabled )) && ${(q)hook} \"\$@\" - (( \$+widgets[${(q)orig}] )) && zle ${(q)orig} -- \"\$@\"" +typeset -grA __p9k_pb_redirect=( + '&>' '' + '>' '' + '>&' '' + '<' '' + '<&' '' + '<>' '' + '&>|' '' + '>|' '' + '&>>' '' + '>>' '' + '>>&' '' + '&>>|' '' + '>>|' '' + '<<<' '' +) - zle -N -- $widget $wrapper -} +typeset -grA __p9k_pb_term=( + '|' '' + '||' '' + ';' '' + '&' '' + '&&' '' + '|&' '' + '&!' '' + '&|' '' + ';;' '' + ';&' '' + ';|' '' + '(' '' + ')' '' + '{' '' + '}' '' + '()' '' +) -function _p9k_zle_line_init() { - (( _p9k__cursor_hidden )) || return 0 - _p9k__cursor_hidden=0 - echoti cnorm -} +typeset -grA __p9k_pb_term_skip=( + '()' '' + '(' '\)' + ';;' '\)|esac' + ';&' '\)|esac' + ';|' '\)|esac' +) -function _p9k_restore_prompt() { - emulate -L zsh - setopt no_hist_expand extended_glob no_prompt_bang prompt_{percent,subst} - { - (( _p9k__must_restore_prompt )) || return - _p9k__must_restore_prompt=0 +# False positives: +# +# {} always {} +# +# False negatives: +# +# --------------- +# : $(x) +# --------------- +# : `x` +# --------------- +# +# Broken: +# +# --------------- +# ${x/} +# --------------- +# - -- x +# --------------- +# command -p -p x +# --------------- +# * +# --------------- +# x=$y; $x +# --------------- +# x <<END +# ; END +# END +# --------------- +# Setup: +# setopt interactive_comments +# alias x='#' +# Punchline: +# x; y +# --------------- +# +# More brokenness with non-standard options (ignore_braces, ignore_close_braces, etc.). +function _p9k_parse_buffer() { + local rcquotes + [[ -o rcquotes ]] && rcquotes=(-o rcquotes) - unset _p9k__line_finished - _p9k_refresh_reason=restore - _p9k_set_prompt - _p9k_refresh_reason= + emulate -L zsh -o extended_glob -o no_nomatch $rcquotes - local tty=$P9K_TTY - P9K_TTY=$_p9k__last_tty - _p9k__expanded=0 - _p9k_reset_prompt - P9K_TTY=$tty - } always { - zle -F $1 - exec {1}>&- - _p9k__restore_prompt_fd=0 + local -r id='(<->|[[:alpha:]_][[:IDENT:]]#)' + local -r var="\$$id|\${$id}|\"\$$id\"|\"\${$id}\"" + + local -i e c=32 + local skip n s r state + local -a aln alp alf v commands match mbegin mend + + [[ -o interactive_comments ]] && local tokens=(${(Z+C+)1}) || local tokens=(${(z)1}) + + () { + while (( $#tokens )); do + if (( $#tokens == alp[-1] )); then + aln[-1]=() + alp[-1]=() + if (( $#tokens == alf[-1] )); then + alf[-1]=() + (( e = 0 )) + else + (( e = $#state )) + fi + else + (( e = $#state )) + 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}) + done + + case $state in + t|p*) + if (( $+__p9k_pb_term[$token] )); then + skip=$__p9k_pb_term_skip[$token] + state=${skip:+s} + [[ $token == '()' ]] || P9K_COMMANDS+=($commands) + commands=() + continue + elif [[ $state == t ]]; then + continue + fi;; + s) + if [[ $token == $~skip ]]; then + state= + fi + continue;; + *r) + state[1]= + continue;; + h) + skip=${(b)token} + state=s + continue;; + esac + + if [[ $token == '<<'(|-) ]]; then + state=h + continue + fi + + if (( $+__p9k_pb_redirect[${token#<0-255>}] )); then + state+=r + continue + fi + + if [[ $token == *'$'* ]]; then + if [[ $token == $~var ]]; then + n=${${token##[^[:IDENT:]]}%%[^[:IDENT:]]} + [[ $token == *'"' ]] && v=("${(P)n}") || v=(${(P)n}) + tokens[1,0]=(${(qq)v}) + continue + fi + fi + + case $state in + '') + if (( $+__p9k_pb_cmd_skip[$token] )); then + skip=$__p9k_pb_cmd_skip[$token] + 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 + if (( $+__p9k_pb_precommand[$commands[-1]] )); then + state=p + skip=$__p9k_pb_precommand[$commands[-1]] + else + state=t + fi + done } + + P9K_COMMANDS+=($commands) + P9K_COMMANDS=(${(u)P9K_COMMANDS:#('(('*'))'|'`'*'`'|'$'*)}) } -function _p9k_zle_line_finish() { - [[ -z $1 && $CONTEXT != start ]] && _p9k__must_restore_prompt=0 +function _p9k_on_widget_zle-keymap-select() { __p9k_reset_state=2; } +function _p9k_on_widget_overwrite-mode() { __p9k_reset_state=2; } +function _p9k_on_widget_vi-replace() { __p9k_reset_state=2; } + +function _p9k_check_visual_mode() { + [[ ${KEYMAP:-} == vicmd ]] || return 0 + local region=${${REGION_ACTIVE:-0}/2/1} + [[ $region != $_p9k__region_active ]] || return 0 + _p9k__region_active=$region + __p9k_reset_state=2 +} +function _p9k_on_widget_visual-mode() { _p9k_check_visual_mode; } +function _p9k_on_widget_visual-line-mode() { _p9k_check_visual_mode; } +function _p9k_on_widget_deactivate-region() { _p9k_check_visual_mode; } + +function _p9k_on_widget_zle-line-init() { + (( _p9k__cursor_hidden )) || return 0 + _p9k__cursor_hidden=0 + echoti cnorm +} + +function _p9k_on_widget_zle-line-finish() { (( $+_p9k__line_finished )) && return _p9k__line_finished= - local -i reset=_p9k_reset_on_line_finish - - if (( $+functions[p10k-on-post-prompt] )); then - __p9k_reset_state=1 - p10k-on-post-prompt - if (( __p9k_reset_state == 2 )); then - reset=1 - fi - __p9k_reset_state=0 - fi + (( _p9k_reset_on_line_finish )) && __p9k_reset_state=2 + (( $+functions[p10k-on-post-prompt] )) && p10k-on-post-prompt if [[ -n $_p9k_transient_prompt ]]; then if [[ $_POWERLEVEL9K_TRANSIENT_PROMPT == always || $_p9k_pwd == $_p9k__last_prompt_pwd ]]; then RPROMPT= PROMPT=$_p9k_transient_prompt - reset=1 + __p9k_reset_state=2 else _p9k__last_prompt_pwd=$_p9k_pwd fi fi - if (( reset )); then + if (( __p9k_reset_state == 2 )); then if [[ $1 == int ]]; then _p9k__must_restore_prompt=1 if (( !_p9k__restore_prompt_fd )); then @@ -5334,17 +5600,109 @@ function _p9k_zle_line_finish() { echo -nE - $hide$'\n'$termcap[up] fi _p9k_reset_prompt + __p9k_reset_state=0 fi _p9k__line_finished='%{%}' } -function _p9k_zle_line_pre_redraw() { - [[ ${KEYMAP:-} == vicmd ]] || return 0 - local region=${${REGION_ACTIVE:-0}/2/1} - [[ $region != $_p9k__region_active ]] || return 0 - _p9k__region_active=$region - _p9k_reset_prompt +function _p9k_widget_hook() { + emulate -L zsh + setopt no_hist_expand extended_glob no_prompt_bang prompt_{percent,subst} + (( _p9k__restore_prompt_fd )) && _p9k_restore_prompt $_p9k__restore_prompt_fd + __p9k_reset_state=1 + local -a P9K_COMMANDS + if [[ $_p9k__last_buffer == $PREBUFFER$BUFFER ]]; then + P9K_COMMANDS=($_p9k__last_commands) + else + _p9k__last_buffer=$PREBUFFER$BUFFER + _p9k_parse_buffer $_p9k__last_buffer # 2>/dev/null + _p9k__last_commands=($P9K_COMMANDS) + fi + (( $+functions[p10k-on-post-widget] )) && p10k-on-post-widget "${@:2}" + (( $+functions[_p9k_on_widget_$1] )) && _p9k_on_widget_$1 + (( __p9k_reset_state == 2 )) && _p9k_reset_prompt + __p9k_reset_state=0 +} + +function _p9k_widget() { + (( ! ${+widgets[._p9k_orig_$1]} )) || zle ._p9k_orig_$1 "${@:2}" + local res=$? + (( ! __p9k_enabled )) || [[ $CONTEXT != start ]] || _p9k_widget_hook "$@" + return res +} + +typeset -gi __p9k_widgets_wrapped=0 + +function _p9k_wrap_widgets() { + (( __p9k_widgets_wrapped )) && return + typeset -gir __p9k_widgets_wrapped=1 + local -a widget_list + if is-at-least 5.3; then + local -aU widget_list=( + zle-line-pre-redraw + zle-line-init + zle-line-finish + zle-keymap-select + overwrite-mode + vi-replace + visual-mode + visual-line-mode + deactivate-region + $_POWERLEVEL9K_HOOK_WIDGETS + ) + else + # There is no zle-line-pre-redraw in zsh < 5.3, so we have to wrap all widgets + # with key bindings. This costs extra 3ms: 1.5ms to fetch the list of widgets and + # another 1.5ms to wrap them. + local keymap tmp=${TMPDIR:-/tmp}/p10k.bindings.$sysparams[pid] + { + for keymap in $keymaps; do bindkey -M $keymap; done >$tmp + local -aU widget_list=( + zle-isearch-exit + zle-isearch-update + zle-line-init + zle-line-finish + zle-history-line-set + zle-keymap-select + $_POWERLEVEL9K_HOOK_WIDGETS + ${${${(f)"$(<$tmp)"}##* }:#(*\"|.*)} + ) + } always { + zf_rm -f $tmp + } + fi + local widget + for widget in $widget_list; do + functions[_p9k_widget_$widget]='_p9k_widget '${(q)widget}' "$@"' + # The leading dot is to work around bugs in zsh-syntax-highlighting. + zle -A $widget ._p9k_orig_$widget + zle -N $widget _p9k_widget_$widget + done 2>/dev/null # `zle -A` fails for inexisting widgets and complains to stderr +} + +function _p9k_restore_prompt() { + emulate -L zsh + setopt no_hist_expand extended_glob no_prompt_bang prompt_{percent,subst} + { + (( _p9k__must_restore_prompt )) || return + _p9k__must_restore_prompt=0 + + unset _p9k__line_finished + _p9k_refresh_reason=restore + _p9k_set_prompt + _p9k_refresh_reason= + + local tty=$P9K_TTY + P9K_TTY=$_p9k__last_tty + _p9k__expanded=0 + _p9k_reset_prompt + P9K_TTY=$tty + } always { + zle -F $1 + exec {1}>&- + _p9k__restore_prompt_fd=0 + } } prompt__p9k_internal_nothing() { _p9k__prompt+='${_p9k_sss::=}'; } @@ -5693,7 +6051,7 @@ _p9k_must_init() { [[ $sig == $_p9k__param_sig ]] && return 1 _p9k_deinit fi - _p9k__param_pat=$'v22\1'${ZSH_VERSION}$'\1'${ZSH_PATCHLEVEL}$'\1' + _p9k__param_pat=$'v23\1'${ZSH_VERSION}$'\1'${ZSH_PATCHLEVEL}$'\1' _p9k__param_pat+=$'${#parameters[(I)POWERLEVEL9K_*]}\1${(%):-%n%#}\1$GITSTATUS_LOG_LEVEL\1' _p9k__param_pat+=$'$GITSTATUS_ENABLE_LOGGING\1$GITSTATUS_DAEMON\1$GITSTATUS_NUM_THREADS\1' _p9k__param_pat+=$'$DEFAULT_USER\1${ZLE_RPROMPT_INDENT:-1}\1$P9K_SSH\1$__p9k_ksh_arrays' @@ -5994,23 +6352,6 @@ _p9k_init() { fi fi - _p9k_wrap_zle_widget zle-line-finish _p9k_zle_line_finish - _p9k_wrap_zle_widget zle-line-init _p9k_zle_line_init - - if _p9k_segment_in_use vi_mode || _p9k_segment_in_use prompt_char; then - _p9k_wrap_zle_widget zle-keymap-select _p9k_zle_keymap_select - fi - - if _p9k_segment_in_use vi_mode && (( $+_POWERLEVEL9K_VI_VISUAL_MODE_STRING )) || _p9k_segment_in_use prompt_char; then - _p9k_wrap_zle_widget zle-line-pre-redraw _p9k_zle_line_pre_redraw - fi - - if { _p9k_segment_in_use vi_mode && (( $+_POWERLEVEL9K_VI_OVERWRITE_MODE_STRING )) } || - { _p9k_segment_in_use prompt_char && (( _POWERLEVEL9K_PROMPT_CHAR_OVERWRITE_STATE )) }; then - _p9k_wrap_zle_widget overwrite-mode _p9k_zle_state_changed - _p9k_wrap_zle_widget vi-replace _p9k_zle_state_changed - fi - if _p9k_segment_in_use dir && [[ $_POWERLEVEL9K_SHORTEN_STRATEGY == truncate_with_package_name && $+commands[jq] == 0 ]]; then print -rP -- '%F{yellow}WARNING!%f %BPOWERLEVEL9K_SHORTEN_STRATEGY=truncate_with_package_name%b requires %F{green}jq%f.' @@ -6389,6 +6730,7 @@ zmodload zsh/parameter 2>/dev/null # https://github.com/romkatv/gitstatus/issue zmodload zsh/system zmodload zsh/termcap zmodload zsh/terminfo +zmodload zsh/zleparameter zmodload -F zsh/stat b:zstat zmodload -F zsh/net/socket b:zsocket zmodload -F zsh/files b:zf_mv b:zf_rm diff --git a/internal/parse.zsh b/internal/parse.zsh new file mode 100644 index 00000000..d093f851 --- /dev/null +++ b/internal/parse.zsh @@ -0,0 +1,322 @@ +typeset -gA __pb_cmd_skip=( + '}' '' + '{' '' + '|' '' + '||' '' + '&' '' + '&&' '' + '|&' '' + '&!' '' + '&|' '' + ')' '' + '(' '' + '()' '' + '!' '' + ';' '' + 'if' '' + 'fi' '' + 'elif' '' + 'else' '' + 'then' '' + 'while' '' + 'until' '' + 'do' '' + 'done' '' + 'esac' '' + 'end' '' + 'coproc' '' + 'nocorrect' '' + 'noglob' '' + 'time' '' + '[[' '\]\]' + '((' '\)\)' + 'case' '\)|esac' + ';;' '\)|esac' + ';&' '\)|esac' + ';|' '\)|esac' + 'foreach' '\(*\)' +) + +typeset -gA __pb_precommand=( + '-' '' + 'builtin' '' + 'command' '' + 'exec' '-[^a]#[a]' + 'nohup' '' + 'setsid' '' + 'eatmydata' '' + 'catchsegv' '' + '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)' +) + +typeset -gA __pb_redirect=( + '&>' '' + '>' '' + '>&' '' + '<' '' + '<&' '' + '<>' '' + '&>|' '' + '>|' '' + '&>>' '' + '>>' '' + '>>&' '' + '&>>|' '' + '>>|' '' + '<<<' '' +) + +typeset -gA __pb_term=( + '|' '' + '||' '' + ';' '' + '&' '' + '&&' '' + '|&' '' + '&!' '' + '&|' '' + ';;' '' + ';&' '' + ';|' '' + '(' '' + ')' '' + '()' '' + '}' '' +) + +typeset -gA __pb_term_skip=( + '(' '\)' + ';;' '\)|esac' + ';&' '\)|esac' + ';|' '\)|esac' +) + +# Broken: +# +# --------------- +# : $(x) +# --------------- +# : `x` +# --------------- +# ${x/} +# --------------- +# - -- x +# --------------- +# command -p -p x +# --------------- +# * +# --------------- +# x=$y; $x +# --------------- +# alias x=y; y +# --------------- +# x <<END +# ; END +# END +# --------------- +# Setup: +# setopt interactive_comments +# alias x='#' +# Punchline: +# x; y +# --------------- +# +# More brokenness with non-standard options (ignore_braces, ignore_close_braces, etc.). +function _parse_buffer() { + [[ ${2:-0} == <-> ]] || return + + local rcquotes + [[ -o rcquotes ]] && rcquotes=(-o rcquotes) + + emulate -L zsh -o extended_glob -o no_nomatch $rcquotes + + typeset -ga _buffer_commands=() + + local -r id='(<->|[[:alpha:]_][[:IDENT:]]#)' + local -r var="\$$id|\${$id}|\"\$$id\"|\"\${$id}\"" + + local -i e ic c=${2:-'1 << 62'} + local skip n s r state + local -a aln alp alf v commands + + if [[ -o interactive_comments ]]; then + ic=1 + local tokens=(${(Z+C+)1}) + else + local tokens=(${(z)1}) + fi + + { + while (( $#tokens )); do + (( e = $#state )) + + if (( $#alp && $#tokens == alp[-1] )); then + aln[-1]=() + alp[-1]=() + if (( $#tokens == alf[-1] )); then + alf[-1]=() + (( e = 0 )) + fi + fi + + while (( c-- > 0 )) || return; do + token=$tokens[1] + tokens[1]=() + if (( $+galiases[$token] )); then + (( $aln[(eI)p$token] )) && break + s=$galiases[$token] + n=p$token + elif (( e )); then + break + elif (( $+aliases[$token] )); then + (( $aln[(eI)p$token] )) && break + s=$aliases[$token] + 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 + (( ic )) && tokens[1,0]=(${(Z+C+)s}) || tokens[1,0]=(${(z)s}) + done + + if [[ $token == '<<'(|-) ]]; then + state=h + continue + fi + + case $state in + a) + if [[ $token == $skip ]]; then + if [[ $token == '{' ]]; then + _buffer_commands+=($commands) + commands=() + state= + else + skip='{' + fi + continue + else + state=t + fi + ;& # fall through + t|p*) + if (( $+__pb_term[$token] )); then + 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=() + continue + elif [[ $state == t ]]; then + continue + fi + ;; + s) + if [[ $token == $~skip ]]; then + state= + fi + continue + ;; + *r) + state[1]= + continue + ;; + h) + 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 + continue + ;; + esac + + if (( $+__pb_redirect[${token#<0-255>}] )); then + state+=r + continue + fi + + if [[ $token == *'$'* ]]; then + if [[ $token == $~var ]]; then + n=${${token##[^[:IDENT:]]}%%[^[:IDENT:]]} + [[ $token == *'"' ]] && v=("${(P)n}") || v=(${(P)n}) + tokens[1,0]=(${(qq)v}) + continue + fi + fi + + case $state in + '') + if (( $+__pb_cmd_skip[$token] )); then + skip=$__pb_cmd_skip[$token] + 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 + ;; + p2) + state=p + continue + ;; + esac + + commands+=$token + if (( $+__pb_precommand[$commands[-1]] )); then + state=p + skip=$__pb_precommand[$commands[-1]] + else + state=t + fi + done + } always { + _buffer_commands+=($commands) + _buffer_commands=(${(u)_buffer_commands:#('(('*'))'|'`'*'`'|'$'*)}) + } +} diff --git a/notes.txt b/notes.txt new file mode 100644 index 00000000..779c7a21 --- /dev/null +++ b/notes.txt @@ -0,0 +1,23 @@ +Hooks: + + p10k-on-init (may be called while zle is active or not) + p10k-on-pre-prompt (may be called while zle is active or not) + p10k-on-post-prompt + p10k-on-post-widget + +Parameters accessible from all hooks: + + P9K_PROMPT=regular|instant + P9K_TTY=old|new + +Parameters accessible from p10k-on-post-widget: + + All regular zle parameters (WIDGET, BUFFER, CURSOR, etc.). + P9K_COMMAND + P9K_LASTCOMMAND + P9K_LASTBUFFER + P9K_LASTCURSOR + +`P9K_LAST*` parameters are set if and only if this isn't the first p10k-on-post-widget call after p10k-on-init. + +# TODO: call all hooks with user options |