Skip to content

Commit 5a28edb

Browse files
committed
feat: add %SYMBOL% substituting to non-clone time ices like atload, etc
There are many such symbol var-like symbols: - %ID% – id-as ice - %USER% – username (in user/plugin ID) - %PLUGIN% – plugin name (in user/plugin ID) - %URL% – snippet url - %DIR% – plugin directory path - %ZPFX% – value of $ZPFX - %OS% – `$OSTYPE` - %MACH% – `$MACHTYPE` - %CPU% – `$CPUTYPE` - %VENDOR% – `$VENDOR` - %HOST% – `$HOST` - %UID% – `$UID` (numerical user id) - %GID% – `$GID` (group #) With this patch many has been fixed (like %ID%, %USER%, %PLUGIN% were returning empty strings, possibly breaking packages, where this undocumented feature is used) and support for load-time ices (like `atload''`, etc.) have been added. For example: ```zsh zinit id-as'plugin-%UID%' atload'print Loaded from dir: %DIR%, plugin id: %ID%' for zdharma-continuum/null ``` output is: ``` Loaded from dir: /home/q/.local/share/zinit/plugins/plugin-500, plugin id: plugin-500 ```
1 parent 52e112c commit 5a28edb

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

β€ŽREADME.md

+29
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,35 @@ Also, two articles on the Wiki present an example setup
675675
[here](https://zdharma-continuum.github.io/zinit/wiki/Example-Oh-My-Zsh-setup/).
676676

677677
# How to Use<a name="how-to-use"></a>
678+
## Special variable-like strings `%NAME%`
679+
680+
You can use a set of special var-like strings in any of the ices (currently: `atinit`, `atload`, `atclone`, `atpull`, `mv`, `cp`, `make`, `configure`, and a few others):
681+
682+
- %ID% – id-as ice
683+
- %USER% – username (in user/plugin ID)
684+
- %PLUGIN% – plugin name (in user/plugin ID)
685+
- %URL% – snippet url
686+
- %DIR% – plugin directory path
687+
- %ZPFX% – value of $ZPFX
688+
- %OS% – `$OSTYPE`
689+
- %MACH% – `$MACHTYPE`
690+
- %CPU% – `$CPUTYPE`
691+
- %VENDOR% – `$VENDOR`
692+
- %HOST% – `$HOST`
693+
- %UID% – `$UID` (numerical user id)
694+
- %GID% – `$GID` (group #)
695+
696+
For example:
697+
698+
```zsh
699+
zinit id-as'plugin-%UID%' atload'print Loaded from dir: %DIR%, plugin id: %ID%' for zdharma-continuum/null
700+
```
701+
702+
output is:
703+
704+
```
705+
Loaded from dir: /home/q/.local/share/zinit/plugins/plugin-500, plugin id: plugin-500
706+
```
678707

679708
## Ice Modifiers<a name="ice-modifiers"></a>
680709

β€Žzinit.zsh

+26-12
Original file line numberDiff line numberDiff line change
@@ -1169,11 +1169,12 @@ builtin setopt noaliases
11691169

11701170
local -A ___subst_map
11711171
___subst_map=(
1172-
"%ID%" "${id_as_clean:-$id_as}"
1173-
"%USER%" "$user"
1174-
"%PLUGIN%" "${plugin:-$save_url}"
1172+
"%ID%" "${${${id_as_clean:-$id_as}:-$___id_as}:-$ICE[id-as]}"
1173+
"%USER%" "$___user"
1174+
"%PLUGIN%" "${___plugin:-$save_url}"
11751175
"%URL%" "${save_url:-${user:+$user/}$plugin}"
1176-
"%DIR%" "${local_path:-$local_dir${dirname:+/$dirname}}"
1176+
"%DIR%" "${${local_path:-$local_dir${dirname:+/$dirname}}:-$___pdir_path}"
1177+
'%ZPFX%' "$ZPFX"
11771178
'$ZPFX' "$ZPFX"
11781179
'${ZPFX}' "$ZPFX"
11791180
'%OS%' "${OSTYPE%(-gnu|[0-9]##)}" '%MACH%' "$MACHTYPE" '%CPU%' "$CPUTYPE"
@@ -1198,11 +1199,14 @@ builtin setopt noaliases
11981199
___add=( "${ICE[param]:+${(@Q)${(@z)ZINIT[PARAM_SUBST]}}}" )
11991200
(( ${#___add} % 2 == 0 )) && ___subst_map+=( "${___add[@]}" )
12001201

1201-
local ___var_name
1202-
for ___var_name; do
1203-
local ___value=${(P)___var_name}
1204-
___value=${___value//(#m)(%[a-zA-Z0-9]##%|\$ZPFX|\$\{ZPFX\})/${___subst_map[$MATCH]}}
1205-
: ${(P)___var_name::=$___value}
1202+
local ___cnt ___var_name
1203+
[[ $1 = <-> ]] && {___cnt=$1; shift;} || ___cnt=1
1204+
repeat $___cnt; do
1205+
for ___var_name; do
1206+
local ___value=${(P)___var_name}
1207+
___value=${___value//(#m)(%[a-zA-Z0-9]##%|\$ZPFX|\$\{ZPFX\})/${___subst_map[$MATCH]}}
1208+
: ${(P)___var_name::=$___value}
1209+
done
12061210
done
12071211
}
12081212
# ]]]
@@ -1405,8 +1409,6 @@ builtin setopt noaliases
14051409
.zinit-setup-params && local -x ${(Q)reply[@]}
14061410
}
14071411

1408-
.zinit-pack-ice "$id_as" ""
1409-
14101412
# Oh-My-Zsh, Prezto and manual shorthands.
14111413
[[ $url = *(${(~kj.|.)${(Mk)ZINIT_1MAP:#OMZ*}}|robbyrussell*oh-my-zsh|ohmyzsh/ohmyzsh)* ]] && local ZSH="${ZINIT[SNIPPETS_DIR]}"
14121414

@@ -1416,6 +1418,12 @@ builtin setopt noaliases
14161418
filename="${reply[-2]}" dirname="${reply[-2]}"
14171419
local_dir="${reply[-3]}" exists=${reply[-1]}
14181420

1421+
# Substitute special strings 3 times deep, like %ID%, %USER%, %PLUGIN%, etc.
1422+
@zinit-substitute 3 id_as filename local_dir dirname 'ICE[atinit]' 'ICE[atload]'
1423+
[[ -f $local_dir${dirname:+/$dirname}/$filename ]] && exists=1
1424+
1425+
.zinit-pack-ice "$id_as" ""
1426+
14191427
local -a arr
14201428
local key
14211429
reply=(
@@ -1609,9 +1617,15 @@ builtin setopt noaliases
16091617
local ___mode="$3" ___limit="$4" ___rst=0 ___retval=0 ___key
16101618
.zinit-any-to-user-plugin "$1" "$2"
16111619
local ___user="${reply[-2]}" ___plugin="${reply[-1]}" ___id_as="${ICE[id-as]:-${reply[-2]}${${reply[-2]:#(%|/)*}:+/}${reply[-1]}}"
1620+
1621+
16121622
local ___pdir_path="${${${(M)___user:#%}:+$___plugin}:-${ZINIT[PLUGINS_DIR]}/${___id_as//\//---}}"
1613-
local ___pdir_orig="$___pdir_path"
16141623
ZINIT[CUR_USR]="$___user" ZINIT[CUR_PLUGIN]="$___plugin" ZINIT[CUR_USPL2]="$___id_as"
1624+
1625+
# Substitute special strings 3 times deep, like %ID%, %USER%, %PLUGIN%, etc.
1626+
@zinit-substitute 3 ___id_as 'ICE[id-as]' ___pdir_path 'ICE[atinit]' 'ICE[atload]'
1627+
local ___pdir_orig="$___pdir_path"
1628+
16151629
if [[ -n ${ICE[teleid]} ]] {
16161630
.zinit-any-to-user-plugin "${ICE[teleid]}"
16171631
___user="${reply[-2]}" ___plugin="${reply[-1]}"

0 commit comments

Comments
Β (0)