1
0
Fork 0
mirror of synced 2024-11-22 15:25:34 -05:00

Add --root option to zmodule

which makes it straighforward to use repositories that contain multiple
modules, like sorin-ionescu/prezto or ohmyzsh/ohmyzsh.
This commit is contained in:
Eric Nielsen 2022-09-26 20:33:49 -05:00
parent 887bfd6a03
commit 05b8e0cdf2
No known key found for this signature in database
GPG key ID: 678AF5E338C87D99
7 changed files with 265 additions and 169 deletions

View file

@ -1,16 +1,20 @@
zmodule() { zmodule() {
local -r zusage="Usage: %B${0}%b <url> [%B-n%b|%B--name%b <module_name>] [options] local -r zusage="Usage: %B${0}%b <url> [%B-n%b|%B--name%b <module_name>] [%B-r%b|%B--root%b <path>] [options]
Add %Bzmodule%b calls to your %B${ZDOTDIR:-${HOME}}/.zimrc%b file to define the modules to be initialized. Add %Bzmodule%b calls to your %B${ZDOTDIR:-${HOME}}/.zimrc%b file to define the modules to be initialized.
The modules are initialized in the same order they are defined. The initialization will be done in the same order it's defined.
<url> Module absolute path or repository URL. The following URL formats <url> Module absolute path or repository URL. The following URL formats
are equivalent: %Bfoo%b, %Bzimfw/foo%b, %Bhttps://github.com/zimfw/foo.git%b. are equivalent: %Bfoo%b, %Bzimfw/foo%b, %Bhttps://github.com/zimfw/foo.git%b.
%B-n%b|%B--name%b <module_name> Set a custom module name. Default: the last component in <url>. If an absolute path is given, the module is considered externally
Use slashes inside the name to organize the module into subdirec- installed, and won't be installed or updated by zimfw.
tories. %B-n%b|%B--name%b <module_name> Set a custom module name. Use slashes inside the name to organize
the module into subdirectories. The module will be installed at
%B${ZIM_HOME}/%b<module_name>.
Default: the last component in <url>.
%B-r%b|%B--root%b <path> Relative path to the module root. Default: none.
Repository options: Per-module options:
%B-b%b|%B--branch%b <branch_name> Use specified branch when installing and updating the module. %B-b%b|%B--branch%b <branch_name> Use specified branch when installing and updating the module.
Overrides the tag option. Default: the repository default branch. Overrides the tag option. Default: the repository default branch.
%B-t%b|%B--tag%b <tag_name> Use specified tag when installing and updating the module. Over- %B-t%b|%B--tag%b <tag_name> Use specified tag when installing and updating the module. Over-
@ -24,25 +28,36 @@ Repository options:
changes are lost on updates. Git submodules are not supported. changes are lost on updates. Git submodules are not supported.
%B--no-submodules%b Don't install or update git submodules. %B--no-submodules%b Don't install or update git submodules.
%B-z%b|%B--frozen%b Don't install or update the module. %B-z%b|%B--frozen%b Don't install or update the module.
The per-module options above are carried over multiple zmodule calls for the same module.
Modules are uniquely identified by their name.
Per-module-root options:
%B--on-pull%b <command> Execute command after installing or updating the module. The com- %B--on-pull%b <command> Execute command after installing or updating the module. The com-
mand is executed in the module root directory. mand is executed in the module root directory.
Initialization options: %B-d%b|%B--disabled%b Don't initialize the module root or uninstall the module.
%B-f%b|%B--fpath%b <path> Add specified path to fpath. The path is relative to the module
root directory. Default: %Bfunctions%b, if the subdirectory exists.
%B-a%b|%B--autoload%b <func_name> Autoload specified function. Default: all valid names inside the
%Bfunctions%b subdirectory, if any.
%B-s%b|%B--source%b <file_path> Source specified file. The file path is relative to the module
root directory. Default: %Binit.zsh%b, if the %Bfunctions%b subdirectory
also exists, or the largest of the files with name matching
%B{init.zsh,module_name.{zsh,plugin.zsh,zsh-theme,sh}}%b, if any.
%B-c%b|%B--cmd%b <command> Execute specified command. Occurrences of the %B{}%b placeholder in
the command are substituted by the module root directory path.
I.e., %B-s 'foo.zsh'%b and %B-c 'source {}/foo.zsh'%b are equivalent.
%B-d%b|%B--disabled%b Don't initialize or uninstall the module.
Setting any initialization option above will disable all the default values from the other The per-module-root options above are carried over multiple zmodule calls for the same mod-
initialization options, so only your provided values are used. I.e. these values are either ule root.
all automatic, or all manual."
Per-call initialization options:
%B-f%b|%B--fpath%b <path> Will add specified path to fpath. The path is relative to the
module root directory. Default: %Bfunctions%b, if the subdirectory
exists and is non-empty.
%B-a%b|%B--autoload%b <func_name> Will autoload specified function. Default: all valid names inside
the %Bfunctions%b subdirectory, if any.
%B-s%b|%B--source%b <file_path> Will source specified file. The path is relative to the module
root directory. Default: %Binit.zsh%b, if a non-empty %Bfunctions%b sub-
directory exists, else the largest of the files matching the glob
%B(init.zsh|%b<root_tail>%B.(zsh|plugin.zsh|zsh-theme|sh))%b, if any.
%B-c%b|%B--cmd%b <command> Will execute specified command. Occurrences of the %B{}%b placeholder
in the command are substituted by the module root directory path.
I.e., %B-s 'foo.zsh'%b and %B-c 'source {}/foo.zsh'%b are equivalent.
Setting any per-call initialization option above will disable the default values from the
other per-call initialization options, so only your provided values will be used. I.e. these
values are either all automatic, or all manual in each zmodule call. To use default values
and also provided values, use separate zmodule calls."
if [[ ${${funcfiletrace[1]%:*}:t} != .zimrc ]]; then if [[ ${${funcfiletrace[1]%:*}:t} != .zimrc ]]; then
print -u2 -PlR "%F{red}${0}: Must be called from %B<%= home %>/.zimrc%b%f" '' ${zusage} print -u2 -PlR "%F{red}${0}: Must be called from %B<%= home %>/.zimrc%b%f" '' ${zusage}
return 2 return 2
@ -52,7 +67,7 @@ Initialization options:
_zfailed=1 _zfailed=1
return 2 return 2
fi fi
local zurl=${1} zname=${1:t} zarg local zurl=${1} zname=${1:t} zroot zarg
local -a zfpaths zfunctions zcmds local -a zfpaths zfunctions zcmds
if [[ ${zurl} =~ ^[^:/]+: ]]; then if [[ ${zurl} =~ ^[^:/]+: ]]; then
zname=${zname%.git} zname=${zname%.git}
@ -64,14 +79,22 @@ Initialization options:
esac esac
fi fi
shift shift
while [[ ${1} == (-n|--name) ]]; do while [[ ${1} == (-n|--name|-r|--root) ]]; do
if (( # < 2 )); then if (( # < 2 )); then
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}:%b Missing argument for zmodule option %B${1}%b%f" '' ${zusage} print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}:%b Missing argument for zmodule option %B${1}%b%f" '' ${zusage}
_zfailed=1 _zfailed=1
return 2 return 2
fi fi
shift case ${1} in
zname=${${1%%/##}##/##} -n|--name)
shift
zname=${${1%%/##}##/##}
;;
-r|--root)
shift
zroot=${${1%%/##}##/##}
;;
esac
shift shift
done done
if [[ ${zurl} == /* ]]; then if [[ ${zurl} == /* ]]; then
@ -80,7 +103,14 @@ Initialization options:
else else
_zdirs[${zname}]=${ZIM_HOME}/modules/${zname} _zdirs[${zname}]=${ZIM_HOME}/modules/${zname}
fi fi
if [[ ${+_zurls[${zname}]} -ne 0 && ${_zurls[${zname}]} != ${zurl} ]]; then
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}:%b Module already defined with a different URL. Expected %B${_zurls[${zname}]}%b%f" '' ${zusage}
_zfailed=1
return 2
fi
_zurls[${zname}]=${zurl} _zurls[${zname}]=${zurl}
local -r zroot_dir=${_zdirs[${zname}]}${zroot:+/${zroot}}
_zroot_dirs+=(${zroot_dir})
# Set default values # Set default values
if (( ! ${+_ztools[${zname}]} )); then if (( ! ${+_ztools[${zname}]} )); then
zstyle -s ':zim:zmodule' use "_ztools[${zname}]" || _ztools[${zname}]=git zstyle -s ':zim:zmodule' use "_ztools[${zname}]" || _ztools[${zname}]=git
@ -122,12 +152,14 @@ Initialization options:
-z|--frozen) _zfrozens[${zname}]=1 ;; -z|--frozen) _zfrozens[${zname}]=1 ;;
--on-pull) --on-pull)
shift shift
_zonpulls[${zname}]="${_zonpulls[${zname}]+${_zonpulls[${zname}]}; }${1}" zarg=${1}
if [[ -n ${zroot} ]] zarg="(builtin cd -q ${zroot}; ${zarg})"
_zonpulls[${zname}]="${_zonpulls[${zname}]+${_zonpulls[${zname}]}; }${zarg}"
;; ;;
-f|--fpath) -f|--fpath)
shift shift
zarg=${1} zarg=${1}
if [[ ${zarg} != /* ]] zarg=${_zdirs[${zname}]}/${zarg} if [[ ${zarg} != /* ]] zarg=${zroot_dir}/${zarg}
zfpaths+=(${zarg}) zfpaths+=(${zarg})
;; ;;
-a|--autoload) -a|--autoload)
@ -137,14 +169,14 @@ Initialization options:
-s|--source) -s|--source)
shift shift
zarg=${1} zarg=${1}
if [[ ${zarg} != /* ]] zarg=${_zdirs[${zname}]}/${zarg} if [[ ${zarg} != /* ]] zarg=${zroot_dir}/${zarg}
zcmds+=("source ${zarg:A}") zcmds+=("source ${zarg:A}")
;; ;;
-c|--cmd) -c|--cmd)
shift shift
zcmds+=(${1//{}/${_zdirs[${zname}]:A}}) zcmds+=(${1//{}/${zroot_dir:A}})
;; ;;
-d|--disabled) _zdisableds[${zname}]=1 ;; -d|--disabled) _zdisabled_root_dirs+=(${zroot_dir}) ;;
*) *)
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}:%b Unknown zmodule option %B${1}%b%f" '' ${zusage} print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}:%b Unknown zmodule option %B${1}%b%f" '' ${zusage}
_zfailed=1 _zfailed=1
@ -157,37 +189,33 @@ Initialization options:
_znames+=(${zname}) _znames+=(${zname})
fi fi
if (( _zflags & 2 )); then if (( _zflags & 2 )); then
if (( _zdisableds[${zname}] )); then if [[ ! -e ${zroot_dir} ]]; then
_zdisabled_dirs+=(${_zdirs[${zname}]}) print -u2 -PR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}: ${zroot_dir}%b not found%f"
else _zfailed=1
if [[ ! -e ${_zdirs[${zname}]} ]]; then return 1
print -u2 -PR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zname}: ${_zdirs[${zname}]}%b not found%f"
_zfailed=1
return 1
fi
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
zfpaths=(${_zdirs[${zname}]}/functions(NF))
# _* functions are autoloaded by compinit
# prompt_*_setup functions are autoloaded by promptinit
zfunctions=(${^zfpaths}/^(*~|*.zwc(|.old)|_*|prompt_*_setup)(N-.:t))
local -ra prezto_scripts=(${_zdirs[${zname}]}/init.zsh(N))
if (( ${#zfpaths} && ${#prezto_scripts} )); then
# this follows the prezto module format, no need to check for other scripts
zcmds=('source '${^prezto_scripts:A})
else
# get script with largest size (descending `O`rder by `L`ength, and return only `[1]` first)
local -ra zscripts=(${_zdirs[${zname}]}/(init.zsh|${zname:t}.(zsh|plugin.zsh|zsh-theme|sh))(NOL[1]))
zcmds=('source '${^zscripts:A})
fi
fi
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
_zimfw_print -u2 -PlR "%F{yellow}<%= warn %>${funcfiletrace[1]}:%B${zname}:%b Nothing found to be initialized. Customize the module name or initialization with %Bzmodule%b options.%f" '' ${zusage}
fi
# Prefix is added to all _zfpaths, _zfunctions and _zcmds to distinguish the originating names
local -r zpre=${zname}$'\0'
_zfpaths+=(${zpre}${^zfpaths})
_zfunctions+=(${zpre}${^zfunctions})
_zcmds+=(${zpre}${^zcmds})
fi fi
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
zfpaths=(${zroot_dir}/functions(NF))
# _* functions are autoloaded by compinit
# prompt_*_setup functions are autoloaded by promptinit
zfunctions=(${^zfpaths}/^(*~|*.zwc(|.old)|_*|prompt_*_setup)(N-.:t))
local -ra prezto_scripts=(${zroot_dir}/init.zsh(N))
if (( ${#zfpaths} && ${#prezto_scripts} )); then
# this follows the prezto module format, no need to check for other scripts
zcmds=('source '${^prezto_scripts:A})
else
# get script with largest size (descending `O`rder by `L`ength, and return only `[1]` first)
local -ra zscripts=(${zroot_dir}/(init.zsh|${zroot_dir:t}.(zsh|plugin.zsh|zsh-theme|sh))(NOL[1]))
zcmds=('source '${^zscripts:A})
fi
fi
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
_zimfw_print -u2 -PlR "%F{yellow}<%= warn %>${funcfiletrace[1]}:%B${zname}:%b Nothing found to be initialized. Customize the module name or initialization with %Bzmodule%b options.%f" '' ${zusage}
fi
# Prefix is added to all _zfpaths, _zfunctions and _zcmds to distinguish the originating root dir
local -r zpre=${zroot_dir}$'\0'
_zfpaths+=(${zpre}${^zfpaths})
_zfunctions+=(${zpre}${^zfunctions})
_zcmds+=(${zpre}${^zcmds})
fi fi
} }

View file

@ -9,4 +9,12 @@ _zimfw_source_zimrc() {
print -u2 -PR "%F{red}<%= failed %>No modules defined in %B${ztarget}%b%f" print -u2 -PR "%F{red}<%= failed %>No modules defined in %B${ztarget}%b%f"
return 1 return 1
fi fi
# Remove all from _zfpaths, _zfunctions and _zcmds with disabled root dirs prefixes
local zroot_dir zpre
for zroot_dir in ${_zdisabled_root_dirs}; do
zpre=${zroot_dir}$'\0'
_zfpaths=(${_zfpaths:#${zpre}*})
_zfunctions=(${_zfunctions:#${zpre}*})
_zcmds=(${_zcmds:#${zpre}*})
done
} }

View file

@ -1,18 +1,18 @@
_zimfw_list_unuseds() { _zimfw_list_unuseds() {
local -i i=1 local -i i=1
local -a zinstalled=(${ZIM_HOME}/modules/*(N/)) local zinstalled=(${ZIM_HOME}/modules/*(N/))
local -ar zdirs=(${(v)_zdirs}) local -r zdirs=(${(v)_zdirs})
# Search into subdirectories # Search into subdirectories
while (( i <= ${#zinstalled} )); do while (( i <= ${#zinstalled} )); do
if (( ${zdirs[(I)${zinstalled[i]}/*]} || ${_zdisabled_dirs[(I)${zinstalled[i]}/*]} )); then if (( ${zdirs[(I)${zinstalled[i]}/*]} )); then
zinstalled+=(${zinstalled[i]}/*(N/)) zinstalled+=(${zinstalled[i]}/*(N/))
zinstalled[i]=() zinstalled[i]=()
else else
(( i++ )) (( i++ ))
fi fi
done done
# Unused = all installed dirs not in zdirs and _zdisabled_dirs # Unused = all installed dirs not in zdirs
_zunused_dirs=(${${zinstalled:|zdirs}:|_zdisabled_dirs}) _zunused_dirs=(${zinstalled:|zdirs})
local zunused local zunused
for zunused (${_zunused_dirs}) _zimfw_print -PR "%B${zunused:t}:%b ${zunused}${1}" for zunused (${_zunused_dirs}) _zimfw_print -PR "%B${zunused:t}:%b ${zunused}${1}"
} }

View file

@ -1,12 +1,12 @@
_zimfw_compile() { _zimfw_compile() {
# Array with unique dirs. ${ZIM_HOME} or any subdirectory should only occur once.
local -Ur zscriptdirs=(${ZIM_HOME} ${${(v)_zdirs##${ZIM_HOME}/*}:A})
# Compile Zim scripts # Compile Zim scripts
local zfile local zroot_dir zfile
for zfile in ${^zscriptdirs}/(^*test*/)#*.zsh(|-theme)(N-.); do for zroot_dir in ${_zroot_dirs:|_zdisabled_root_dirs}; do
if [[ ! ${zfile}.zwc -nt ${zfile} ]]; then for zfile in ${zroot_dir}/(^*test*/)#*.zsh(|-theme)(N-.); do
zcompile -R ${zfile} && _zimfw_print -PR "<%= okay %>%B${zfile}.zwc:%b Compiled" if [[ ! ${zfile}.zwc -nt ${zfile} ]]; then
fi zcompile -UR ${zfile} && _zimfw_print -PR "<%= okay %>%B${zfile}.zwc:%b Compiled"
fi
done
done done
_zimfw_print -P '<%= done %>Done with compile.' _zimfw_print -P '<%= done %>Done with compile.'
} }

View file

@ -1,9 +1,10 @@
_zimfw_run_list() { _zimfw_run_list() {
local -r zname=${1} local -r zname=${1}
print -PnR "%B${zname}:%b ${_zdirs[${zname}]}" local -r zdir=${_zdirs[${zname}]}
if [[ -z ${_zurls[${zname}]} ]] print -Pn ' (external)' print -PnR "%B${zname}:%b ${zdir}"
if (( ${_zfrozens[${zname}]} )) print -Pn ' (frozen)' if [[ -z ${_zurls[${zname}]} ]] print -n ' (external)'
if (( ${_zdisableds[${zname}]} )) print -Pn ' (disabled)' if (( ${_zfrozens[${zname}]} )) print -n ' (frozen)'
if (( ${_zdisabled_root_dirs[(I)${zdir}]} )) print -n ' (disabled)'
print print
if (( _zprintlevel > 1 )); then if (( _zprintlevel > 1 )); then
if [[ ${_zfrozens[${zname}]} -eq 0 && -n ${_zurls[${zname}]} ]]; then if [[ ${_zfrozens[${zname}]} -eq 0 && -n ${_zurls[${zname}]} ]]; then
@ -14,13 +15,24 @@ _zimfw_run_list() {
print -nR "${_ztypes[${zname}]} ${_zrevs[${zname}]}" print -nR "${_ztypes[${zname}]} ${_zrevs[${zname}]}"
fi fi
print -nR ", using ${_ztools[${zname}]}" print -nR ", using ${_ztools[${zname}]}"
if (( ! _zsubmodules[${zname}] )) print -nR ', no git submodules' if (( ! _zsubmodules[${zname}] )) print -n ', no git submodules'
print print
if [[ -n ${_zonpulls[${zname}]} ]] print -R " On-pull: ${_zonpulls[${zname}]}" if [[ -n ${_zonpulls[${zname}]} ]] print -R " On-pull: ${_zonpulls[${zname}]}"
fi fi
# Match and remove the current module prefix from _zfpaths, _zfunctions and _zcmds # Match the current module dir prefix from _zroot_dirs
local -r zpre=${zname}$'\0' local -r zroot_dirs=(${(M)_zroot_dirs:#${zdir}/*})
local -r zfpaths=(${${(M)_zfpaths:#${zpre}*}#${zpre}}) zfunctions=(${${(M)_zfunctions:#${zpre}*}#${zpre}}) zcmds=(${${(M)_zcmds:#${zpre}*}#${zpre}}) if (( ${#zroot_dirs} )); then
print ' Additional root:'
local zroot_dir
for zroot_dir in ${zroot_dirs}; do
print -nR " ${zroot_dir}"
if (( ${_zdisabled_root_dirs[(I)${zroot_dir}]} )) print -n ' (disabled)'
print
done
fi
# Match and remove the prefix from _zfpaths, _zfunctions and _zcmds
local -r zpre="${zdir}(|/*)"$'\0'
local -r zfpaths=(${${(M)_zfpaths:#${~zpre}*}#${~zpre}}) zfunctions=(${${(M)_zfunctions:#${~zpre}*}#${~zpre}}) zcmds=(${${(M)_zcmds:#${~zpre}*}#${~zpre}})
if (( ${#zfpaths} )) print -R ' fpath: '${zfpaths} if (( ${#zfpaths} )) print -R ' fpath: '${zfpaths}
if (( ${#zfunctions} )) print -R ' autoload: '${zfunctions} if (( ${#zfunctions} )) print -R ' autoload: '${zfunctions}
if (( ${#zcmds} )) print -R ' cmd: '${(j:; :)zcmds} if (( ${#zcmds} )) print -R ' cmd: '${(j:; :)zcmds}

View file

@ -26,8 +26,8 @@ Actions:
Options: Options:
%B-q%b Quiet (yes to prompts, and only outputs errors) %B-q%b Quiet (yes to prompts, and only outputs errors)
%B-v%b Verbose (outputs more details)" %B-v%b Verbose (outputs more details)"
local -Ua _znames _zdisabled_dirs local -Ua _znames _zroot_dirs _zdisabled_root_dirs
local -A _zfrozens _zdisableds _ztools _zdirs _zurls _ztypes _zrevs _zsubmodules _zonpulls local -A _zfrozens _ztools _zdirs _zurls _ztypes _zrevs _zsubmodules _zonpulls
local -a _zfpaths _zfunctions _zcmds _zunused_dirs local -a _zfpaths _zfunctions _zcmds _zunused_dirs
local -i _zprintlevel=1 local -i _zprintlevel=1
if (( # > 2 )); then if (( # > 2 )); then

218
zimfw.zsh
View file

@ -85,18 +85,22 @@ _zimfw_build() {
} }
zmodule() { zmodule() {
local -r zusage="Usage: %B${0}%b <url> [%B-n%b|%B--name%b <module_name>] [options] local -r zusage="Usage: %B${0}%b <url> [%B-n%b|%B--name%b <module_name>] [%B-r%b|%B--root%b <path>] [options]
Add %Bzmodule%b calls to your %B${ZDOTDIR:-${HOME}}/.zimrc%b file to define the modules to be initialized. Add %Bzmodule%b calls to your %B${ZDOTDIR:-${HOME}}/.zimrc%b file to define the modules to be initialized.
The modules are initialized in the same order they are defined. The initialization will be done in the same order it's defined.
<url> Module absolute path or repository URL. The following URL formats <url> Module absolute path or repository URL. The following URL formats
are equivalent: %Bfoo%b, %Bzimfw/foo%b, %Bhttps://github.com/zimfw/foo.git%b. are equivalent: %Bfoo%b, %Bzimfw/foo%b, %Bhttps://github.com/zimfw/foo.git%b.
%B-n%b|%B--name%b <module_name> Set a custom module name. Default: the last component in <url>. If an absolute path is given, the module is considered externally
Use slashes inside the name to organize the module into subdirec- installed, and won't be installed or updated by zimfw.
tories. %B-n%b|%B--name%b <module_name> Set a custom module name. Use slashes inside the name to organize
the module into subdirectories. The module will be installed at
%B${ZIM_HOME}/%b<module_name>.
Default: the last component in <url>.
%B-r%b|%B--root%b <path> Relative path to the module root. Default: none.
Repository options: Per-module options:
%B-b%b|%B--branch%b <branch_name> Use specified branch when installing and updating the module. %B-b%b|%B--branch%b <branch_name> Use specified branch when installing and updating the module.
Overrides the tag option. Default: the repository default branch. Overrides the tag option. Default: the repository default branch.
%B-t%b|%B--tag%b <tag_name> Use specified tag when installing and updating the module. Over- %B-t%b|%B--tag%b <tag_name> Use specified tag when installing and updating the module. Over-
@ -110,25 +114,36 @@ Repository options:
changes are lost on updates. Git submodules are not supported. changes are lost on updates. Git submodules are not supported.
%B--no-submodules%b Don't install or update git submodules. %B--no-submodules%b Don't install or update git submodules.
%B-z%b|%B--frozen%b Don't install or update the module. %B-z%b|%B--frozen%b Don't install or update the module.
The per-module options above are carried over multiple zmodule calls for the same module.
Modules are uniquely identified by their name.
Per-module-root options:
%B--on-pull%b <command> Execute command after installing or updating the module. The com- %B--on-pull%b <command> Execute command after installing or updating the module. The com-
mand is executed in the module root directory. mand is executed in the module root directory.
Initialization options: %B-d%b|%B--disabled%b Don't initialize the module root or uninstall the module.
%B-f%b|%B--fpath%b <path> Add specified path to fpath. The path is relative to the module
root directory. Default: %Bfunctions%b, if the subdirectory exists.
%B-a%b|%B--autoload%b <func_name> Autoload specified function. Default: all valid names inside the
%Bfunctions%b subdirectory, if any.
%B-s%b|%B--source%b <file_path> Source specified file. The file path is relative to the module
root directory. Default: %Binit.zsh%b, if the %Bfunctions%b subdirectory
also exists, or the largest of the files with name matching
%B{init.zsh,module_name.{zsh,plugin.zsh,zsh-theme,sh}}%b, if any.
%B-c%b|%B--cmd%b <command> Execute specified command. Occurrences of the %B{}%b placeholder in
the command are substituted by the module root directory path.
I.e., %B-s 'foo.zsh'%b and %B-c 'source {}/foo.zsh'%b are equivalent.
%B-d%b|%B--disabled%b Don't initialize or uninstall the module.
Setting any initialization option above will disable all the default values from the other The per-module-root options above are carried over multiple zmodule calls for the same mod-
initialization options, so only your provided values are used. I.e. these values are either ule root.
all automatic, or all manual."
Per-call initialization options:
%B-f%b|%B--fpath%b <path> Will add specified path to fpath. The path is relative to the
module root directory. Default: %Bfunctions%b, if the subdirectory
exists and is non-empty.
%B-a%b|%B--autoload%b <func_name> Will autoload specified function. Default: all valid names inside
the %Bfunctions%b subdirectory, if any.
%B-s%b|%B--source%b <file_path> Will source specified file. The path is relative to the module
root directory. Default: %Binit.zsh%b, if a non-empty %Bfunctions%b sub-
directory exists, else the largest of the files matching the glob
%B(init.zsh|%b<root_tail>%B.(zsh|plugin.zsh|zsh-theme|sh))%b, if any.
%B-c%b|%B--cmd%b <command> Will execute specified command. Occurrences of the %B{}%b placeholder
in the command are substituted by the module root directory path.
I.e., %B-s 'foo.zsh'%b and %B-c 'source {}/foo.zsh'%b are equivalent.
Setting any per-call initialization option above will disable the default values from the
other per-call initialization options, so only your provided values will be used. I.e. these
values are either all automatic, or all manual in each zmodule call. To use default values
and also provided values, use separate zmodule calls."
if [[ ${${funcfiletrace[1]%:*}:t} != .zimrc ]]; then if [[ ${${funcfiletrace[1]%:*}:t} != .zimrc ]]; then
print -u2 -PlR "%F{red}${0}: Must be called from %B${ZDOTDIR:-${HOME}}/.zimrc%b%f" '' ${zusage} print -u2 -PlR "%F{red}${0}: Must be called from %B${ZDOTDIR:-${HOME}}/.zimrc%b%f" '' ${zusage}
return 2 return 2
@ -138,7 +153,7 @@ Initialization options:
_zfailed=1 _zfailed=1
return 2 return 2
fi fi
local zurl=${1} zname=${1:t} zarg local zurl=${1} zname=${1:t} zroot zarg
local -a zfpaths zfunctions zcmds local -a zfpaths zfunctions zcmds
if [[ ${zurl} =~ ^[^:/]+: ]]; then if [[ ${zurl} =~ ^[^:/]+: ]]; then
zname=${zname%.git} zname=${zname%.git}
@ -150,14 +165,22 @@ Initialization options:
esac esac
fi fi
shift shift
while [[ ${1} == (-n|--name) ]]; do while [[ ${1} == (-n|--name|-r|--root) ]]; do
if (( # < 2 )); then if (( # < 2 )); then
print -u2 -PlR "%F{red}x ${funcfiletrace[1]}:%B${zname}:%b Missing argument for zmodule option %B${1}%b%f" '' ${zusage} print -u2 -PlR "%F{red}x ${funcfiletrace[1]}:%B${zname}:%b Missing argument for zmodule option %B${1}%b%f" '' ${zusage}
_zfailed=1 _zfailed=1
return 2 return 2
fi fi
shift case ${1} in
zname=${${1%%/##}##/##} -n|--name)
shift
zname=${${1%%/##}##/##}
;;
-r|--root)
shift
zroot=${${1%%/##}##/##}
;;
esac
shift shift
done done
if [[ ${zurl} == /* ]]; then if [[ ${zurl} == /* ]]; then
@ -166,7 +189,14 @@ Initialization options:
else else
_zdirs[${zname}]=${ZIM_HOME}/modules/${zname} _zdirs[${zname}]=${ZIM_HOME}/modules/${zname}
fi fi
if [[ ${+_zurls[${zname}]} -ne 0 && ${_zurls[${zname}]} != ${zurl} ]]; then
print -u2 -PlR "%F{red}x ${funcfiletrace[1]}:%B${zname}:%b Module already defined with a different URL. Expected %B${_zurls[${zname}]}%b%f" '' ${zusage}
_zfailed=1
return 2
fi
_zurls[${zname}]=${zurl} _zurls[${zname}]=${zurl}
local -r zroot_dir=${_zdirs[${zname}]}${zroot:+/${zroot}}
_zroot_dirs+=(${zroot_dir})
# Set default values # Set default values
if (( ! ${+_ztools[${zname}]} )); then if (( ! ${+_ztools[${zname}]} )); then
zstyle -s ':zim:zmodule' use "_ztools[${zname}]" || _ztools[${zname}]=git zstyle -s ':zim:zmodule' use "_ztools[${zname}]" || _ztools[${zname}]=git
@ -208,12 +238,14 @@ Initialization options:
-z|--frozen) _zfrozens[${zname}]=1 ;; -z|--frozen) _zfrozens[${zname}]=1 ;;
--on-pull) --on-pull)
shift shift
_zonpulls[${zname}]="${_zonpulls[${zname}]+${_zonpulls[${zname}]}; }${1}" zarg=${1}
if [[ -n ${zroot} ]] zarg="(builtin cd -q ${zroot}; ${zarg})"
_zonpulls[${zname}]="${_zonpulls[${zname}]+${_zonpulls[${zname}]}; }${zarg}"
;; ;;
-f|--fpath) -f|--fpath)
shift shift
zarg=${1} zarg=${1}
if [[ ${zarg} != /* ]] zarg=${_zdirs[${zname}]}/${zarg} if [[ ${zarg} != /* ]] zarg=${zroot_dir}/${zarg}
zfpaths+=(${zarg}) zfpaths+=(${zarg})
;; ;;
-a|--autoload) -a|--autoload)
@ -223,14 +255,14 @@ Initialization options:
-s|--source) -s|--source)
shift shift
zarg=${1} zarg=${1}
if [[ ${zarg} != /* ]] zarg=${_zdirs[${zname}]}/${zarg} if [[ ${zarg} != /* ]] zarg=${zroot_dir}/${zarg}
zcmds+=("source ${zarg:A}") zcmds+=("source ${zarg:A}")
;; ;;
-c|--cmd) -c|--cmd)
shift shift
zcmds+=(${1//{}/${_zdirs[${zname}]:A}}) zcmds+=(${1//{}/${zroot_dir:A}})
;; ;;
-d|--disabled) _zdisableds[${zname}]=1 ;; -d|--disabled) _zdisabled_root_dirs+=(${zroot_dir}) ;;
*) *)
print -u2 -PlR "%F{red}x ${funcfiletrace[1]}:%B${zname}:%b Unknown zmodule option %B${1}%b%f" '' ${zusage} print -u2 -PlR "%F{red}x ${funcfiletrace[1]}:%B${zname}:%b Unknown zmodule option %B${1}%b%f" '' ${zusage}
_zfailed=1 _zfailed=1
@ -243,38 +275,34 @@ Initialization options:
_znames+=(${zname}) _znames+=(${zname})
fi fi
if (( _zflags & 2 )); then if (( _zflags & 2 )); then
if (( _zdisableds[${zname}] )); then if [[ ! -e ${zroot_dir} ]]; then
_zdisabled_dirs+=(${_zdirs[${zname}]}) print -u2 -PR "%F{red}x ${funcfiletrace[1]}:%B${zname}: ${zroot_dir}%b not found%f"
else _zfailed=1
if [[ ! -e ${_zdirs[${zname}]} ]]; then return 1
print -u2 -PR "%F{red}x ${funcfiletrace[1]}:%B${zname}: ${_zdirs[${zname}]}%b not found%f"
_zfailed=1
return 1
fi
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
zfpaths=(${_zdirs[${zname}]}/functions(NF))
# _* functions are autoloaded by compinit
# prompt_*_setup functions are autoloaded by promptinit
zfunctions=(${^zfpaths}/^(*~|*.zwc(|.old)|_*|prompt_*_setup)(N-.:t))
local -ra prezto_scripts=(${_zdirs[${zname}]}/init.zsh(N))
if (( ${#zfpaths} && ${#prezto_scripts} )); then
# this follows the prezto module format, no need to check for other scripts
zcmds=('source '${^prezto_scripts:A})
else
# get script with largest size (descending `O`rder by `L`ength, and return only `[1]` first)
local -ra zscripts=(${_zdirs[${zname}]}/(init.zsh|${zname:t}.(zsh|plugin.zsh|zsh-theme|sh))(NOL[1]))
zcmds=('source '${^zscripts:A})
fi
fi
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
_zimfw_print -u2 -PlR "%F{yellow}! ${funcfiletrace[1]}:%B${zname}:%b Nothing found to be initialized. Customize the module name or initialization with %Bzmodule%b options.%f" '' ${zusage}
fi
# Prefix is added to all _zfpaths, _zfunctions and _zcmds to distinguish the originating names
local -r zpre=${zname}$'\0'
_zfpaths+=(${zpre}${^zfpaths})
_zfunctions+=(${zpre}${^zfunctions})
_zcmds+=(${zpre}${^zcmds})
fi fi
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
zfpaths=(${zroot_dir}/functions(NF))
# _* functions are autoloaded by compinit
# prompt_*_setup functions are autoloaded by promptinit
zfunctions=(${^zfpaths}/^(*~|*.zwc(|.old)|_*|prompt_*_setup)(N-.:t))
local -ra prezto_scripts=(${zroot_dir}/init.zsh(N))
if (( ${#zfpaths} && ${#prezto_scripts} )); then
# this follows the prezto module format, no need to check for other scripts
zcmds=('source '${^prezto_scripts:A})
else
# get script with largest size (descending `O`rder by `L`ength, and return only `[1]` first)
local -ra zscripts=(${zroot_dir}/(init.zsh|${zroot_dir:t}.(zsh|plugin.zsh|zsh-theme|sh))(NOL[1]))
zcmds=('source '${^zscripts:A})
fi
fi
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
_zimfw_print -u2 -PlR "%F{yellow}! ${funcfiletrace[1]}:%B${zname}:%b Nothing found to be initialized. Customize the module name or initialization with %Bzmodule%b options.%f" '' ${zusage}
fi
# Prefix is added to all _zfpaths, _zfunctions and _zcmds to distinguish the originating root dir
local -r zpre=${zroot_dir}$'\0'
_zfpaths+=(${zpre}${^zfpaths})
_zfunctions+=(${zpre}${^zfunctions})
_zcmds+=(${zpre}${^zcmds})
fi fi
} }
@ -289,23 +317,31 @@ _zimfw_source_zimrc() {
print -u2 -PR "%F{red}No modules defined in %B${ztarget}%b%f" print -u2 -PR "%F{red}No modules defined in %B${ztarget}%b%f"
return 1 return 1
fi fi
# Remove all from _zfpaths, _zfunctions and _zcmds with disabled root dirs prefixes
local zroot_dir zpre
for zroot_dir in ${_zdisabled_root_dirs}; do
zpre=${zroot_dir}$'\0'
_zfpaths=(${_zfpaths:#${zpre}*})
_zfunctions=(${_zfunctions:#${zpre}*})
_zcmds=(${_zcmds:#${zpre}*})
done
} }
_zimfw_list_unuseds() { _zimfw_list_unuseds() {
local -i i=1 local -i i=1
local -a zinstalled=(${ZIM_HOME}/modules/*(N/)) local zinstalled=(${ZIM_HOME}/modules/*(N/))
local -ar zdirs=(${(v)_zdirs}) local -r zdirs=(${(v)_zdirs})
# Search into subdirectories # Search into subdirectories
while (( i <= ${#zinstalled} )); do while (( i <= ${#zinstalled} )); do
if (( ${zdirs[(I)${zinstalled[i]}/*]} || ${_zdisabled_dirs[(I)${zinstalled[i]}/*]} )); then if (( ${zdirs[(I)${zinstalled[i]}/*]} )); then
zinstalled+=(${zinstalled[i]}/*(N/)) zinstalled+=(${zinstalled[i]}/*(N/))
zinstalled[i]=() zinstalled[i]=()
else else
(( i++ )) (( i++ ))
fi fi
done done
# Unused = all installed dirs not in zdirs and _zdisabled_dirs # Unused = all installed dirs not in zdirs
_zunused_dirs=(${${zinstalled:|zdirs}:|_zdisabled_dirs}) _zunused_dirs=(${zinstalled:|zdirs})
local zunused local zunused
for zunused (${_zunused_dirs}) _zimfw_print -PR "%B${zunused:t}:%b ${zunused}${1}" for zunused (${_zunused_dirs}) _zimfw_print -PR "%B${zunused:t}:%b ${zunused}${1}"
} }
@ -371,20 +407,20 @@ _zimfw_clean_dumpfile() {
} }
_zimfw_compile() { _zimfw_compile() {
# Array with unique dirs. ${ZIM_HOME} or any subdirectory should only occur once.
local -Ur zscriptdirs=(${ZIM_HOME} ${${(v)_zdirs##${ZIM_HOME}/*}:A})
# Compile Zim scripts # Compile Zim scripts
local zfile local zroot_dir zfile
for zfile in ${^zscriptdirs}/(^*test*/)#*.zsh(|-theme)(N-.); do for zroot_dir in ${_zroot_dirs:|_zdisabled_root_dirs}; do
if [[ ! ${zfile}.zwc -nt ${zfile} ]]; then for zfile in ${zroot_dir}/(^*test*/)#*.zsh(|-theme)(N-.); do
zcompile -R ${zfile} && _zimfw_print -PR "%F{green})%f %B${zfile}.zwc:%b Compiled" if [[ ! ${zfile}.zwc -nt ${zfile} ]]; then
fi zcompile -UR ${zfile} && _zimfw_print -PR "%F{green})%f %B${zfile}.zwc:%b Compiled"
fi
done
done done
_zimfw_print -P 'Done with compile.' _zimfw_print -P 'Done with compile.'
} }
_zimfw_info() { _zimfw_info() {
print -R 'zimfw version: '${_zversion}' (built at 2022-08-02 17:46:34 UTC, previous commit is b5703f9)' print -R 'zimfw version: '${_zversion}' (built at 2022-09-27 01:07:23 UTC, previous commit is 887bfd6)'
print -R 'OSTYPE: '${OSTYPE} print -R 'OSTYPE: '${OSTYPE}
print -R 'TERM: '${TERM} print -R 'TERM: '${TERM}
print -R 'TERM_PROGRAM: '${TERM_PROGRAM} print -R 'TERM_PROGRAM: '${TERM_PROGRAM}
@ -440,10 +476,11 @@ _zimfw_upgrade() {
_zimfw_run_list() { _zimfw_run_list() {
local -r zname=${1} local -r zname=${1}
print -PnR "%B${zname}:%b ${_zdirs[${zname}]}" local -r zdir=${_zdirs[${zname}]}
if [[ -z ${_zurls[${zname}]} ]] print -Pn ' (external)' print -PnR "%B${zname}:%b ${zdir}"
if (( ${_zfrozens[${zname}]} )) print -Pn ' (frozen)' if [[ -z ${_zurls[${zname}]} ]] print -n ' (external)'
if (( ${_zdisableds[${zname}]} )) print -Pn ' (disabled)' if (( ${_zfrozens[${zname}]} )) print -n ' (frozen)'
if (( ${_zdisabled_root_dirs[(I)${zdir}]} )) print -n ' (disabled)'
print print
if (( _zprintlevel > 1 )); then if (( _zprintlevel > 1 )); then
if [[ ${_zfrozens[${zname}]} -eq 0 && -n ${_zurls[${zname}]} ]]; then if [[ ${_zfrozens[${zname}]} -eq 0 && -n ${_zurls[${zname}]} ]]; then
@ -454,13 +491,24 @@ _zimfw_run_list() {
print -nR "${_ztypes[${zname}]} ${_zrevs[${zname}]}" print -nR "${_ztypes[${zname}]} ${_zrevs[${zname}]}"
fi fi
print -nR ", using ${_ztools[${zname}]}" print -nR ", using ${_ztools[${zname}]}"
if (( ! _zsubmodules[${zname}] )) print -nR ', no git submodules' if (( ! _zsubmodules[${zname}] )) print -n ', no git submodules'
print print
if [[ -n ${_zonpulls[${zname}]} ]] print -R " On-pull: ${_zonpulls[${zname}]}" if [[ -n ${_zonpulls[${zname}]} ]] print -R " On-pull: ${_zonpulls[${zname}]}"
fi fi
# Match and remove the current module prefix from _zfpaths, _zfunctions and _zcmds # Match the current module dir prefix from _zroot_dirs
local -r zpre=${zname}$'\0' local -r zroot_dirs=(${(M)_zroot_dirs:#${zdir}/*})
local -r zfpaths=(${${(M)_zfpaths:#${zpre}*}#${zpre}}) zfunctions=(${${(M)_zfunctions:#${zpre}*}#${zpre}}) zcmds=(${${(M)_zcmds:#${zpre}*}#${zpre}}) if (( ${#zroot_dirs} )); then
print ' Additional root:'
local zroot_dir
for zroot_dir in ${zroot_dirs}; do
print -nR " ${zroot_dir}"
if (( ${_zdisabled_root_dirs[(I)${zroot_dir}]} )) print -n ' (disabled)'
print
done
fi
# Match and remove the prefix from _zfpaths, _zfunctions and _zcmds
local -r zpre="${zdir}(|/*)"$'\0'
local -r zfpaths=(${${(M)_zfpaths:#${~zpre}*}#${~zpre}}) zfunctions=(${${(M)_zfunctions:#${~zpre}*}#${~zpre}}) zcmds=(${${(M)_zcmds:#${~zpre}*}#${~zpre}})
if (( ${#zfpaths} )) print -R ' fpath: '${zfpaths} if (( ${#zfpaths} )) print -R ' fpath: '${zfpaths}
if (( ${#zfunctions} )) print -R ' autoload: '${zfunctions} if (( ${#zfunctions} )) print -R ' autoload: '${zfunctions}
if (( ${#zcmds} )) print -R ' cmd: '${(j:; :)zcmds} if (( ${#zcmds} )) print -R ' cmd: '${(j:; :)zcmds}
@ -785,8 +833,8 @@ Actions:
Options: Options:
%B-q%b Quiet (yes to prompts, and only outputs errors) %B-q%b Quiet (yes to prompts, and only outputs errors)
%B-v%b Verbose (outputs more details)" %B-v%b Verbose (outputs more details)"
local -Ua _znames _zdisabled_dirs local -Ua _znames _zroot_dirs _zdisabled_root_dirs
local -A _zfrozens _zdisableds _ztools _zdirs _zurls _ztypes _zrevs _zsubmodules _zonpulls local -A _zfrozens _ztools _zdirs _zurls _ztypes _zrevs _zsubmodules _zonpulls
local -a _zfpaths _zfunctions _zcmds _zunused_dirs local -a _zfpaths _zfunctions _zcmds _zunused_dirs
local -i _zprintlevel=1 local -i _zprintlevel=1
if (( # > 2 )); then if (( # > 2 )); then