-
Notifications
You must be signed in to change notification settings - Fork 126
/
zplugin.zsh
3462 lines (2894 loc) · 107 KB
/
zplugin.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- mode: shell-script -*-
# vim:ft=zsh
#
# Main state variables
#
typeset -gaH ZPLG_REGISTERED_PLUGINS
# Maps plugins to 0, 1 or 2 - not loaded, light loaded, fully loaded
typeset -gAH ZPLG_REGISTERED_STATES
# Snippets loaded, url -> file name
typeset -gAH ZPLG_SNIPPETS
# Reports, per plugin
typeset -gAH ZPLG_REPORTS
#
# Common needed values
#
typeset -gH ZPLG_NAME
# User can override ZPLG_DIR. Misleading? Reset
if [ ! -e "$ZPLG_DIR"/widget-list.zsh ]; then
typeset -gH ZPLG_DIR=""
fi
# Problematic function_argzero
if [[ ! -o "functionargzero" ]]; then
if [[ "$0" = "${argv[0]}" || -z "${argv[0]}" ]]; then
ZPLG_NAME="zplugin" # A try of typical, actually expected name
[ -z "$ZPLG_DIR" ] && ZPLG_DIR="${0:h}"
else
ZPLG_NAME="${${argv[0]:t}:r}"
[ -z "$ZPLG_DIR" ] && ZPLG_DIR="${argv[0]:h}"
fi
else
ZPLG_NAME="${${0:t}:r}"
[ -z "$ZPLG_DIR" ] && ZPLG_DIR="${0:h}"
fi
# Make ZPLG_DIR path absolute
if [[ "$ZPLG_DIR" != /* ]]; then
if [ "$ZPLG_DIR" = "." ]; then
ZPLG_DIR="$PWD"
else
ZPLG_DIR="$PWD/$ZPLG_DIR"
fi
fi
# Final test of ZPLG_DIR
if [ ! -e "$ZPLG_DIR"/widget-list.zsh ]; then
print "Could not establish ZPLG_DIR variable. Set it to where Zplugin's git repository is."
return 1
fi
# User can override ZPLG_HOME
if [ -z "$ZPLG_HOME" ]; then
# Ignore ZDOTDIR if user manually put Zplugin to $HOME
if [ -d "$HOME/.$ZPLG_NAME" ]; then
typeset -gH ZPLG_HOME="$HOME/.$ZPLG_NAME"
else
typeset -gH ZPLG_HOME="${ZDOTDIR:-$HOME}/.$ZPLG_NAME"
fi
fi
typeset -gH ZPLG_PLUGINS_DIR="$ZPLG_HOME/plugins"
typeset -gH ZPLG_COMPLETIONS_DIR="$ZPLG_HOME/completions"
typeset -gH ZPLG_SNIPPETS_DIR="$ZPLG_HOME/snippets"
typeset -gH ZPLG_HOME_READY
typeset -gaHU ZPLG_ENTER_OPTIONS
typeset -gH ZPLG_EXTENDED_GLOB
typeset -gAH ZPLG_BACKUP_FUNCTIONS
typeset -gAH ZPLG_BACKUP_ALIASES
typeset -ga ZPLG_STRESS_TEST_OPTIONS
ZPLG_STRESS_TEST_OPTIONS=( "NO_SHORT_LOOPS" "IGNORE_BRACES" "IGNORE_CLOSE_BRACES" "SH_GLOB" "CSH_JUNKIE_QUOTES" "NO_MULTI_FUNC_DEF" )
#
# All to the users - simulate OMZ directory structure (1/3)
#
typeset -gH ZSH="$ZPLG_PLUGINS_DIR"
typeset -gH ZSH_CUSTOM="$ZPLG_PLUGINS_DIR/custom"
export ZSH ZSH_CUSTOM
#
# Nasty variables {{{
# Can be used by any shadowing function to recognize current context
#
typeset -gH ZPLG_CUR_USER=""
typeset -gH ZPLG_CUR_PLUGIN=""
# Concatenated with "---"
typeset -gH ZPLG_CUR_USPL=""
# Concatenated with "/"
typeset -gH ZPLG_CUR_USPL2=""
# If any plugin retains the shadowed function instead of
# original one then this will protect from further reporting
typeset -gH ZPLG_SHADOWING_ACTIVE="inactive"
# To show "function already defined, in zsh" warning once per function
typeset -gAH ZPLG_ALREADY_WARNINGS_F
# If "1", it will make debug reporting active,
# e.g. shadowing will be permanently on
typeset -gH ZPLG_DEBUG_ACTIVE="0"
# Name of "plugin" to which debug reports should be assigned - uspl2 format
typeset -gH ZPLG_DEBUG_USPL2="_dtrace/_dtrace"
# Name of "plugin" to which debug reports should be assigned - uspl1 format
typeset -gH ZPLG_DEBUG_USPL="_dtrace---_dtrace"
# User part of the debug plugin
typeset -gH ZPLG_DEBUG_USER="_dtrace"
# Plugin part of the debug plugin
typeset -gH ZPLG_DEBUG_PLUGIN="_dtrace"
# }}}
#
# Function diffing {{{
#
# Used to hold declared functions existing before loading a plugin
typeset -gAH ZPLG_FUNCTIONS_BEFORE
# Functions existing after loading a plugin. Reporting will do a diff
typeset -gAH ZPLG_FUNCTIONS_AFTER
# Functions computed to be associated with plugin
typeset -gAH ZPLG_FUNCTIONS
# Was the function diff already ran?
typeset -gAH ZPLG_FUNCTIONS_DIFF_RAN
#}}}
#
# Option diffing {{{
#
# Concatenated state of options before loading a plugin
typeset -gAH ZPLG_OPTIONS_BEFORE
# Concatenated state of options after loading a plugin
typeset -gAH ZPLG_OPTIONS_AFTER
# Concatenated options that changed, hold as they were before plugin load
typeset -gAH ZPLG_OPTIONS
# Was the option diff already ran?
typeset -gAH ZPLG_OPTIONS_DIFF_RAN
# }}}
#
# Environment diffing {{{
#
# Concatenated state of PATH before loading a plugin
typeset -gAH ZPLG_PATH_BEFORE
# Concatenated state of PATH after loading a plugin
typeset -gAH ZPLG_PATH_AFTER
# Concatenated new elements of PATH (after diff)
typeset -gAH ZPLG_PATH
# Concatenated state of FPATH before loading a plugin
typeset -gAH ZPLG_FPATH_BEFORE
# Concatenated state of FPATH after loading a plugin
typeset -gAH ZPLG_FPATH_AFTER
# Concatenated new elements of FPATH (after diff)
typeset -gAH ZPLG_FPATH
# Was the environment diff already ran?
typeset -gHA ZPLG_ENV_DIFF_RAN
# }}}
#
# Parameter diffing {{{
#
# Concatenated state of PARAMETERS before loading a plugin
typeset -gAH ZPLG_PARAMETERS_BEFORE
# Concatenated state of PARAMETERS after loading a plugin
typeset -gAH ZPLG_PARAMETERS_AFTER
# Concatenated *changed* previous elements of $parameters (before)
typeset -gAH ZPLG_PARAMETERS_PRE
# Concatenated *changed* current elements of $parameters (after)
typeset -gAH ZPLG_PARAMETERS_POST
# Was the environment diff already ran?
typeset -gHA ZPLG_PARAMETERS_DIFF_RAN
# }}}
#
# Zstyle, bindkey, alias, zle remembering {{{
#
# Holds concatenated Zstyles declared by each plugin
# Concatenated after quoting, so (z)-splittable
typeset -gAH ZPLG_ZSTYLES
# Holds concatenated bindkeys declared by each plugin
typeset -gAH ZPLG_BINDKEYS
# Holds counter used for main keymap saves
typeset -giH ZPLG_BINDKEY_MAIN_IDX
# Holds concatenated aliases declared by each plugin
typeset -gAH ZPLG_ALIASES
# Holds concatenated pairs "widget_name save_name" for use with zle -A
typeset -gAH ZPLG_WIDGETS_SAVED
# Holds concatenated names of widgets that should be deleted
typeset -gAH ZPLG_WIDGETS_DELETE
# Holds compdef calls (i.e. "${(j: :)${(q)@}}" of each call)
typeset -gaH ZPLG_COMPDEF_REPLAY
# }}}
#
# Init {{{
#
zmodload zsh/zutil || return 1
zmodload zsh/parameter || return 1
zmodload zsh/terminfo 2>/dev/null
zmodload zsh/termcap 2>/dev/null
if [[ ( -n "${terminfo[colors]}" || -n "${termcap[Co]}" ) && -z "${functions[colors]}" ]]; then
autoload colors
colors
fi
typeset -gAH ZPLG_COL
ZPLG_COL=(
"title" ""
"pname" "${fg_bold[yellow]}"
"uname" "${fg_bold[magenta]}"
"keyword" "${fg_bold[green]}"
"error" "${fg_bold[red]}"
"p" "${fg_bold[blue]}"
"bar" "${fg_bold[magenta]}"
"info" "${fg_bold[green]}"
"uninst" "${fg_bold[blue]}"
"success" "${fg_bold[green]}"
"failure" "${fg_bold[red]}"
"rst" "$reset_color"
)
# Load list of widgets
if [ "${(t)ZPLG_WIDGET_LIST}" != "association" ]; then
source "$ZPLG_DIR/widget-list.zsh"
fi
# List of hooks
typeset -gAH ZPLG_ZLE_HOOKS_LIST
ZPLG_ZLE_HOOKS_LIST=(
zle-line-init "1"
zle-line-finish "1"
paste-insert "1"
zle-isearch-exit "1"
zle-isearch-update "1"
zle-history-line-set "1"
zle-keymap-select "1"
)
# }}}
#
# Shadowing-related functions (names of substitute functions start with --) {{{
# Must be resistant to various game-changing options like KSH_ARRAYS
#
--zplg-reload-and-run () {
local fpath_prefix="$1" autoload_opts="$2" func="$3"
shift 3
# Unfunction caller function (its name is given)
unfunction "$func"
local FPATH="$fpath_prefix":"${FPATH}"
# After this the function exists again
builtin autoload $=autoload_opts "$func"
# User wanted to call the function, not only load it
"$func" "$@"
}
--zplg-shadow-autoload () {
local -a opts
local func
zparseopts -a opts -D ${(s::):-TUXkmtzw}
# TODO: +X
if [ -n "${opts[(r)-X]}" ]
then
-zplg-add-report "$ZPLG_CUR_USPL2" "Failed autoload $opts $*"
print -u2 "builtin autoload required for $opts"
return 1 # Testable
fi
if [ -n "${opts[(r)-w]}" ]
then
-zplg-add-report "$ZPLG_CUR_USPL2" "-w-Autoload $opts $*"
builtin autoload $opts "$@"
return # Testable
fi
# Report ZPLUGIN's "native" autoloads
local i
for i in "$@"; do
local msg="Autoload $i"
[ -n "$opts" ] && msg+=" with options ${opts[@]}"
-zplg-add-report "$ZPLG_CUR_USPL2" "$msg"
done
# Do ZPLUGIN's "native" autoloads
local PLUGIN_DIR="$ZPLG_PLUGINS_DIR/${ZPLG_CUR_USPL}"
for func
do
# Real autoload doesn't touch function if it already exists
if (( ${+functions[$func]} != 1 )); then
eval "function $func {
--zplg-reload-and-run ${(q)PLUGIN_DIR} ${(qq)opts[*]} ${(q)func} "'"$@"
}'
#functions[$func]="--zplg-reload-and-run ${(q)PLUGIN_DIR} ${(qq)opts[*]} ${(q)func} "'"$@"'
fi
done
# Testable
return 0
}
--zplg-shadow-bindkey() {
-zplg-add-report "$ZPLG_CUR_USPL2" "Bindkey $*"
# Remember to perform the actual bindkey call
typeset -a pos
pos=( "$@" )
# Check if we have regular bindkey call, i.e.
# with no options or with -s, plus possible -M
# option
local -A optsA
zparseopts -A optsA -D ${(s::):-lLdDAmrsevaR} "M:" "N:"
local -a opts
opts=( "${(k)optsA[@]}" )
if [[ "${#opts[@]}" -eq "0" ||
( "${#opts[@]}" -eq "1" && "${opts[(r)-M]}" = "-M" ) ||
( "${#opts[@]}" -eq "1" && "${opts[(r)-R]}" = "-R" ) ||
( "${#opts[@]}" -eq "1" && "${opts[(r)-s]}" = "-s" ) ||
( "${#opts[@]}" -le "2" && "${opts[(r)-M]}" = "-M" && "${opts[(r)-s]}" = "-s" ) ||
( "${#opts[@]}" -le "2" && "${opts[(r)-M]}" = "-M" && "${opts[(r)-R]}" = "-R" )
]]; then
local string="${(q)1}" widget="${(q)2}"
local quoted
# "-M map" given?
if [ "${opts[(r)-M]}" = "-M" ]; then
local Mopt="-M"
local Marg="${optsA[-M]}"
Mopt="${(q)Mopt}"
Marg="${(q)Marg}"
quoted="$string $widget $Mopt $Marg"
else
quoted="$string $widget"
fi
# -R given?
if [ "${opts[(r)-R]}" = "-R" ]; then
local Ropt="-R"
Ropt="${(q)Ropt}"
if [ "${opts[(r)-M]}" = "-M" ]; then
quoted="$quoted $Ropt"
else
# Two empty fields for non-existent -M arg
local space="_"
space="${(q)space}"
quoted="$quoted $space $space $Ropt"
fi
fi
quoted="${(q)quoted}"
# Remember the bindkey, only when load is in progress (it can be dstart that leads execution here)
[ -n "$ZPLG_CUR_USPL2" ] && ZPLG_BINDKEYS[$ZPLG_CUR_USPL2]+="$quoted "
# Remember for dtrace
[ "$ZPLG_DEBUG_ACTIVE" = "1" ] && ZPLG_BINDKEYS[$ZPLG_DEBUG_USPL2]+="$quoted "
else
# bindkey -A newkeymap main?
# Negative indices for KSH_ARRAYS immunity
if [[ "${#opts[@]}" -eq "1" && "${opts[(r)-A]}" = "-A" && "${#pos[@]}" = "3" && "${pos[-1]}" = "main" && "${pos[-2]}" != "-A" ]]; then
# Save a copy of main keymap
(( ZPLG_BINDKEY_MAIN_IDX ++ ))
local pname="${ZPLG_CUR_PLUGIN:-$ZPLG_DEBUG_PLUGIN}"
local name="${(q)pname}-main-$ZPLG_BINDKEY_MAIN_IDX"
builtin bindkey -N "${name}" main
# Remember occurence of main keymap substitution, to revert on unload
local keys="_" widget="_" optA="-A" mapname="${name}" optR="_"
local quoted="${(q)keys} ${(q)widget} ${(q)optA} ${(q)mapname} ${(q)optR}"
quoted="${(q)quoted}"
# Remember the bindkey, only when load is in progress (it can be dstart that leads execution here)
[ -n "$ZPLG_CUR_USPL2" ] && ZPLG_BINDKEYS[$ZPLG_CUR_USPL2]+="$quoted "
# Remember for dtrace
[ "$ZPLG_DEBUG_ACTIVE" = "1" ] && ZPLG_BINDKEYS[$ZPLG_DEBUG_USPL2]+="$quoted "
-zplg-add-report "$ZPLG_CUR_USPL2" "Warning: keymap \`main' copied to \`${name}' because of \`${pos[-2]}' substitution"
# bindkey -N newkeymap [other]
elif [[ "${#opts[@]}" -eq 1 && "${opts[(r)-N]}" = "-N" ]]; then
local Nopt="-N"
local Narg="${optsA[-N]}"
local keys="_" widget="_" optN="-N" mapname="${Narg}" optR="_"
local quoted="${(q)keys} ${(q)widget} ${(q)optN} ${(q)mapname} ${(q)optR}"
quoted="${(q)quoted}"
# Remember the bindkey, only when load is in progress (it can be dstart that leads execution here)
[ -n "$ZPLG_CUR_USPL2" ] && ZPLG_BINDKEYS[$ZPLG_CUR_USPL2]+="$quoted "
# Remember for dtrace
[ "$ZPLG_DEBUG_ACTIVE" = "1" ] && ZPLG_BINDKEYS[$ZPLG_DEBUG_USPL2]+="$quoted "
else
-zplg-add-report "$ZPLG_CUR_USPL2" "Warning: last bindkey used non-typical options: ${opts[*]}"
fi
fi
# A. Shadow off. Unfunction bindkey
# 0.autoload, A.bindkey, B.zstyle, C.alias, D.zle, E.compdef
(( ${+ZPLG_BACKUP_FUNCTIONS[bindkey]} )) && functions[bindkey]="${ZPLG_BACKUP_FUNCTIONS[bindkey]}" || unfunction "bindkey"
# Actual bindkey
bindkey "${pos[@]}"
integer ret=$?
# A. Shadow on. Custom function could unfunction itself
(( ${+functions[bindkey]} )) && ZPLG_BACKUP_FUNCTIONS[bindkey]="${functions[bindkey]}" || unset "ZPLG_BACKUP_FUNCTIONS[bindkey]"
function bindkey { --zplg-shadow-bindkey "$@"; }
return $ret # testable
}
--zplg-shadow-zstyle() {
-zplg-add-report "$ZPLG_CUR_USPL2" "Zstyle $*"
# Remember to perform the actual zstyle call
typeset -a pos
pos=( "$@" )
# Check if we have regular zstyle call, i.e.
# with no options or with -e
local -a opts
zparseopts -a opts -D ${(s::):-eLdgabsTtm}
if [[ "${#opts[@]}" -eq 0 || ( "${#opts[@]}" -eq 1 && "${opts[(r)-e]}" = "-e" ) ]]; then
# Have to quote $1, then $2, then concatenate them, then quote them again
local pattern="${(q)1}" style="${(q)2}"
local ps="$pattern $style"
ps="${(q)ps}"
# Remember the zstyle, only when load is in progress (it can be dstart that leads execution here)
[ -n "$ZPLG_CUR_USPL2" ] && ZPLG_ZSTYLES[$ZPLG_CUR_USPL2]+="$ps "
# Remember for dtrace
[ "$ZPLG_DEBUG_ACTIVE" = "1" ] && ZPLG_ZSTYLES[$ZPLG_DEBUG_USPL2]+="$ps "
else
if [[ ! "${#opts[@]}" = "1" && ( "${opts[(r)-s]}" = "-s" || "${opts[(r)-b]}" = "-b" || "${opts[(r)-a]}" = "-a" ||
"${opts[(r)-t]}" = "-t" || "${opts[(r)-T]}" = "-T" || "${opts[(r)-m]}" = "-m" ) ]]
then
-zplg-add-report "$ZPLG_CUR_USPL2" "Warning: last zstyle used non-typical options: ${opts[*]}"
fi
fi
# B. Shadow off. Unfunction zstyle
# 0.autoload, A.bindkey, B.zstyle, C.alias, D.zle, E.compdef
(( ${+ZPLG_BACKUP_FUNCTIONS[zstyle]} )) && functions[zstyle]="${ZPLG_BACKUP_FUNCTIONS[zstyle]}" || unfunction "zstyle"
# Actual zstyle
zstyle "${pos[@]}"
integer ret=$?
# B. Shadow on. Custom function could unfunction itself
(( ${+functions[zstyle]} )) && ZPLG_BACKUP_FUNCTIONS[zstyle]="${functions[zstyle]}" || unset "ZPLG_BACKUP_FUNCTIONS[zstyle]"
function zstyle { --zplg-shadow-zstyle "$@"; }
return $ret # testable
}
--zplg-shadow-alias() {
-zplg-add-report "$ZPLG_CUR_USPL2" "Alias $*"
# Remember to perform the actual alias call
typeset -a pos
pos=( "$@" )
local -a opts
zparseopts -a opts -D ${(s::):-gs}
local a quoted tmp
for a in "$@"; do
local aname="${a%%[=]*}"
local avalue="${a#*=}"
# Check if alias is to be redefined
(( ${+aliases[$aname]} )) && -zplg-add-report "$ZPLG_CUR_USPL2" "Warning: redefining alias \`${aname}', previous value: ${avalue}"
aname="${(q)aname}"
local bname="${(q)avalue}"
if [ "${opts[(r)-s]}" = "-s" ]; then
tmp="-s"
tmp="${(q)tmp}"
quoted="$aname $bname $tmp"
elif [ "${opts[(r)-g]}" = "-g" ]; then
tmp="-g"
tmp="${(q)tmp}"
quoted="$aname $bname $tmp"
else
quoted="$aname $bname"
fi
quoted="${(q)quoted}"
# Remember the alias, only when load is in progress (it can be dstart that leads execution here)
[ -n "$ZPLG_CUR_USPL2" ] && ZPLG_ALIASES[$ZPLG_CUR_USPL2]+="$quoted "
# Remember for dtrace
[ "$ZPLG_DEBUG_ACTIVE" = "1" ] && ZPLG_ALIASES[$ZPLG_DEBUG_USPL2]+="$quoted "
done
# C. Shadow off. Unfunction alias
# 0.autoload, A.bindkey, B.zstyle, C.alias, D.zle, E.compdef
(( ${+ZPLG_BACKUP_FUNCTIONS[alias]} )) && functions[alias]="${ZPLG_BACKUP_FUNCTIONS[alias]}" || unfunction "alias"
# Actual alias
alias "${pos[@]}"
integer ret=$?
# C. Shadow on. Custom function could unfunction itself
(( ${+functions[alias]} )) && ZPLG_BACKUP_FUNCTIONS[alias]="${functions[alias]}" || unset "ZPLG_BACKUP_FUNCTIONS[compdef]"
function alias { --zplg-shadow-alias "$@"; }
return $ret # testable
}
--zplg-shadow-zle() {
-zplg-add-report "$ZPLG_CUR_USPL2" "Zle $*"
# Remember to perform the actual zle call
typeset -a pos
pos=( "$@" )
# Try to catch game-changing "-N"
if [[ "$1" = "-N" && "$#" = "3" ]]; then
# Hooks
if [ "${ZPLG_ZLE_HOOKS_LIST[$2]}" = "1" ]; then
local quoted="$2"
quoted="${(q)quoted}"
# Remember only when load is in progress (it can be dstart that leads execution here)
[ -n "$ZPLG_CUR_USPL2" ] && ZPLG_WIDGETS_DELETE[$ZPLG_CUR_USPL2]+="$quoted "
# These will be saved and restored
elif [ "${ZPLG_WIDGET_LIST[$2]}" = "1" ]; then
# Have to remember original widget "$2" and
# the copy that it's going to be done
local widname="$2" saved_widname="zplugin-saved-$2"
builtin zle -A "$widname" "$saved_widname"
widname="${(q)widname}"
saved_widname="${(q)saved_widname}"
local quoted="$widname $saved_widname"
quoted="${(q)quoted}"
# Remember only when load is in progress (it can be dstart that leads execution here)
[ -n "$ZPLG_CUR_USPL2" ] && ZPLG_WIDGETS_SAVED[$ZPLG_CUR_USPL2]+="$quoted "
# Remember for dtrace
[ "$ZPLG_DEBUG_ACTIVE" = "1" ] && ZPLG_WIDGETS_SAVED[$ZPLG_DEBUG_USPL2]+="$quoted "
# These will be deleted
else
-zplg-add-report "$ZPLG_CUR_USPL2" "Warning: unknown widget replaced/taken via zle -N: \`$2', it is set to be deleted"
local quoted="$2"
quoted="${(q)quoted}"
# Remember only when load is in progress (it can be dstart that leads execution here)
[ -n "$ZPLG_CUR_USPL2" ] && ZPLG_WIDGETS_DELETE[$ZPLG_CUR_USPL2]+="$quoted "
# Remember for dtrace
[ "$ZPLG_DEBUG_ACTIVE" = "1" ] && ZPLG_WIDGETS_DELETE[$ZPLG_DEBUG_USPL2]+="$quoted "
fi
# Creation of new widgets. They will be removed on unload
elif [[ "$1" = "-N" && "$#" = "2" ]]; then
local quoted="$2"
quoted="${(q)quoted}"
# Remember only when load is in progress (it can be dstart that leads execution here)
[ -n "$ZPLG_CUR_USPL2" ] && ZPLG_WIDGETS_DELETE[$ZPLG_CUR_USPL2]+="$quoted "
# Remember for dtrace
[ "$ZPLG_DEBUG_ACTIVE" = "1" ] && ZPLG_WIDGETS_DELETE[$ZPLG_DEBUG_USPL2]+="$quoted "
fi
# D. Shadow off. Unfunction zle
# 0.autoload, A.bindkey, B.zstyle, C.alias, D.zle, E.compdef
(( ${+ZPLG_BACKUP_FUNCTIONS[zle]} )) && functions[zle]="${ZPLG_BACKUP_FUNCTIONS[zle]}" || unfunction "zle"
# Actual zle
zle "${pos[@]}"
integer ret=$?
# D. Shadow on. Custom function could unfunction itself
(( ${+functions[zle]} )) && ZPLG_BACKUP_FUNCTIONS[zle]="${functions[zle]}" || unset "ZPLG_BACKUP_FUNCTIONS[zle]"
function zle { --zplg-shadow-zle "$@"; }
return $ret # testable
}
--zplg-shadow-compdef() {
# No reply needed when compdef exists
if (( ${+ZPLG_BACKUP_FUNCTIONS[compdef]} )); then
-zplg-add-report "$ZPLG_CUR_USPL2" "Compdef $*"
# E. Shadow off. Unfunction "compdef"
# 0.autoload, A.bindkey, B.zstyle, C.alias, D.zle, E.compdef
functions[compdef]="${ZPLG_BACKUP_FUNCTIONS[compdef]}"
# Actual compdef
compdef "$@"
integer ret=$?
# E. Shadow on. Custom compdef could unfunction itself
(( ${+functions[compdef]} )) && ZPLG_BACKUP_FUNCTIONS[compdef]="${functions[compdef]}" || unset "ZPLG_BACKUP_FUNCTIONS[compdef]"
function compdef { --zplg-shadow-compdef "$@"; }
# Save compdef call for replay
else
-zplg-add-report "$ZPLG_CUR_USPL2" "Saving \`compdef $*' for replay"
ZPLG_COMPDEF_REPLAY+=( "${(j: :)${(q)@}}" )
fi
return $ret # testable
}
# Shadowing on completely for a given mode ("load", "light" or "compdef")
-zplg-shadow-on() {
local mode="$1"
# Enable shadowing only once
#
# One could expect possibility of widening of shadowing, however
# such sequence doesn't exist, e.g. "light" then "load"/"dtrace",
# "compdef" then "load"/"dtrace", "light" then "compdef",
# "compdef" then "light"
#
# It is always "dtrace" then "load" (i.e. dtrace then load)
# "dtrace" then "light" (i.e. dtrace then light load)
# "dtrace" then "compdef" (i.e. dtrace then snippet)
[ "$ZPLG_SHADOWING_ACTIVE" != "inactive" ] && return 0
ZPLG_SHADOWING_ACTIVE="$mode"
# The point about backuping is: does the key exist in functions array
# If it does exist, then it will also exist in ZPLG_BACKUP_FUNCTIONS
# Defensive code, shouldn't be needed
unset "ZPLG_BACKUP_FUNCTIONS[autoload]" # 0.
unset "ZPLG_BACKUP_FUNCTIONS[compdef]" # E.
if [ "$mode" != "compdef" ]; then
# 0. Used, but not in temporary restoration, which doesn't happen for autoload
(( ${+functions[autoload]} )) && ZPLG_BACKUP_FUNCTIONS[autoload]="${functions[autoload]}"
function autoload { --zplg-shadow-autoload "$@"; }
fi
# E. Always shadow compdef
(( ${+functions[compdef]} )) && ZPLG_BACKUP_FUNCTIONS[compdef]="${functions[compdef]}"
function compdef { --zplg-shadow-compdef "$@"; }
# Light and compdef shadowing stops here. Dtrace and load go on
[[ "$mode" = "light" || "$mode" = "compdef" ]] && return 0
# Defensive code, shouldn't be needed
unset "ZPLG_BACKUP_FUNCTIONS[bindkey]" # A.
unset "ZPLG_BACKUP_FUNCTIONS[zstyle]" # B.
unset "ZPLG_BACKUP_FUNCTIONS[alias]" # C.
unset "ZPLG_BACKUP_FUNCTIONS[zle]" # D.
# A.
(( ${+functions[bindkey]} )) && ZPLG_BACKUP_FUNCTIONS[bindkey]="${functions[bindkey]}"
function bindkey { --zplg-shadow-bindkey "$@"; }
# B.
(( ${+functions[zstyle]} )) && ZPLG_BACKUP_FUNCTIONS[zstyle]="${functions[zstyle]}"
function zstyle { --zplg-shadow-zstyle "$@"; }
# C.
(( ${+functions[alias]} )) && ZPLG_BACKUP_FUNCTIONS[alias]="${functions[alias]}"
function alias { --zplg-shadow-alias "$@"; }
# D.
(( ${+functions[zle]} )) && ZPLG_BACKUP_FUNCTIONS[zle]="${functions[zle]}"
function zle { --zplg-shadow-zle "$@"; }
return 0
}
# Shadowing off completely for a given mode "load", "light" or "compdef"
-zplg-shadow-off() {
local mode="$1"
# Disable shadowing only once
[ "$ZPLG_SHADOWING_ACTIVE" = "inactive" ] && return 0
# Disable shadowing only the way it was enabled first
[ "$ZPLG_SHADOWING_ACTIVE" != "$mode" ] && return 0
ZPLG_SHADOWING_ACTIVE="inactive"
if [ "$mode" != "compdef" ]; then
# 0. Unfunction "autoload"
(( ${+ZPLG_BACKUP_FUNCTIONS[autoload]} )) && functions[autoload]="${ZPLG_BACKUP_FUNCTIONS[autoload]}" || unfunction "autoload"
fi
# E. Restore original compdef if it existed
(( ${+ZPLG_BACKUP_FUNCTIONS[compdef]} )) && functions[compdef]="${ZPLG_BACKUP_FUNCTIONS[compdef]}" || unfunction "compdef"
# Light and comdef shadowing stops here
[[ "$mode" = "light" || "$mode" = "compdef" ]] && return 0
# Unfunction shadowing functions
# A.
(( ${+ZPLG_BACKUP_FUNCTIONS[bindkey]} )) && functions[bindkey]="${ZPLG_BACKUP_FUNCTIONS[bindkey]}" || unfunction "bindkey"
# B.
(( ${+ZPLG_BACKUP_FUNCTIONS[zstyle]} )) && functions[zstyle]="${ZPLG_BACKUP_FUNCTIONS[zstyle]}" || unfunction "zstyle"
# C.
(( ${+ZPLG_BACKUP_FUNCTIONS[alias]} )) && functions[alias]="${ZPLG_BACKUP_FUNCTIONS[alias]}" || unfunction "alias"
# D.
(( ${+ZPLG_BACKUP_FUNCTIONS[zle]} )) && functions[zle]="${ZPLG_BACKUP_FUNCTIONS[zle]}" || unfunction "zle"
return 0
}
# }}}
#
# Function diff functions {{{
#
# Can remember current $functions twice, and compute the
# difference, storing it in ZPLG_FUNCTIONS, associated
# with given ($1) plugin
-zplg-diff-functions() {
local uspl2="$1"
local cmd="$2"
case "$cmd" in
begin)
ZPLG_FUNCTIONS_BEFORE[$uspl2]="${(j: :)${(qk)functions[@]}}"
ZPLG_FUNCTIONS[$uspl2]=""
ZPLG_FUNCTIONS_DIFF_RAN[$uspl2]="0"
;;
end)
ZPLG_FUNCTIONS_AFTER[$uspl2]="${(j: :)${(qk)functions[@]}}"
ZPLG_FUNCTIONS[$uspl2]=""
ZPLG_FUNCTIONS_DIFF_RAN[$uspl2]="0"
;;
diff)
# Run diff once, `begin' or `end' is needed to be run again for a new diff
[ "${ZPLG_FUNCTIONS_DIFF_RAN[$uspl2]}" = "1" ] && return 0
ZPLG_FUNCTIONS_DIFF_RAN[$uspl2]="1"
# Cannot run diff if *_BEFORE or *_AFTER variable is not set
# Following is paranoid for *_BEFORE and *_AFTER being only spaces
setopt localoptions extendedglob
[[ "${ZPLG_FUNCTIONS_BEFORE[$uspl2]}" != *[$'! \t']* || "${ZPLG_FUNCTIONS_AFTER[$uspl2]}" != *[$'! \t']* ]] && return 1
typeset -A func
local i
# This includes new functions. Quoting is kept (i.e. no i=${(Q)i})
for i in "${(z)ZPLG_FUNCTIONS_AFTER[$uspl2]}"; do
func[$i]=1
done
# Remove duplicated entries, i.e. existing before. Quoting is kept
for i in "${(z)ZPLG_FUNCTIONS_BEFORE[$uspl2]}"; do
# if would do unset, then: func[opp+a\[]: invalid parameter name
func[$i]=0
done
# Store the functions, associating them with plugin ($uspl2)
for i in "${(onk)func[@]}"; do
[ "${func[$i]}" = "1" ] && ZPLG_FUNCTIONS[$uspl2]+="$i "
done
;;
*)
return 1
esac
return 0
}
# Creates a one or two columns text with functions
# belonging to given ($1) plugin
-zplg-format-functions() {
local uspl2="$1"
typeset -a func
func=( "${(z)ZPLG_FUNCTIONS[$uspl2]}" )
# Get length of longest left-right string pair,
# and length of longest left string
integer longest=0 longest_left=0 cur_left_len=0 count=1
local f
for f in "${(on)func[@]}"; do
[ -z "${#f}" ] && continue
f="${(Q)f}"
# Compute for elements in left column,
# ones that will be paded with spaces
if (( count ++ % 2 != 0 )); then
[ "${#f}" -gt "$longest_left" ] && longest_left="${#f}"
cur_left_len="${#f}"
else
cur_left_len+="${#f}"
cur_left_len+=1 # For separating space
[ "$cur_left_len" -gt "$longest" ] && longest="$cur_left_len"
fi
done
# Output in one or two columns
local answer=""
count=1
for f in "${(on)func[@]}"; do
[ -z "$f" ] && continue
f="${(Q)f}"
if (( COLUMNS >= longest )); then
if (( count ++ % 2 != 0 )); then
answer+="${(r:longest_left+1:: :)f}"
else
answer+="$f"$'\n'
fi
else
answer+="$f"$'\n'
fi
done
REPLY="$answer"
# == 0 is: next element would have newline (postfix addition in "count ++")
(( COLUMNS >= longest && count % 2 == 0 )) && REPLY="$REPLY"$'\n'
}
# }}}
#
# Option diff functions {{{
#
# Can remember current $options twice. After that can
# detect any change between the two saves. Changed
# options are appended as they were in first save to
# ZPLG_OPTIONS, all associated with given ($1) plugin
-zplg-diff-options() {
local uspl2="$1"
local cmd="$2"
case "$cmd" in
begin)
local bIFS="$IFS"; IFS=" "
ZPLG_OPTIONS_BEFORE[$uspl2]="${(kv)options[@]}"
IFS="$bIFS"
ZPLG_OPTIONS[$uspl2]=""
ZPLG_OPTIONS_DIFF_RAN[$uspl2]="0"
;;
end)
local bIFS="$IFS"; IFS=" "
ZPLG_OPTIONS_AFTER[$uspl2]="${(kv)options[@]}"
IFS="$bIFS"
ZPLG_OPTIONS[$uspl2]=""
ZPLG_OPTIONS_DIFF_RAN[$uspl2]="0"
;;
diff)
# Run diff once, `begin' or `end' is needed to be run again for a new diff
[ "${ZPLG_OPTIONS_DIFF_RAN[$uspl2]}" = "1" ] && return 0
ZPLG_OPTIONS_DIFF_RAN[$uspl2]="1"
# Cannot run diff if *_BEFORE or *_AFTER variable is not set
# Following is paranoid for *_BEFORE and *_AFTER being only spaces
setopt localoptions extendedglob
[[ "${ZPLG_OPTIONS_BEFORE[$uspl2]}" != *[$'! \t']* || "${ZPLG_OPTIONS_AFTER[$uspl2]}" != *[$'! \t']* ]] && return 1
typeset -A opts_before opts_after opts
opts_before=( "${(z)ZPLG_OPTIONS_BEFORE[$uspl2]}" )
opts_after=( "${(z)ZPLG_OPTIONS_AFTER[$uspl2]}" )
opts=( )
# Iterate through first array (keys the same
# on both of them though) and test for a change
local key
for key in "${(k)opts_before[@]}"; do
if [ "${opts_before[$key]}" != "${opts_after[$key]}" ]; then
opts[$key]="${opts_before[$key]}"
fi
done
# Serialize for reporting
local bIFS="$IFS"; IFS=" "
ZPLG_OPTIONS[$uspl2]="${(kv)opts[@]}"
IFS="$bIFS"
;;
*)
return 1
esac
return 0
}
# Creates a text about options that changed when loaded plugin "$1"
-zplg-format-options() {
local uspl2="$1"
REPLY=""
# Paranoid, don't want bad key/value pair error
integer empty=0
-zplg-save-set-extendedglob
[[ "${ZPLG_OPTIONS[$uspl2]}" != *[$'! \t']* ]] && empty=1
-zplg-restore-extendedglob
(( empty )) && return 0
typeset -A opts
opts=( "${(z)ZPLG_OPTIONS[$uspl2]}" )
# Get length of longest option
integer longest=0
local k
for k in "${(kon)opts[@]}"; do
[ "${#k}" -gt "$longest" ] && longest="${#k}"
done
# Output in one column
local txt
for k in "${(kon)opts[@]}"; do
[ "${opts[$k]}" = "on" ] && txt="was unset" || txt="was set"
REPLY+="${(r:longest+1:: :)k}$txt"$'\n'
done
}
# }}}
#
# Environment diff functions {{{
#
-zplg-diff-env() {
local uspl2="$1"
local cmd="$2"
typeset -a tmp
case "$cmd" in
begin)
local bIFS="$IFS"; IFS=" "
tmp=( "${(q)path[@]}" )
ZPLG_PATH_BEFORE[$uspl2]="${tmp[*]}"
tmp=( "${(q)fpath[@]}" )
ZPLG_FPATH_BEFORE[$uspl2]="${tmp[*]}"
IFS="$bIFS"
# Reset diffing
ZPLG_PATH[$uspl2]=""
ZPLG_FPATH[$uspl2]=""
ZPLG_ENV_DIFF_RAN[$uspl2]="0"
;;
end)
local bIFS="$IFS"; IFS=" "
tmp=( "${(q)path[@]}" )
ZPLG_PATH_AFTER[$uspl2]="${tmp[*]}"
tmp=( "${(q)fpath[@]}" )
ZPLG_FPATH_AFTER[$uspl2]="${tmp[*]}"
IFS="$bIFS"
# Reset diffing
ZPLG_PATH[$uspl2]=""
ZPLG_FPATH[$uspl2]=""
ZPLG_ENV_DIFF_RAN[$uspl2]="0"
;;
diff)
# Run diff once, `begin' or `end' is needed to be run again for a new diff
[ "${ZPLG_ENV_DIFF_RAN[$uspl2]}" = "1" ] && return 0
ZPLG_ENV_DIFF_RAN[$uspl2]="1"
# Cannot run diff if *_BEFORE or *_AFTER variable is not set
# Following is paranoid for *_BEFORE and *_AFTER being only spaces
setopt localoptions extendedglob
[[ "${ZPLG_PATH_BEFORE[$uspl2]}" != *[$'! \t']* || "${ZPLG_PATH_AFTER[$uspl2]}" != *[$'! \t']* ]] && return 1
[[ "${ZPLG_FPATH_BEFORE[$uspl2]}" != *[$'! \t']* || "${ZPLG_FPATH_AFTER[$uspl2]}" != *[$'! \t']* ]] && return 1
typeset -A path_state fpath_state
local i
#
# PATH processing