-
Notifications
You must be signed in to change notification settings - Fork 41
/
zi.zsh
executable file
·2977 lines (2851 loc) · 135 KB
/
zi.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: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
#
# Copyright (c) 2021 Salvydas Lukosius and Z-Shell Community.
# Distributed under the terms of the MIT License.
typeset -gaH ZI_REGISTERED_PLUGINS ZI_TASKS ZI_RUN
typeset -ga zsh_loaded_plugins
if (( !${#ZI_TASKS} )) { ZI_TASKS=( "<no-data>" ); }
# Rename snippets URL -> NAME.
typeset -gAH ZI ZI_SNIPPETS ZI_REPORTS ZI_ICES ZI_SICE ZI_CUR_BIND_MAP ZI_EXTS ZI_EXTS2
typeset -gaH ZI_COMPDEF_REPLAY
# The specified names are marked for automatic export to the environment of subsequently executed commands.
typeset -gx ZPFX XDG_ZI_HOME XDG_ZI_CACHE XDG_ZI_CONFIG PMSPEC=0fuUpiPsX
# Compatibility for previous versions.
typeset -gAH ZINIT
ZI=( "${(kv)ZINIT[@]}" "${(kv)ZI[@]}" )
unset ZINIT
# An ice-modifiers list.
ZI[ice-list]="svn|proto|from|teleid|bindmap|cloneopts|id-as|depth|if|wait|load|unload|blockf|pick|bpick|src|as|\
ver|silent|lucid|notify|mv|cp|atinit|atclone|atload|atpull|nocd|run-atpull|has|cloneonly|make|service|trackbinds|\
multisrc|compile|nocompile|nocompletions|reset-prompt|wrap|reset|sh|\!sh|bash|\!bash|ksh|\!ksh|csh|\!csh|aliases|\
countdown|ps-on-unload|ps-on-update|trigger-load|light-mode|is-snippet|atdelete|pack|git|verbose|on-update-of|\
subscribe|extract|param|opts|autoload|subst|install|pullopts|debug|null|binary"
# In no value, i.e. not designed to hold value.
ZI[nval-ice-list]="blockf|silent|lucid|trackbinds|cloneonly|nocd|run-atpull|nocompletions|sh|\!sh|bash|\!bash|\
ksh|\!ksh|csh|\!csh|aliases|countdown|light-mode|is-snippet|git|verbose|cloneopts|pullopts|debug|null|binary|make|\
nocompile|reset"
# Subcommands list
ZI[cmd-list]="-h|--help|help|subcmds|icemods|analytics|man|self-update|times|zstatus|load|light|unload|\
snippet|ls|ice|update|status|report|delete|loaded|list|cd|create|edit|glance|stress|changes|recently|clist|completions|\
cclear|cdisable|cenable|creinstall|cuninstall|csearch|compinit|dtrace|dstart|dstop|dunload|dreport|dclear|compile|\
uncompile|compiled|cdlist|cdreplay|cdclear|srv|recall|env-whitelist|bindkeys|module|add-fpath|run"
# Establish ZI[BIN_DIR]
[[ ! -e ${ZI[BIN_DIR]}/zi.zsh ]] && ZI[BIN_DIR]=
ZI[ZERO]="${ZERO:-${${0:#$ZSH_ARGZERO}:-${(%):-%N}}}"
[[ ! -o functionargzero || ${options[posixargzero]} = on || ${ZI[ZERO]} != */* ]] && ZI[ZERO]="${(%):-%N}"
: ${ZI[BIN_DIR]:="${ZI[ZERO]:h}"}
[[ ${ZI[BIN_DIR]} = \~* ]] && ZI[BIN_DIR]=${~ZI[BIN_DIR]}
ZI[BIN_DIR]="${${(M)ZI[BIN_DIR]:#/*}:-$PWD/${ZI[BIN_DIR]}}"
# Check if ZI[BIN_DIR] is established correctly.
if [[ ! -e ${ZI[BIN_DIR]}/zi.zsh ]]; then
builtin print -P "%F{196}Could not establish ZI[BIN_DIR] hash field. It should point where ❮ Zi ❯ Git repository is.%f"
return 1
fi
# Zi home path for creating working directories.
if [[ -z $ZI[HOME_DIR] ]]; then
if [[ -d $HOME ]]; then
ZI[HOME_DIR]="${HOME}/.zi"
elif [[ -d $ZDOTDIR ]]; then
ZI[HOME_DIR]="${ZDOTDIR}/.zi"
elif [[ -d $XDG_DATA_HOME ]]; then
ZI[HOME_DIR]="${XDG_DATA_HOME}/.zi"
else
ZI[HOME_DIR]="${HOME}/.zi"
fi
fi
# Directory for Zi cache files.
if [[ -z $ZI[CACHE_DIR] ]]; then
if [[ -d ${HOME}/.cache ]]; then
ZI[CACHE_DIR]="${HOME}/.cache/zi"
elif [[ -d $XDG_CACHE_HOME ]]; then
ZI[CACHE_DIR]="${XDG_CACHE_HOME}/zi"
elif [[ -d ${ZDOTDIR}/.cache ]]; then
ZI[CACHE_DIR]="${ZDOTDIR}/.cache/zi"
elif [[ -d ${XDG_DATA_HOME}/.cache ]]; then
ZI[CACHE_DIR]="${XDG_DATA_HOME}/.cache/zi"
else
ZI[CACHE_DIR]="${HOME}/.cache/zi"
fi
fi
# Directory for Zi configuration files.
if [[ -z $ZI[CONFIG_DIR] ]]; then
if [[ -d ${HOME}/.config ]]; then
ZI[CONFIG_DIR]="${HOME}/.config/zi"
elif [[ -d $XDG_CONFIG_HOME ]]; then
ZI[CONFIG_DIR]="${XDG_CONFIG_HOME}/zi"
elif [[ -d ${XDG_DATA_HOME}/.config ]]; then
ZI[CONFIG_DIR]="${XDG_DATA_HOME}/.config/zi"
else
ZI[CONFIG_DIR]="${HOME}/.config/zi"
fi
fi
# Base Directories
# https://wiki.zshell.dev/docs/guides/customization#customizing-paths
# User/Device specific directory for software/data files.
# https://wiki.zshell.dev/community/zsh_plugin_standard#global-parameter-with-prefix
: ${ZPFX:=${ZI[HOME_DIR]}/polaris}
# Directory to store manpage.
: ${ZI[MAN_DIR]:=${ZPFX}/man}
# The cdpath variable sets the search path for the cd command.
: ${ZI[CDPATH_DIR]:=${ZPFX}/usr/cd_path}
: ${ZI[MAIL_DIR]:=${ZPFX}/usr/mail}
: ${ZI[LOG_DIR]:=${ZI[CACHE_DIR]}/log}
: ${ZI[THEMES_DIR]:=${ZI[HOME_DIR]}/themes}
: ${ZI[PLUGINS_DIR]:=${ZI[HOME_DIR]}/plugins}
: ${ZI[SNIPPETS_DIR]:=${ZI[HOME_DIR]}/snippets}
: ${ZI[SERVICES_DIR]:=${ZI[HOME_DIR]}/services}
: ${ZI[ZMODULES_DIR]:=${ZI[HOME_DIR]}/zmodules}
: ${ZI[ZCOMPDUMP_PATH]:=${ZI[CACHE_DIR]}/.zcompdump}
: ${ZI[COMPLETIONS_DIR]:=${ZI[HOME_DIR]}/completions}
# Additional/Optional directories for software/data files.
: ${ZI[NODE_PATH_DIR]:=${ZPFX}/lib/node_modules}
# Base Directory Specification (XDG)
# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
: ${XDG_ZI_HOME:=${ZI[HOME_DIR]}}
: ${XDG_ZI_CACHE:=${ZI[CACHE_DIR]}}
: ${XDG_ZI_CONFIG:=${ZI[CONFIG_DIR]}}
# Options Specification
: ${ZI[ALIASES_OPT]:=${${options[aliases]:#off}:+1}}
: ${ZI[PKG_OWNER]:=z-shell}
ZI[HOME_DIR]=${~ZI[HOME_DIR]}
ZI[MAN_DIR]=${~ZI[MAN_DIR]}
ZI[CDPATH_DIR]=${~ZI[CDPATH_DIR]}
ZI[LOG_DIR]=${~ZI[LOG_DIR]}
ZI[MAIL_DIR]=${~ZI[MAIL_DIR]}
ZI[CACHE_DIR]=${~ZI[CACHE_DIR]}
ZI[THEMES_DIR]=${~ZI[THEMES_DIR]}
ZI[CONFIG_DIR]=${~ZI[CONFIG_DIR]}
ZI[PLUGINS_DIR]=${~ZI[PLUGINS_DIR]}
ZI[SNIPPETS_DIR]=${~ZI[SNIPPETS_DIR]}
ZI[SERVICES_DIR]=${~ZI[SERVICES_DIR]}
ZI[ZMODULES_DIR]=${~ZI[ZMODULES_DIR]}
ZI[ZCOMPDUMP_PATH]=${~ZI[ZCOMPDUMP_PATH]}
ZI[COMPLETIONS_DIR]=${~ZI[COMPLETIONS_DIR]}
ZI[NODE_PATH_DIR]=${~ZI[NODE_PATH_DIR]}
ZPFX=${~ZPFX}
XDG_ZI_HOME=${~ZI[HOME_DIR]}
XDG_ZI_CACHE=${~ZI[CACHE_DIR]}
XDG_ZI_CONFIG=${~ZI[CONFIG_DIR]}
if [[ -z ${manpath[(re)${ZI[MAN_DIR]}]} ]] && [[ -d ${ZI[MAN_DIR]} ]]; then
manpath=( "${ZI[MAN_DIR]}" "${manpath[@]}" )
typeset -gxU manpath
fi
if [[ -z ${cdpath[(re)${ZI[CDPATH_DIR]}]} ]] && [[ -d ${ZI[CDPATH_DIR]} ]]; then
builtin setopt auto_cd
cdpath=( "${ZI[CDPATH_DIR]}" "${cdpath[@]}" )
typeset -gxU cdpath CDPATH
fi
if [[ -z ${mailpath[(re)${ZI[MAIL_DIR]}]} ]]; then
mailpath=( "${ZI[MAIL_DIR]}" "${mailpath[@]}" )
typeset -gxU mailpath MAILPATH
fi
if [[ -z ${path[(re)${ZPFX}/bin]} ]] && [[ -d ${ZPFX}/bin ]]; then
path=( "${ZPFX}/bin" "${path[@]}" )
typeset -gxU path PATH
fi
if [[ -z ${path[(re)${ZPFX}/sbin]} ]] && [[ -d ${ZPFX}/sbin ]]; then
path=( "${ZPFX}/sbin" "${path[@]}" )
typeset -gxU path PATH
fi
if [[ -z ${fpath[(re)${ZI[COMPLETIONS_DIR]}]} ]]; then
fpath=( "${ZI[COMPLETIONS_DIR]}" "${fpath[@]}" )
typeset -gxU fpath FPATH
fi
# Export/assign/tie new paths.
if [[ -z ${logpath[(re)${ZI[LOG_DIR]}]} ]] && [[ -d ${ZI[LOG_DIR]} ]]; then
logpath=( "${ZI[LOG_DIR]}" "${logpath[@]}" )
typeset -gxU logpath LOG_PATH
typeset -gxTU LOG_PATH logpath
fi
if [[ -z ${nodepath[(re)${ZI[NODE_PATH_DIR]}]} ]] && [[ -d ${ZI[NODE_PATH_DIR]} ]]; then
nodepath=( "${ZI[NODE_PATH_DIR]}" "${nodepath[@]}" )
typeset -gxU nodepath NODE_PATH
typeset -gxTU NODE_PATH nodepath
fi
ZI[UPAR]=";:^[[A;:^[OA;:\\e[A;:\\eOA;:${termcap[ku]/$'\e'/^\[};:${terminfo[kcuu1]/$'\e'/^\[};:"
ZI[DOWNAR]=";:^[[B;:^[OB;:\\e[B;:\\eOB;:${termcap[kd]/$'\e'/^\[};:${terminfo[kcud1]/$'\e'/^\[};:"
ZI[RIGHTAR]=";:^[[C;:^[OC;:\\e[C;:\\eOC;:${termcap[kr]/$'\e'/^\[};:${terminfo[kcuf1]/$'\e'/^\[};:"
ZI[LEFTAR]=";:^[[D;:^[OD;:\\e[D;:\\eOD;:${termcap[kl]/$'\e'/^\[};:${terminfo[kcub1]/$'\e'/^\[};:"
builtin autoload -Uz is-at-least
is-at-least 5.1 && ZI[NEW_AUTOLOAD]=1 || ZI[NEW_AUTOLOAD]=0
#is-at-least 5.4 && ZI[NEW_AUTOLOAD]=2
# Parameters - temporary substituting of functions. [[[
ZI[TMP_SUBST]=inactive ZI[DTRACE]=0 ZI[CUR_PLUGIN]=
# ]]]
# Parameters - ICE. [[[
typeset -gA ZI_1MAP ZI_2MAP
ZI_1MAP=(
OMZ:: https://github.com/ohmyzsh/ohmyzsh/trunk/
OMZP:: https://github.com/ohmyzsh/ohmyzsh/trunk/plugins/
OMZT:: https://github.com/ohmyzsh/ohmyzsh/trunk/themes/
OMZL:: https://github.com/ohmyzsh/ohmyzsh/trunk/lib/
PZT:: https://github.com/sorin-ionescu/prezto/trunk/
PZTM:: https://github.com/sorin-ionescu/prezto/trunk/modules/
)
ZI_2MAP=(
OMZ:: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/
OMZP:: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/plugins/
OMZT:: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/themes/
OMZL:: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/lib/
PZT:: https://raw.githubusercontent.com/sorin-ionescu/prezto/master/
PZTM:: https://raw.githubusercontent.com/sorin-ionescu/prezto/master/modules/
)
# ]]]
# Initiate. [[[
zmodload zsh/zutil || { builtin print -P "%F{196}zsh/zutil module is required, aborting ❮ Zi ❯ set up.%f"; return 1; }
zmodload zsh/parameter || { builtin print -P "%F{196}zsh/parameter module is required, aborting ❮ Zi ❯ set up.%f"; return 1; }
zmodload zsh/terminfo 2>/dev/null
zmodload zsh/termcap 2>/dev/null
# Terminal color codes.
if [[ -z $SOURCED && ( ${+terminfo} -eq 1 && -n ${terminfo[colors]} ) || ( ${+termcap} -eq 1 && -n ${termcap[Co]} ) ]] {
ZI+=(
col-annex $'\e[38;5;165m' col-info $'\e[38;5;82m' col-p $'\e[38;5;81m'
col-apo $'\e[1;38;5;220m' col-info2 $'\e[38;5;220m' col-pkg $'\e[1;3;38;5;27m'
col-b $'\e[1m' col-info3 $'\e[1m\e[38;5;220m' col-pname $'\e[1;4m\e[32m'
col-aps $'\e[38;5;117m' col-baps $'\e[1;38;5;82m' col-pre $'\e[38;5;93m'
col-bar $'\e[38;5;82m' col-bcmd $'\e[38;5;220m' col-profile $'\e[38;5;201m'
col-bspc $'\b' col-bapo $'\e[1;39;5;220m' col-keyword $'\e[32m'
col-b-lhi $'\e[1m\e[38;5;27m' col-lhi $'\e[38;5;33m' col-slight $'\e[38;5;230m'
col-b-warn $'\e[1;38;5;214m' col-msg $'\e[0m' col-st $'\e[9m'
col-cmd $'\e[38;5;82m' col-msg2 $'\e[38;5;172m' col-tab $' \t '
col-data $'\e[38;5;82m' col-msg3 $'\e[38;5;238m' col-term $'\e[38;5;190m'
col-data2 $'\e[38;5;39m' col-meta $'\e[38;5;50m' col-th-bar $'\e[38;5;82m'
col-dir $'\e[3;38;5;135m' col-meta2 $'\e[38;5;135m' col-txt $'\e[38;5;254m'
col-ehi $'\e[1m\e[38;5;210m' col-nb $'\e[22m' col-u $'\e[4m'
col-error $'\e[1m\e[38;5;204m' col-nit $'\e[23m' col-uname $'\e[1;4m\e[35m'
col-failure $'\e[38;5;204m' col-nl $'\n' col-uninst $'\e[38;5;118m'
col-faint $'\e[38;5;240m' col-note $'\e[38;5;148m' col-url $'\e[38;5;33m'
col-file $'\e[3;38;5;39m' col-nst $'\e[29m' col-u-warn $'\e[4;38;5;214m'
col-func $'\e[38;5;135m' col-nu $'\e[24m' col-var $'\e[38;5;39m'
col-flag $'\e[1;3;38;5;79m' col-it $'\e[3m' col-quo $'\e[1;38;5;33m'
col-glob $'\e[38;5;226m' col-num $'\e[3;38;5;154m' col-version $'\e[3;38;5;46m'
col-happy $'\e[1m\e[38;5;82m' col-obj $'\e[38;5;218m' col-warn $'\e[38;5;214m'
col-hi $'\e[1m\e[38;5;165m' col-obj2 $'\e[38;5;118m' col-dbg $'\e[90m'
col-ice $'\e[38;5;39m' col-ok $'\e[38;5;220m' col-rst $'\e[0m'
col-id-as $'\e[4;38;5;220m' col-opt $'\e[38;5;82m' col-time $'\e[38;5;220m'
col-quos $'\e[1;38;5;160m'
col-mdsh $'\e[1;38;5;220m'"${${${(M)LANG:#*UTF-8*}:+–}:--}"$'\e[0m'
col-mmdsh $'\e[1;38;5;220m'"${${${(M)LANG:#*UTF-8*}:+――}:--}"$'\e[0m'
col-↔ ${${${(M)LANG:#*UTF-8*}:+$'\e[38;5;82m↔\e[0m'}:-$'\e[38;5;82m«-»\e[0m'}
col-… "${${${(M)LANG:#*UTF-8*}:+…}:-...}" col-ndsh "${${${(M)LANG:#*UTF-8*}:+–}:-}"
col--… "${${${(M)LANG:#*UTF-8*}:+⋯⋯}:-···}" col-lr "${${${(M)LANG:#*UTF-8*}:+↔}:-"«-»"}"
)
if [[ ( ${+terminfo} -eq 1 && ${terminfo[colors]} -ge 256 ) || ( ${+termcap} -eq 1 && ${termcap[Co]} -ge 256 ) ]] {
ZI+=( col-pname $'\e[1;4m\e[38;5;39m' col-uname $'\e[1;4m\e[38;5;207m' )
}
}
# List of hooks.
typeset -gAH ZI_ZLE_HOOKS_LIST
ZI_ZLE_HOOKS_LIST=(
zle-isearch-exit 1
zle-isearch-update 1
zle-line-pre-redraw 1
zle-line-init 1
zle-line-finish 1
zle-history-line-set 1
zle-keymap-select 1
paste-insert 1
)
builtin setopt noaliases
# ]]]
#
# Temporary substituting of functions-related functions.
#
# FUNCTION: :zi-reload-and-run. [[[
# Marks given function ($3) for autoloading, and executes it triggering the load.
# $1 is the fpath dedicated to the function, $2 are autoload options. This function replaces "autoload -X",
# because using that on older Zsh versions causes problems with traps.
#
# So basically one creates function stub that calls :zi-reload-and-run() instead of "autoload -X".
#
# $1 - FPATH dedicated to function
# $2 - autoload options
# $3 - function name (one that needs autoloading)
#
# Author: Bart Schaefer
:zi-reload-and-run () {
local fpath_prefix="$1" autoload_opts="$2" func="$3"
shift 3
# Unfunction caller function (its name is given).
unfunction -- "$func"
local -a ___fpath
___fpath=( ${fpath[@]} )
local -a +h fpath
[[ $FPATH != *${${(@0)fpath_prefix}[1]}* ]] && fpath=( ${(@0)fpath_prefix} ${___fpath[@]} )
# After this the function exists again.
builtin autoload ${(s: :)autoload_opts} -- "$func"
# User wanted to call the function, not only load it.
"$func" "$@"
} # ]]]
# FUNCTION: :zi-tmp-subst-autoload. [[[
# Function defined to hijack plugin's calls to the `autoload' builtin.
#
# The hijacking is not only to gather report data, but also to.
# run custom `autoload' function, that doesn't need FPATH.
:zi-tmp-subst-autoload () {
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
builtin setopt extendedglob warncreateglobal typesetsilent rcquotes
local -a opts opts2 custom reply
local func
zparseopts -D -E -M -a opts ${(s::):-RTUXdkmrtWzwC} I+=opts2 S+:=custom
builtin set -- ${@:#--}
# Process the id-as''/teleid'' to get the plugin dir.
.zi-any-to-user-plugin $ZI[CUR_USPL2]
[[ $reply[1] = % ]] && local PLUGIN_DIR="$reply[2]" || \
local PLUGIN_DIR="$ZI[PLUGINS_DIR]/${reply[1]:+$reply[1]---}${reply[2]//\//---}"
# "Fpath elements" - ie those elements that are inside the plug-in directory.
# The name comes from the fact that they are the selected fpath elements → so just "items".
local -a fpath_elements
fpath_elements=( ${fpath[(r)$PLUGIN_DIR/*]} )
# Add a function subdirectory to items, if any (this action is according to the Plug Standard version 1.07 and later).
[[ -d $PLUGIN_DIR/functions ]] && fpath_elements+=( "$PLUGIN_DIR"/functions )
if (( ${+opts[(r)-X]} )); then
.zi-add-report "${ZI[CUR_USPL2]}" "Warning: Failed autoload ${(j: :)opts[@]} $*"
+zi-message -u2 "{error}builtin autoload required for {obj}${(j: :)opts[@]}{error} option(s)"
return 1
fi
if (( ${+opts[(r)-w]} )); then
.zi-add-report "${ZI[CUR_USPL2]}" "-w-Autoload ${(j: :)opts[@]} ${(j: :)@}"
fpath+=( $PLUGIN_DIR )
builtin autoload ${opts[@]} "$@"
return $?
fi
if [[ -n ${(M)@:#+X} ]]; then
.zi-add-report "${ZI[CUR_USPL2]}" "Autoload +X ${opts:+${(j: :)opts[@]} }${(j: :)${@:#+X}}"
local +h FPATH=$PLUGINS_DIR${fpath_elements:+:${(j.:.)fpath_elements[@]}}:$FPATH
local +h -a fpath
fpath=( $PLUGIN_DIR $fpath_elements $fpath )
builtin autoload +X ${opts[@]} "${@:#+X}"
return $?
fi
for func; do
.zi-add-report "${ZI[CUR_USPL2]}" "Autoload $func${opts:+ with options ${(j: :)opts[@]}}"
done
integer count retval
for func; do
# Real autoload doesn't touch function if it already exists.
# Author of the idea of FPATH-clean autoloading: Bart Schaefer.
if (( ${+functions[$func]} != 1 )) {
builtin setopt noaliases
if [[ $func == /* ]] && is-at-least 5.4; then
builtin autoload ${opts[@]} $func
return $?
elif [[ $func == /* ]]; then
if [[ $ZI[MUTE_WARNINGS] != (1|true|on|yes) && -z $ZI[WARN_SHOWN_FOR_$ZI[CUR_USPL2]] ]]; then
+zi-message "{u-warn}Warning{b-warn}: {rst}the plugin {pid}$ZI[CUR_USPL2]" \
"{rst}is using autoload functions specified by their absolute path," \
"which is not supported by this Zsh version ({↔} {version}$ZSH_VERSION{rst}," \
"required is Zsh >= {version}5.4{rst})." "{nl}A fallback mechanism has been applied, which works well only" \
"for functions in the plugin {u}{slight}main{rst} directory." "{nl}(To mute this message, set" \
"{var}\$ZI[MUTE_WARNINGS]{rst} to a truth value.)"
ZI[WARN_SHOWN_FOR_$ZI[CUR_USPL2]]=1
fi
# Apply workaround
func=$func:t
fi
if [[ ${ZI[NEW_AUTOLOAD]} = 2 ]]; then
builtin autoload ${opts[@]} "$PLUGIN_DIR/$func"
retval=$?
elif [[ ${ZI[NEW_AUTOLOAD]} = 1 ]]; then
if (( ${+opts[(r)-C]} )) {
local pth nl=$'\n' sel=""
for pth ( $PLUGIN_DIR $fpath_elements $fpath ) {
[[ -f $pth/$func ]] && { sel=$pth; break; }
}
if [[ -z $sel ]] {
+zi-message "{u-warn}zi{b-warn}:{error} Couldn't find autoload function{ehi}:" \
"{apo}\`{file}${func}{apo}\`{error} anywhere in {var}\$fpath{error}."
retval=1
} else {
eval "function ${(q)${custom[++count*2]}:-$func} {
local body=\"\$(<${(qqq)sel}/${(qqq)func})\" body2
() { builtin setopt localoptions extendedglob
body2=\"\${body##[[:space:]]#${func}[[:blank:]]#\(\)[[:space:]]#\{}\"
[[ \$body2 != \$body ]] && body2=\"\${body2%\}[[:space:]]#([$nl]#([[:blank:]]#\#[^$nl]#((#e)|[$nl]))#)#}\"
}
functions[${${(q)custom[count*2]}:-$func}]=\"\$body2\"
${(q)${custom[count*2]}:-$func} \"\$@\"
}"
retval=$?
}
} else {
eval "function ${(q)func} {
local -a fpath
fpath=( ${(qqq)PLUGIN_DIR} ${(qqq@)fpath_elements} ${(qqq@)fpath} )
builtin autoload -X ${(j: :)${(q-)opts[@]}}
}"
retval=$?
}
else
eval "function ${(q)func} {
:zi-reload-and-run ${(qqq)PLUGIN_DIR}"$'\0'"${(pj,\0,)${(qqq)fpath_elements[@]}} ${(qq)opts[*]} ${(q)func} "'"$@"
}'
retval=$?
fi
(( ZI[ALIASES_OPT] )) && builtin setopt aliases
}
if (( ${+opts2[(r)-I]} )) {
${custom[count*2]:-$func}
retval=$?
}
done
return $retval
} # ]]]
# FUNCTION: :zi-tmp-subst-bindkey. [[[
# Function defined to hijack plugin's calls to the `bindkey' builtin.
#
# The hijacking is to gather report data (which is used in unload).
:zi-tmp-subst-bindkey() {
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
builtin setopt extendedglob warncreateglobal typesetsilent noshortloops
is-at-least 5.3 && .zi-add-report "${ZI[CUR_USPL2]}" "Bindkey ${(j: :)${(q+)@}}" || .zi-add-report "${ZI[CUR_USPL2]}" "Bindkey ${(j: :)${(q)@}}"
# 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 opts
zparseopts -A opts -D ${(s::):-lLdDAmrsevaR} M: N:
if (( ${#opts} == 0 || ( ${#opts} == 1 && ${+opts[-M]} ) || ( ${#opts} == 1 && ${+opts[-R]} ) || ( ${#opts} == 1 && ${+opts[-s]} ) || ( ${#opts} <= 2 && ${+opts[-M]} && ${+opts[-s]} ) || ( ${#opts} <= 2 && ${+opts[-M]} && ${+opts[-R]} ) )); then
local string="${(q)1}" widget="${(q)2}"
local quoted
if [[ -n ${ICE[bindmap]} && ${ZI_CUR_BIND_MAP[empty]} -eq 1 ]]; then
local -a pairs
pairs=( "${(@s,;,)ICE[bindmap]}" )
if [[ -n ${(M)pairs:#*\\(#e)} ]] {
local prev
pairs=( ${pairs[@]//(#b)((*)\\(#e)|(*))/${match[3]:+${prev:+$prev\;}}${match[3]}${${prev::=${match[2]:+${prev:+$prev\;}}${match[2]}}:+}} )
}
pairs=( "${(@)${(@)${(@s:->:)pairs}##[[:space:]]##}%%[[:space:]]##}" )
ZI_CUR_BIND_MAP=( empty 0 )
(( ${#pairs} > 1 && ${#pairs[@]} % 2 == 0 )) && ZI_CUR_BIND_MAP+=( "${pairs[@]}" )
fi
local bmap_val="${ZI_CUR_BIND_MAP[${1}]}"
if (( !ZI_CUR_BIND_MAP[empty] )) {
[[ -z $bmap_val ]] && bmap_val="${ZI_CUR_BIND_MAP[${(qqq)1}]}"
[[ -z $bmap_val ]] && bmap_val="${ZI_CUR_BIND_MAP[${(qqq)${(Q)1}}]}"
[[ -z $bmap_val ]] && { bmap_val="${ZI_CUR_BIND_MAP[!${(qqq)1}]}"; integer val=1; }
[[ -z $bmap_val ]] && bmap_val="${ZI_CUR_BIND_MAP[!${(qqq)${(Q)1}}]}"
}
if [[ -n $bmap_val ]]; then
string="${(q)bmap_val}"
if (( val )) {
[[ ${pos[1]} = "-M" ]] && pos[4]="$bmap_val" || pos[2]="$bmap_val"
} else {
[[ ${pos[1]} = "-M" ]] && pos[3]="${(Q)bmap_val}" || pos[1]="${(Q)bmap_val}"
}
.zi-add-report "${ZI[CUR_USPL2]}" ":::Bindkey: combination <$1> changed to <$bmap_val>${${(M)bmap_val:#hold}:+, i.e. ${ZI[col-error]}unmapped${ZI[col-rst]}}"
((1))
elif [[ ( -n ${bmap_val::=${ZI_CUR_BIND_MAP[UPAR]}} && -n ${${ZI[UPAR]}[(r);:${(q)1};:]} ) || \
( -n ${bmap_val::=${ZI_CUR_BIND_MAP[DOWNAR]}} && -n ${${ZI[DOWNAR]}[(r);:${(q)1};:]} ) || \
( -n ${bmap_val::=${ZI_CUR_BIND_MAP[RIGHTAR]}} && -n ${${ZI[RIGHTAR]}[(r);:${(q)1};:]} ) || \
( -n ${bmap_val::=${ZI_CUR_BIND_MAP[LEFTAR]}} && -n ${${ZI[LEFTAR]}[(r);:${(q)1};:]} )
]]; then
string="${(q)bmap_val}"
if (( val )) {
[[ ${pos[1]} = "-M" ]] && pos[4]="$bmap_val" || pos[2]="$bmap_val"
} else {
[[ ${pos[1]} = "-M" ]] && pos[3]="${(Q)bmap_val}" || pos[1]="${(Q)bmap_val}"
}
.zi-add-report "${ZI[CUR_USPL2]}" ":::Bindkey: combination <$1> recognized as cursor-key and changed to <${bmap_val}>${${(M)bmap_val:#hold}:+, i.e. ${ZI[col-error]}unmapped${ZI[col-rst]}}"
fi
[[ $bmap_val = hold ]] && return 0
local prev="${(q)${(s: :)$(builtin bindkey ${(Q)string})}[-1]#undefined-key}"
# "-M map" given?
if (( ${+opts[-M]} )); then
local Mopt=-M
local Marg="${opts[-M]}"
Mopt="${(q)Mopt}"
Marg="${(q)Marg}"
quoted="$string $widget $prev $Mopt $Marg"
else
quoted="$string $widget $prev"
fi
# -R given?
if (( ${+opts[-R]} )); then
local Ropt=-R
Ropt="${(q)Ropt}"
if (( ${+opts[-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 ${ZI[CUR_USPL2]} ]] && ZI[BINDKEYS__${ZI[CUR_USPL2]}]+="$quoted "
# Remember for dtrace.
[[ ${ZI[DTRACE]} = 1 ]] && ZI[BINDKEYS___dtrace/_dtrace]+="$quoted "
else
# bindkey -A newkeymap main?
# Negative indices for KSH_ARRAYS immunity.
if [[ ${#opts} -eq 1 && ${+opts[-A]} = 1 && ${#pos} = 3 && ${pos[-1]} = main && ${pos[-2]} != -A ]]; then
# Save a copy of main keymap.
(( ZI[BINDKEY_MAIN_IDX] = ${ZI[BINDKEY_MAIN_IDX]:-0} + 1 ))
local pname="${ZI[CUR_PLUGIN]:-_dtrace}"
local name="${(q)pname}-main-${ZI[BINDKEY_MAIN_IDX]}"
builtin bindkey -N "$name" main
# Remember occurrence of main keymap substitution, to revert on unload.
local keys=_ widget=_ prev= optA=-A mapname="${name}" optR=_
local quoted="${(q)keys} ${(q)widget} ${(q)prev} ${(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 ${ZI[CUR_USPL2]} ]] && ZI[BINDKEYS__${ZI[CUR_USPL2]}]+="$quoted "
[[ ${ZI[DTRACE]} = 1 ]] && ZI[BINDKEYS___dtrace/_dtrace]+="$quoted "
.zi-add-report "${ZI[CUR_USPL2]}" "Warning: keymap \`main' copied to \`${name}' because of \`${pos[-2]}' substitution"
# bindkey -N newkeymap [other].
elif [[ ${#opts} -eq 1 && ${+opts[-N]} = 1 ]]; then
local Nopt=-N
local Narg="${opts[-N]}"
local keys=_ widget=_ prev= optN=-N mapname="${Narg}" optR=_
local quoted="${(q)keys} ${(q)widget} ${(q)prev} ${(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 ${ZI[CUR_USPL2]} ]] && ZI[BINDKEYS__${ZI[CUR_USPL2]}]+="$quoted "
[[ ${ZI[DTRACE]} = 1 ]] && ZI[BINDKEYS___dtrace/_dtrace]+="$quoted "
else
.zi-add-report "${ZI[CUR_USPL2]}" "Warning: last bindkey used non-typical options: ${(kv)opts[*]}"
fi
fi
# Actual bindkey.
builtin bindkey "${pos[@]}"
return $? # testable
} # ]]]
# FUNCTION: :zi-tmp-subst-zstyle. [[[
# Function defined to hijack plugin's calls to the `zstyle' builtin.
#
# The hijacking is to gather report data (which is used in unload).
:zi-tmp-subst-zstyle() {
builtin setopt localoptions noerrreturn noerrexit extendedglob nowarncreateglobal typesetsilent noshortloops unset
.zi-add-report "${ZI[CUR_USPL2]}" "Zstyle $*"
# Remember in order 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]} = 1 ) ]]; 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 ${ZI[CUR_USPL2]} ]] && ZI[ZSTYLES__${ZI[CUR_USPL2]}]+="$ps "
# Remember for dtrace.
[[ ${ZI[DTRACE]} = 1 ]] && ZI[ZSTYLES___dtrace/_dtrace]+=$ps
else
if [[ ! ${#opts[@]} = 1 && ( ${+opts[(r)-s]} = 1 || ${+opts[(r)-b]} = 1 || ${+opts[(r)-a]} = 1 || ${+opts[(r)-t]} = 1 || ${+opts[(r)-T]} = 1 || ${+opts[(r)-m]} = 1 ) ]]; then
.zi-add-report "${ZI[CUR_USPL2]}" "Warning: last zstyle used non-typical options: ${opts[*]}"
fi
fi
# Actual zstyle.
builtin zstyle "${pos[@]}"
return $? # testable
} # ]]]
# FUNCTION: :zi-tmp-subst-alias. [[[
# Function defined to hijack plugin's calls to the `alias' builtin.
#
# The hijacking is to gather report data (which is used in unload).
:zi-tmp-subst-alias() {
builtin setopt localoptions noerrreturn noerrexit extendedglob warncreateglobal typesetsilent noshortloops unset
.zi-add-report "${ZI[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]} )) && .zi-add-report "${ZI[CUR_USPL2]}" "Warning: redefining alias \`${aname}', previous value: ${aliases[$aname]}"
local bname=${(q)aliases[$aname]}
aname="${(q)aname}"
if (( ${+opts[(r)-s]} )); then
tmp=-s
tmp="${(q)tmp}"
quoted="$aname $bname $tmp"
elif (( ${+opts[(r)-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 ${ZI[CUR_USPL2]} ]] && ZI[ALIASES__${ZI[CUR_USPL2]}]+="$quoted "
# Remember for dtrace.
[[ ${ZI[DTRACE]} = 1 ]] && ZI[ALIASES___dtrace/_dtrace]+="$quoted "
done
# Actual alias.
builtin alias "${pos[@]}"
return $? # testable
} # ]]]
# FUNCTION: :zi-tmp-subst-zle. [[[.
# Function defined to hijack plugin's calls to the `zle' builtin.
#
# The hijacking is to gather report data (which is used in unload).
:zi-tmp-subst-zle() {
builtin setopt localoptions noerrreturn noerrexit extendedglob warncreateglobal typesetsilent noshortloops unset
.zi-add-report "${ZI[CUR_USPL2]}" "Zle $*"
# Remember to perform the actual zle call.
typeset -a pos
pos=( "$@" )
builtin set -- "${@:#--}"
# Try to catch game-changing "-N".
if [[ ( $1 = -N && ( $# = 2 || $# = 3 ) ) || ( $1 = -C && $# = 4 ) ]]; then
# Hooks.
if [[ ${ZI_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 ${ZI[CUR_USPL2]} ]] && ZI[WIDGETS_DELETE__${ZI[CUR_USPL2]}]+="$quoted "
# Remember for dtrace.
[[ ${ZI[DTRACE]} = 1 ]] && ZI[WIDGETS_DELETE___dtrace/_dtrace]+="$quoted "
# These will be saved and restored.
elif (( ${+widgets[$2]} )); then
# Have to remember original widget "$2" and the copy that it's going to be done.
local widname="$2" targetfun="${${${(M)1:#-C}:+$4}:-$3}"
local completion_widget="${${(M)1:#-C}:+$3}"
local saved_widcontents="${widgets[$widname]}"
widname="${(q)widname}"
completion_widget="${(q)completion_widget}"
targetfun="${(q)targetfun}"
saved_widcontents="${(q)saved_widcontents}"
local quoted="$1 $widname $completion_widget $targetfun $saved_widcontents"
quoted="${(q)quoted}"
# Remember only when load is in progress (it can be dstart that leads execution here).
[[ -n ${ZI[CUR_USPL2]} ]] && ZI[WIDGETS_SAVED__${ZI[CUR_USPL2]}]+="$quoted "
# Remember for dtrace.
[[ ${ZI[DTRACE]} = 1 ]] && ZI[WIDGETS_SAVED___dtrace/_dtrace]+="$quoted "
# These will be deleted.
else
.zi-add-report "${ZI[CUR_USPL2]}" "Note: a new widget created via zle -N: \`$2'"
local quoted="$2"
quoted="${(q)quoted}"
# Remember only when load is in progress (it can be dstart that leads execution here).
[[ -n ${ZI[CUR_USPL2]} ]] && ZI[WIDGETS_DELETE__${ZI[CUR_USPL2]}]+="$quoted "
# Remember for dtrace.
[[ ${ZI[DTRACE]} = 1 ]] && ZI[WIDGETS_DELETE___dtrace/_dtrace]+="$quoted "
fi
fi
# Actual zle.
builtin zle "${pos[@]}"
return $? # testable
} # ]]]
# FUNCTION: :zi-tmp-subst-compdef. [[[
# Function defined to hijack plugin's calls to the `compdef' function.
# The hijacking is not only for reporting, but also to save compdef
# calls so that `compinit' can be called after loading plugins.
:zi-tmp-subst-compdef() {
builtin setopt localoptions noerrreturn noerrexit extendedglob warncreateglobal typesetsilent noshortloops unset
.zi-add-report "${ZI[CUR_USPL2]}" "Saving \`compdef $*' for replay"
ZI_COMPDEF_REPLAY+=( "${(j: :)${(q)@}}" )
return 0 # testable
} # ]]]
# FUNCTION: .zi-tmp-subst-on. [[[
# Turn on temporary substituting of functions of builtins and functions according to passed
# mode ("load", "light", "light-b" or "compdef"). The temporary substituting of functions is
# to gather report data, and to hijack `autoload', `bindkey' and `compdef' calls.
.zi-tmp-subst-on() {
local mode="$1"
# Enable temporary substituting of functions only once.
#
# One could expect possibility of widening of temporary substituting of functions, 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).
[[ ${ZI[TMP_SUBST]} != inactive ]] && builtin return 0
ZI[TMP_SUBST]="$mode"
# The point about backups is: does the key exist in functions array.
# If it does exist, then it will also exist as ZI[bkp-*]. Defensive code, shouldn't be needed.
builtin unset "ZI[bkp-autoload]" "ZI[bkp-compdef]" # 0, E.
if [[ $mode != compdef ]]; then
# 0. Used, but not in temporary restoration, which doesn't happen for autoload.
(( ${+functions[autoload]} )) && ZI[bkp-autoload]="${functions[autoload]}"
functions[autoload]=':zi-tmp-subst-autoload "$@";'
fi
# E. Always shade compdef.
(( ${+functions[compdef]} )) && ZI[bkp-compdef]="${functions[compdef]}"
functions[compdef]=':zi-tmp-subst-compdef "$@";'
# Temporarily replace `source' if subst'' given.
if [[ -n ${ICE[subst]} ]] {
(( ${+functions[source]} )) && ZI[bkp-source]="${functions[source]}"
(( ${+functions[.]} )) && ZI[bkp-.]="${functions[.]}"
(( ${+functions[.zi-service]} )) || builtin source "${ZI[BIN_DIR]}/lib/zsh/additional.zsh"
functions[source]=':zi-tmp-subst-source "$@";'
functions[.]=':zi-tmp-subst-source "$@";'
}
# Light and compdef temporary substituting of functions stops here. Dtrace and load go on.
[[ ( $mode = light && ${+ICE[trackbinds]} -eq 0 ) || $mode = compdef ]] && return 0
# Defensive code, shouldn't be needed. A, B, C, D.
builtin unset "ZI[bkp-bindkey]" "ZI[bkp-zstyle]" "ZI[bkp-alias]" "ZI[bkp-zle]"
# A.
(( ${+functions[bindkey]} )) && ZI[bkp-bindkey]="${functions[bindkey]}"
functions[bindkey]=':zi-tmp-subst-bindkey "$@";'
# B, when `zi light -b ...' or when `zi ice trackbinds ...; zi light ...'.
[[ $mode = light-b || ( $mode = light && ${+ICE[trackbinds]} -eq 1 ) ]] && return 0
# B.
(( ${+functions[zstyle]} )) && ZI[bkp-zstyle]="${functions[zstyle]}"
functions[zstyle]=':zi-tmp-subst-zstyle "$@";'
# C.
(( ${+functions[alias]} )) && ZI[bkp-alias]="${functions[alias]}"
functions[alias]=':zi-tmp-subst-alias "$@";'
# D.
(( ${+functions[zle]} )) && ZI[bkp-zle]="${functions[zle]}"
functions[zle]=':zi-tmp-subst-zle "$@";'
builtin return 0
} # ]]]
# FUNCTION: .zi-tmp-subst-off. [[[
# Turn off temporary substituting of functions completely for a given mode ("load", "light",
# "light-b" (i.e. the `trackbinds' mode) or "compdef").
.zi-tmp-subst-off() {
builtin setopt localoptions noerrreturn noerrexit extendedglob warncreateglobal typesetsilent noshortloops unset noaliases
local mode="$1"
# Disable temporary substituting of functions only once.
# Disable temporary substituting of functions only the way it was enabled first.
[[ ${ZI[TMP_SUBST]} = inactive || ${ZI[TMP_SUBST]} != $mode ]] && return 0
ZI[TMP_SUBST]=inactive
if [[ $mode != compdef ]]; then
# 0. Unfunction autoload.
(( ${+ZI[bkp-autoload]} )) && functions[autoload]="${ZI[bkp-autoload]}" || unfunction autoload
fi
# E. Restore original compdef if it existed.
(( ${+ZI[bkp-compdef]} )) && functions[compdef]="${ZI[bkp-compdef]}" || unfunction compdef
# Restore the possible source function.
(( ${+ZI[bkp-source]} )) && functions[source]="${ZI[bkp-source]}" || unfunction source 2>/dev/null
(( ${+ZI[bkp-.]} )) && functions[.]="${ZI[bkp-.]}" || unfunction . 2> /dev/null
# Light and compdef temporary substituting of functions stops here.
[[ ( $mode = light && ${+ICE[trackbinds]} -eq 0 ) || $mode = compdef ]] && return 0
# Unfunction temporary substituting of functions functions.
# A.
(( ${+ZI[bkp-bindkey]} )) && functions[bindkey]="${ZI[bkp-bindkey]}" || unfunction bindkey
# When `zi light -b ...' or when `zi ice trackbinds ...; zi light ...'.
[[ $mode = light-b || ( $mode = light && ${+ICE[trackbinds]} -eq 1 ) ]] && return 0
# B.
(( ${+ZI[bkp-zstyle]} )) && functions[zstyle]="${ZI[bkp-zstyle]}" || unfunction zstyle
# C.
(( ${+ZI[bkp-alias]} )) && functions[alias]="${ZI[bkp-alias]}" || unfunction alias
# D.
(( ${+ZI[bkp-zle]} )) && functions[zle]="${ZI[bkp-zle]}" || unfunction zle
return 0
} # ]]]
# FUNCTION: pmodload. [[[
# {function:pmodload} Compatibility with Prezto. Calls can be recursive.
(( ${+functions[pmodload]} )) || pmodload() {
local -A ices
(( ${+ICE} )) && ices=( "${(kv)ICE[@]}" teleid '' )
local -A ICE ZI_ICE
ICE=( "${(kv)ices[@]}" ) ZI_ICE=( "${(kv)ices[@]}" )
while (( $# )); do
ICE[teleid]="PZT::modules/$1${ICE[svn]-/init.zsh}"
ZI_ICE[teleid]="PZT::modules/$1${ICE[svn]-/init.zsh}"
if zstyle -t ":prezto:module:$1" loaded 'yes' 'no'; then
shift
continue
else
[[ -z ${ZI_SNIPPETS[PZT::modules/$1${ICE[svn]-/init.zsh}]} && -z ${ZI_SNIPPETS[https://github.com/sorin-ionescu/prezto/trunk/modules/$1${ICE[svn]-/init.zsh}]} ]] && .zi-load-snippet PZT::modules/"$1${ICE[svn]-/init.zsh}"
shift
fi
done
} # ]]]
#
# Diff functions.
#
# FUNCTION: .zi-diff-functions. [[[
# Implements detection of newly created functions. Performs data gathering, computation is done in *-compute().
#
# $1 - user/plugin (i.e. uspl2 format)
# $2 - command, can be "begin" or "end"
.zi-diff-functions() {
local uspl2="$1"
local cmd="$2"
[[ $cmd = begin ]] && { [[ -z ${ZI[FUNCTIONS_BEFORE__$uspl2]} ]] && ZI[FUNCTIONS_BEFORE__$uspl2]="${(j: :)${(qk)functions[@]}}" } || ZI[FUNCTIONS_AFTER__$uspl2]+=" ${(j: :)${(qk)functions[@]}}"
} # ]]]
# FUNCTION: .zi-diff-options. [[[
# Implements detection of change in option state. Performs data gathering, computation is done in *-compute().
#
# $1 - user/plugin (i.e. uspl2 format)
# $2 - command, can be "begin" or "end"
.zi-diff-options() {
local IFS=" "
[[ $2 = begin ]] && { [[ -z ${ZI[OPTIONS_BEFORE__$uspl2]} ]] && ZI[OPTIONS_BEFORE__$1]="${(kv)options[@]}" } || ZI[OPTIONS_AFTER__$1]+=" ${(kv)options[@]}"
} # ]]]
# FUNCTION: .zi-diff-env. [[[
# Implements detection of change in PATH and FPATH.
#
# $1 - user/plugin (i.e. uspl2 format)
# $2 - command, can be "begin" or "end"
.zi-diff-env() {
typeset -a tmp
local IFS=" "
[[ $2 = begin ]] && {
{ [[ -z ${ZI[PATH_BEFORE__$uspl2]} ]] && tmp=( "${(q)path[@]}" )
ZI[PATH_BEFORE__$1]="${tmp[*]}"
}
{ [[ -z ${ZI[FPATH_BEFORE__$uspl2]} ]] && tmp=( "${(q)fpath[@]}" )
ZI[FPATH_BEFORE__$1]="${tmp[*]}"
}
} || {
tmp=( "${(q)path[@]}" )
ZI[PATH_AFTER__$1]+=" ${tmp[*]}"
tmp=( "${(q)fpath[@]}" )
ZI[FPATH_AFTER__$1]+=" ${tmp[*]}"
}
} # ]]]
# FUNCTION: .zi-diff-parameter. [[[
# Implements detection of change in any parameter's existence and type.
# Performs data gathering, computation is done in *-compute().
#
# $1 - user/plugin (i.e. uspl2 format)
# $2 - command, can be "begin" or "end"
.zi-diff-parameter() {
typeset -a tmp
[[ $2 = begin ]] && {
{ [[ -z ${ZI[PARAMETERS_BEFORE__$uspl2]} ]] && ZI[PARAMETERS_BEFORE__$1]="${(j: :)${(qkv)parameters[@]}}"; }
} || {
ZI[PARAMETERS_AFTER__$1]+=" ${(j: :)${(qkv)parameters[@]}}"
}
} # ]]]
# FUNCTION: .zi-diff. [[[
# Performs diff actions of all types
.zi-diff() {
.zi-diff-functions "$1" "$2"
.zi-diff-options "$1" "$2"
.zi-diff-env "$1" "$2"
.zi-diff-parameter "$1" "$2"
} # ]]]
#
# Utility functions.
#
# FUNCTION: .zi-get-mtime-into. [[[
.zi-get-mtime-into() {
if (( ZI[HAVE_ZSTAT] )) {
local -a arr
{ zstat +mtime -A arr "$1"; } 2>/dev/null
: ${(P)2::="${arr[1]}"}
} else {
{ : ${(P)2::="$(stat -c %Y "$1")"}; } 2>/dev/null
}
} # ]]]
# FUNCTION: .zi-any-to-user-plugin. [[[
# Allows elastic plugin-spec across the code.
#
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - plugin (only when $1 - i.e. user - given)
#
# Returns user and plugin in $reply.
#
.zi-any-to-user-plugin() {
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
builtin setopt extendedglob typesetsilent noshortloops rcquotes ${${${+reply}:#0}:+warncreateglobal}
# Two components given?
# That's a pretty fast track to call this function this way.
if [[ -n $2 ]]; then
2=${~2}
reply=( ${1:-${${(M)2#/}:+%}} ${${${(M)1#%}:+$2}:-${2//---//}} )
return 0
fi
# Is it absolute path?
if [[ $1 = /* ]]; then
reply=( % $1 )
return 0
fi
# Is it absolute path in zi format?
if [[ $1 = %* ]]; then
local -A map
map=( ZPFX "$ZPFX" HOME $HOME SNIPPETS $ZI[SNIPPETS_DIR] PLUGINS $ZI[PLUGINS_DIR] )
reply=( % ${${1/(#b)(#s)%(${(~j:|:)${(@k)map}}|)/$map[$match[1]]}} )
reply[2]=${~reply[2]}
return 0
fi
# Rest is for single component given.
# It doesn't touch $2
1=${1//---//}
if [[ $1 = */* ]]; then
reply=( ${1%%/*} ${1#*/} )
return 0
fi
reply=( "" "${1:-_unknown}" )
return 0
} # ]]]
# FUNCTION: .zi-any-to-pid. [[[
.zi-any-to-pid() {
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
builtin setopt extendedglob typesetsilent noshortloops rcquotes ${${${+REPLY}:#0}:+warncreateglobal}
1=${~1} 2=${~2}
# Two components given?
if [[ -n $2 ]] {
if [[ $1 == (%|/)* || ( -z $1 && $2 == /* ) ]] {
.zi-util-shands-path $1${${(M)1#(%/?|%[^/]|/?)}:+/}$2
REPLY=${${REPLY:#%*}:+%}$REPLY
} else {
REPLY=$1${1:+/}$2
}
return 0
}
# Is it absolute path?
if [[ $1 = (%|/|\~)* ]] {
.zi-util-shands-path $1
REPLY=${${REPLY:#%*}:+%}$REPLY
return 0
}
# Single component given.
REPLY=${1//---//}
return 0
} # ]]]
# FUNCTION: .zi-util-shands-path. [[[
# Replaces parts of path with %HOME, etc.
.zi-util-shands-path() {
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
builtin setopt extendedglob typesetsilent noshortloops rcquotes ${${${+REPLY}:#0}:+warncreateglobal}
local -A map
map=( \~ %HOME $HOME %HOME $ZI[SNIPPETS_DIR] %SNIPPETS $ZI[PLUGINS_DIR] %PLUGINS
"$ZPFX" %ZPFX HOME %HOME SNIPPETS %SNIPPETS PLUGINS %PLUGINS "" "" )
REPLY=${${1/(#b)(#s)(%|)(${(~j:|:)${(@k)map:#$HOME}}|$HOME|)/$map[$match[2]]}}
return 0
} # ]]]
# FUNCTION: .zi-find-other-matches. [[[
# Plugin's main source file is in general `name.plugin.zsh'. However,
# there can be different conventions, if that file is not found, then
# this functions examines other conventions in the most sane order.
.zi-find-other-matches() {
local pdir_path="$1" pbase="$2"
if [[ -e $pdir_path/init.zsh ]] {
reply=( "$pdir_path"/init.zsh )
} elif [[ -e $pdir_path/$pbase.zsh-theme ]] {
reply=( "$pdir_path/$pbase".zsh-theme )
} elif [[ -e $pdir_path/$pbase.theme.zsh ]] {
reply=( "$pdir_path/$pbase".theme.zsh )
} else {
reply=(
"$pdir_path"/*.plugin.zsh(DN) "$pdir_path"/*.zsh-theme(DN) "$pdir_path"/*.lib.zsh(DN)
"$pdir_path"/*.zsh(DN) "$pdir_path"/*.sh(DN) "$pdir_path"/.zshrc(DN)
)
}
reply=( "${(u)reply[@]}" )
return $(( ${#reply} > 0 ? 0 : 1 ))
} # ]]]
# FUNCTION: .zi-register-plugin. [[[
# Adds the plugin to ZI_REGISTERED_PLUGINS array and to the
# zsh_loaded_plugins array (managed according to the plugin standard: