zimfw/src/stage2/30_zmodule.zsh.erb

186 lines
7.8 KiB
Plaintext

zmodule() {
local -r zusage="Usage: %B${0}%b <url> [%B-n%b|%B--name%b <module_name>] [options]
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.
<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.
%B-n%b|%B--name%b <module_name> Set a custom module name. Default: the last component in <url>.
Use slashes inside the name to organize the module into subdirec-
tories.
Repository options:
%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.
%B-t%b|%B--tag%b <tag_name> Use specified tag when installing and updating the module. Over-
rides the branch option.
%B-u%b|%B--use%b <%%Bgit%b|%Bdegit%b> Install and update the module using the defined tool. Default is
either defined by %Bzstyle ':zim:zmodule' use '%b<%%Bgit%b|%Bdegit%b>%B'%b, or %Bgit%b
if none is provided.
%Bgit%b requires git itself. Local changes are preserved on updates.
%Bdegit%b requires curl or wget, and currently only works with GitHub
URLs. Modules install faster and take less disk space. Local
changes are lost on updates. Git submodules are not supported.
%B-z%b|%B--frozen%b Don't install or update the module.
Initialization options:
%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 file with largest size and 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
initialization options, so only your provided values are used. I.e. these values are either
all automatic, or all manual."
if [[ ${${funcfiletrace[1]%:*}:t} != .zimrc ]]; then
print -u2 -PlR "%F{red}${0}: Must be called from %B<%= home %>/.zimrc%b%f" '' ${zusage}
return 2
fi
if (( ! # )); then
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}: Missing zmodule url%f" '' ${zusage}
_zfailed=1
return 2
fi
local zurl=${1} zmodule=${1:t} ztool zdir ztype zrev zarg
local -i zdisabled=0 zfrozen=0
local -a zfpaths zfunctions zcmds
zstyle -s ':zim:zmodule' use 'ztool' || ztool=git
if [[ ${zurl} =~ ^[^:/]+: ]]; then
zmodule=${zmodule%.git}
elif [[ ${zurl} != /* ]]; then
# Count number of slashes
case ${#zurl//[^\/]/} in
0) zurl=https://github.com/zimfw/${zurl}.git ;;
1) zurl=https://github.com/${zurl}.git ;;
esac
fi
shift
if [[ ${1} == (-n|--name) ]]; then
if (( # < 2 )); then
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zmodule}:%b Missing argument for zmodule option %B${1}%b%f" '' ${zusage}
_zfailed=1
return 2
fi
shift
zmodule=${${1%%/##}##/##}
shift
fi
if [[ ${zurl} == /* ]]; then
zdir=${zurl%%/##}
zurl=
else
zdir=${ZIM_HOME}/modules/${zmodule}
fi
while (( # > 0 )); do
case ${1} in
-b|--branch|-t|--tag|-u|--use|-f|--fpath|-a|--autoload|-s|--source|-c|--cmd)
if (( # < 2 )); then
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zmodule}:%b Missing argument for zmodule option %B${1}%b%f" '' ${zusage}
_zfailed=1
return 2
fi
;;
esac
case ${1} in
-b|--branch|-t|--tag|-u|--use)
if [[ -z ${zurl} ]] _zimfw_print -u2 -PR "%F{yellow}<%= warn %>${funcfiletrace[1]}:%B${zmodule}:%b The zmodule option %B${1}%b has no effect for external modules%f"
;;
esac
case ${1} in
-b|--branch)
shift
ztype=branch
zrev=${1}
;;
-t|--tag)
shift
ztype=tag
zrev=${1}
;;
-u|--use)
shift
ztool=${1}
;;
-z|--frozen) zfrozen=1 ;;
-f|--fpath)
shift
zarg=${1}
if [[ ${zarg} != /* ]] zarg=${zdir}/${zarg}
zfpaths+=(${zarg})
;;
-a|--autoload)
shift
zfunctions+=(${1})
;;
-s|--source)
shift
zarg=${1}
if [[ ${zarg} != /* ]] zarg=${zdir}/${zarg}
zcmds+=("source ${zarg:A}")
;;
-c|--cmd)
shift
zcmds+=(${1//{}/${zdir:A}})
;;
-d|--disabled) zdisabled=1 ;;
*)
print -u2 -PlR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zmodule}:%b Unknown zmodule option %B${1}%b%f" '' ${zusage}
_zfailed=1
return 2
;;
esac
shift
done
if (( _zflags & 1 )); then
_zmodules_zargs+=("${ztool}" "${_zargs_action}" "${zmodule}" "${zdir}" "${zurl}" "${ztype}" "${zrev}" "${zfrozen}" "${zdisabled}")
fi
if (( _zflags & 2 )); then
if (( zdisabled )); then
_zdisabled_dirs+=(${zdir})
else
if [[ ! -e ${zdir} ]]; then
if [[ -z ${zurl} ]]; then
print -u2 -PR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zmodule}: ${zdir}%b not found%f"
else
print -u2 -PR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zmodule}:%b Not installed. Run %Bzimfw install%b to install.%f"
fi
_zfailed=1
return 1
fi
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
zfpaths=(${zdir}/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=(${zdir}/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=(${zdir}/(init.zsh|${zmodule: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${zmodule}:%b Nothing found to be initialized. Customize the module name or initialization with %Bzmodule%b options.%f" '' ${zusage}
fi
_zdirs+=(${zdir})
# Prefix is added to all _zfpaths, _zfunctions and _zcmds to distinguish the originating modules
local -r zpre=${zmodule}$'\0'
_zfpaths+=(${zpre}${^zfpaths})
_zfunctions+=(${zpre}${^zfunctions})
_zcmds+=(${zpre}${^zcmds})
fi
fi
}