forked from z-shell/zbrowse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzbrowse.plugin.zsh
199 lines (160 loc) · 6.4 KB
/
zbrowse.plugin.zsh
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
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
#
# No plugin manager is needed to use this file. All that is needed is adding:
# source {where-zbrowse-is}/zbrowse.plugin.zsh
#
# to ~/.zshrc.
#
0="${ZERO:-${${0:#$ZSH_ARGZERO}:-${(%):-%N}}}"
0="${${(M)0:#/*}:-$PWD/$0}"
ZBROWSE_REPO_DIR="${0:h}"
[[ ! -d "${XDG_CONFIG_HOME:-$HOME/.config}/zbrowse" ]] && command mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/zbrowse"
#
# Update FPATH if:
# 1. Not loading with ZI or other standard-compliant p-m
# (see: https://z.digitalclouds.dev/community/zsh_plugin_standard#activity-indicator).
# 2. Not having fpath already updated (that is being done by other p-ms).
#
if [[ ${zsh_loaded_plugins[-1]} != */zbrowse && -z ${fpath[(r)${0:h}(|/)]} ]] {
fpath+=( "${0:h}" )
}
autoload zbrowse
zle -N zbrowse
bindkey '^B' zbrowse
zmodload zsh/parameter || { echo "No zsh/parameter module, z-shell/zbrowse not registering itself"; return 0 }
# Holds 1) all defined parameters, 2) ZBROWSE_IPARAMS'
# values, both at preexec. Keys are "types", "values"
typeset -gAH ZBROWSE_BEFORE
# Holds interactively defined parameters
typeset -gaUH ZBROWSE_IPARAMS
# Holds parameters that changed recently, order matters
typeset -gaUH ZBROWSE_CHANGED_IPARAMS
# Holds types of parameters after change
typeset -gH ZBROWSE_IPARAMS_POST
# Holds black-listed parameters
typeset -gaH ZBROWSE_BLACK_LIST
ZBROWSE_BLACK_LIST=(
'?' '*' '$' '#' '!' '@'
ZUI_PB_WORDS
ZUI_PB_LEFT
ZUI_PB_WORDS_BEGINNINGS
ZUI_PB_RIGHT
ZUI_PB_SPACES
ZUI_PB_SELECTED_WORD
ZUILIST_NONSELECTABLE_ELEMENTS
ZUILIST_HOP_INDICES
ZUILIST_ACTIVE_SEGMENTS
zcurses_attrs
zcurses_keycodes
zcurses_windows
zcurses_colors
ZCURSES_COLORS
ZCURSES_COLOR_PAIRS
_ZSH_HIGHLIGHT_PRIOR_CURSOR
_ZSH_HIGHLIGHT_PRIOR_BUFFER
ZCALCPROMPT
)
# Ran before command
__zbrowse_preexec() {
ZBROWSE_BEFORE[types]="${(j: :)${(qkv)parameters[@]}}"
ZBROWSE_BEFORE[values]=""
local -a __elems
local __param __param2 __value_str __text __last
for __param in "${ZBROWSE_CHANGED_IPARAMS[@]}"; do
if [[ "${(Pt)__param}" = *association* ]]; then
__elems=( "${(Pkv@)__param}" )
__last="${__elems[-1]}"
__elems=( "${(@)__elems[1,50]}" )
__text="${__elems[*]}"
elif [[ "${(Pt)__param}" = *array* ]]; then
__param2="${__param}[-1]"
__last="${(P)__param2}"
__param2="${__param}[1,50]"
__elems=( "${(@P)__param2}" )
__text="${__elems[*]}"
else
__text="${(P)__param}"
__last="${__text[-10,-1]}"
__text="${__text[1,300]}"
fi
__value_str="$__text$__last"
# Matches hash dump-format - keys and values alternating
ZBROWSE_BEFORE[values]+="${(q)__param} ${(q)__value_str} "
done
return 0
}
# Ran after command
__zbrowse_precmd() {
builtin setopt localoptions extendedglob
local ZBROWSE_TYPES_AFTER="${(j: :)${(qkv)parameters[@]}}"
# Paranoid defence against the parameters being only spaces
[[ "${ZBROWSE_BEFORE[types]}" != *[$'! \t']* || "$ZBROWSE_TYPES_AFTER" != *[$'! \t']* ]] && return 0
# De-concatenated parameters
local -A params_before params_after
params_before=( "${(z)ZBROWSE_BEFORE[types]}" )
params_after=( "${(z)ZBROWSE_TYPES_AFTER}" )
ZBROWSE_BEFORE[types]=""
# Iterate through all existing keys, before or after diff,
# i.e. after all variables that were somehow live across
# the diffing process
local __key
typeset -aU __keys
__keys=( "${(k)params_after[@]}" "${(k)params_before[@]}" );
for __key in "${__keys[@]}"; do
__key="${(Q)__key}"
# Checks if given parameter is from black-list
[[ -n "${ZBROWSE_BLACK_LIST[(er)$__key]}" || "${(Pt)__key}" = *special* ]] && continue
if [[ -z "${params_before[$__key]}" ]]; then
ZBROWSE_IPARAMS+=( "$__key" )
ZBROWSE_CHANGED_IPARAMS[1,0]=( "$__key" )
elif [[ "${params_after[$__key]}" != "${params_before[$__key]}" ]]; then
ZBROWSE_CHANGED_IPARAMS[1,0]=( "$__key" )
fi
done
local -A __before_values
if [[ -n "${ZBROWSE_BEFORE[values]}" ]]; then
__before_values=( "${(z)ZBROWSE_BEFORE[values]}" )
__before_values=( "${(Qkv)__before_values[@]}" )
fi
local __size_limit
zstyle -s ':plugin:zbrowse' var-size-limit __size_limit || __size_limit="2048"
local data_dir="${XDG_CONFIG_HOME:-$HOME/.config}/zbrowse"
local -a __elems __all_elems
local __param __param2 __value_str __text __all_text __last
for __param in "${ZBROWSE_CHANGED_IPARAMS[@]}"; do
if [[ "${(Pt)__param}" = *association* ]]; then
__all_elems=( "${(Pkv@)__param}" )
__all_text="${(j::)__all_elems}"
__last="${__all_elems[-1]}"
__elems=( "${(@)__all_elems[1,50]}" )
__text="${__elems[*]}"
__all_elems=( "${(qqqq@)__all_elems}" )
[[ -z "$__text" ]] && continue
[[ "${#__all_text}" -gt "$__size_limit" ]] && continue
[[ "$__text$__last" != "${__before_values[$__param]}" ]] && print -r -- "association ${(q)__param} ${__all_elems[*]}" >>! "$data_dir"/param.log
elif [[ "${(Pt)__param}" = *array* ]]; then
__all_text="${(Pj::)__param}"
__param2="${__param}[-1]"
__last="${(P)__param2}"
__param2="${__param}[1,50]"
__text="${(Pj: :)__param2}"
[[ -z "$__text" ]] && continue
[[ "${#__all_text}" -gt "$__size_limit" ]] && continue
__all_elems=( "${(P@)__param}" )
__all_elems=( "${(qqqq@)__all_elems}" )
[[ "$__text$__last" != "${__before_values[$__param]}" ]] && print -r -- "array ${(q)__param} ${__all_elems[*]}" >>! "$data_dir"/param.log
else
__all_text="${(P)__param}"
[[ "${#__all_text}" -gt "$__size_limit" ]] && continue
__last="${__all_text[-10,-1]}"
__text="${__all_text[1,300]}"
__all_text="${(qqqq)__all_text}"
[[ -z "$__text" ]] && continue
[[ "$__text$__last" != "${__before_values[$__param]}" ]] && print -r -- "scalar ${(q)__param} ${__all_text}" >>! "$data_dir"/param.log
fi
done
}
autoload -Uz add-zsh-hook
add-zsh-hook preexec __zbrowse_preexec
add-zsh-hook precmd __zbrowse_precmd